0

How to set joins based on conditions, I am trying to use if or case for doing this, how can I achieve this. mysql error Code: 1241. Operand should contain 1 column(s)

SET @sUserId = 15;

SELECT userClip.userId
FROM
  ( select if(@sUserId > 0 ,(select * from groups),(select * from otherGroups))folderId)abc
INNER JOIN userClip
on abc.folderId = userClip.folderId
pitu
  • 822
  • 3
  • 11
  • 35

1 Answers1

0

You could split your condition into two LEFT JOINS. This should work since your @sUserId can't be > and <= 0 at the same time. I also changed userClip to userFolder because it looked like you mixed those up (please give feedback if not).

TWO LEFT_JOINS:

SELECT userFolder.userId
FROM
 userFolder 
LEFT JOIN (SELECT * FROM groups) as abc   
on(@sUserId>0 and abc.folderId = userFolder.folderId)
LEFT JOIN (SELECT * FROM otherGroups) as bcd 
on(@sUserId>=0 and bcd.folderId = userFolder.folderId)
iLikeMySql
  • 736
  • 3
  • 7