使用IN语句组合两个SQL查询

时间:2017-09-24 01:43:30

标签: mysql sql

我有sql查询:

SELECT DISTINCT project_id 
FROM projects 
WHERE  (meta_key = "Project Name" AND meta_value LIKE '%t%' )

返回

project_id
251
299
302
319
317
307
310
312
371
364
365
366
368

我还有另一个疑问:

SELECT DISTINCT project_id
FROM tags
WHERE (tag LIKE '%t%')

返回:

project_id
251

我尝试将这两个语句与IN语句结合起来,以便到达那里。

SELECT DISTINCT project_id 
FROM projects 
WHERE (meta_key = "Project Name" AND meta_value LIKE '%t%' )
IN (SELECT DISTINCT project_id
    FROM tags
    WHERE (tag LIKE '%t%'))

但是,我没有得到任何结果。

project_id

我想我应该

project_id
251

我在这里正确使用IN声明吗?如果不是我应该使用哪个工具来完成此任务。谢谢。

1 个答案:

答案 0 :(得分:2)

只需将 project_id 分配给IN()子句表达式即可。现在,WHERE评估其他... WHERE (meta_key = 'Project Name' AND meta_value LIKE '%t%' ) AND project_id IN (SELECT DISTINCT project_id FROM tags WHERE (tag LIKE '%t%')) 条件的布尔逻辑, False True 永远不会等同于三位整数。< / p>

NSItemProvider