如何根据mysql中的条件添加连接

时间:2016-05-30 09:16:01

标签: mysql stored-procedures subquery inner-join derived-table

如何根据条件设置联接,我试图使用if或case来做到这一点,我该如何实现这一点。 mysql错误代码:1241。操作数应包含1列

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

1 个答案:

答案 0 :(得分:0)

您可以将您的病情分成两个LEFT JOINS。这应该有效,因为你的@sUserId不能>并且< = 0同时。 我还将userClip更改为userFolder,因为看起来你把它们混合起来(如果没有,请提供反馈)。

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)