0

我想要一个查询返回另一个表中不存在的值。我目前运行两个查询并在代码中进行交集。我被多列的语法和语句的存在所困扰where

第一个查询:

SELECT sid, cid 
FROM  Table2
where used = 0
group by sid, cid

主要查询:

SELECT    sid, cid, count(1) as cnt 
FROM      Table1
WHERE     ##not any pair of (sid, cid) returned from first query##
GROUP BY  sid, cid 
HAVING    cnt < 20 
LIMIT     50

什么是完整的主查询?

4

1 回答 1

0

尝试:

SELECT    t1.sid, t1.cid, count(1) as cnt 
FROM      Table1 t1
LEFT JOIN Table2 t2
ON        t1.sid = t2.sid AND t1.cid = t2.cid AND t2.used = 0
WHERE     t2.sid IS NULL AND t2.cid IS NULL
GROUP BY  sid, cid 
HAVING    cnt < 20 
LIMIT     50
于 2013-02-11T11:57:08.500 回答