从表中选择不在另一个有条件的表中

时间:2012-12-10 14:22:09

标签: mysql

有两张桌子 节点:

nid INT(10)
type (VARCHAR(32))

node_hit_count

nid INT(10)
totalcount (bigint(20))

我需要从nid表中选择node表中不在node_hit_count表格中的所有WHEREnode feature类型等于{{1} }或homecompetition

我尝试过,错了,我的思绪会被打击:/

select * from node left join node_hit_counter on node.nid=node_hit_counter.nid 

where hit.nid is null and node.type IN ('feature', 'home', 'competition')

2 个答案:

答案 0 :(得分:2)

hit.nid子句中的WHERE是什么。您必须使用WHERE node.nid IS NULL,而不是这样:

select node.*
from node 
left join node_hit_counter on node.nid = node_hit_counter.nid 
where node.nid is null 
  and node.type IN ('feature', 'home', 'competition');

SELECT *
FROM node
WHERE nid NOT IN(SELECT nid FROM node_hit_counter WHERE nid IS NOT NULL)
  AND type IN ('feature', 'home', 'competition');

答案 1 :(得分:1)

试试这个,你可以在where子句中使用你的hit.nid:o)

select n.* 
from node n
left join node_hit_counter hit on n.nid=hit.nid 
where hit.nid is null 
and n.type IN ('feature', 'home', 'competition')