MySQL在子句“WHERE IN(SELECT ...)”中手动添加id

时间:2013-09-04 11:53:18

标签: mysql

我有这个问题:

SELECT * from t1
WHERE child_id IN (SELECT child_id from t2 WHERE parent_id='1234')
OR parent_id='1234'

可以做这样的事情:

SELECT * from t1
WHERE child_id IN (
    (SELECT child_id from t2 WHERE parent_id='1234')
    UNION '5678'
)

UNION关键字会显示错误消息,我尝试在google上搜索但是当您搜索“Mysql concat IN”时很难找到某些内容

我希望我的问题很清楚

2 个答案:

答案 0 :(得分:3)

是的,就像这样:

SELECT * from t1
WHERE child_id IN 
(
  SELECT child_id from t2 WHERE parent_id='1234'
  UNION  
  SELECT 5678
)

答案 1 :(得分:1)

查询不同(我的意思是示例和带错误的示例),但您可以这样做:

SELECT * from t1
WHERE child_id IN (SELECT child_id from t2 WHERE parent_id='1234'
                   union all
                   select '5678'
                  );