SELECT count(res_c.id) FROM res_c WHERE id = '5552';
计算8条记录。工作正常但需要进一步检查。我的下面尝试超过一千 - 所以不能正常工作。我认为这是因为res_sear也有一个“ID”列。我该如何解决这个问题?下面应该像上面那样计算8但是检查并排除res_sear.position ='X'的记录。
SELECT count(res_c.id)
FROM res_c, res_sear
WHERE res_c.id = '5552'
AND res_sear.id NOT IN
(SELECT res_sear.id FROM res_sear WHERE res_sear.position = 'X');
我需要加入吗?什么是正确的方法?
答案 0 :(得分:2)
res_sear
子句中不应该有FROM
。更确切地说:
SELECT count(res_c.id)
FROM res_c
WHERE res_c.id = '5552'
AND res_c.id NOT IN -- res_c.id here, not res_sear.id !!
(SELECT res_sear.id FROM res_sear WHERE res_sear.position = 'X');
答案 1 :(得分:1)
你错过了res_c和amp;之间的连接条件。外部查询中的res_sear。只需添加可以连接两个表的列,您应该很好。
答案 2 :(得分:1)
你可以left join
喜欢
SELECT count(res_c.id)
FROM res_c
left join res_sear
on res_c.id = res_sear.id
and res_sear.position = 'X'
WHERE res_c.id = '5552'
AND res_sear.id is null;
(OR)就像下面的
SELECT count(res_c.id)
FROM res_c JOIN res_sear
on res_c.id = res_sear.id
WHERE res_c.id = '5552'
AND res_sear.position <> 'X';