是否可以进行如下查询:WHERE IF(q.id NOT IN(SELECT ...?

时间:2010-08-10 20:29:58

标签: sql

是否可以像这样进行查询?那有一些错误...... 查询应检查用户是否确实使用q.id回答了问题。

SELECT pictureid,
       id,
       points 
  FROM questions q 
 WHERE IF (q.id NOT IN (SELECT questions_id 
                          FROM history h 
                         WHERE h.users_id = 3))

3 个答案:

答案 0 :(得分:7)

SELECT pictureid,id,points 
FROM questions q 
WHERE q.id NOT IN (
    SELECT questions_id 
    FROM history  
    WHERE users_id = 3)

答案 1 :(得分:1)

 SELECT pictureid,id,points 
 FROM questions q 
 WHERE q.id not in 
  (SELECT questions_id FROM history h WHERE h.users_id = 3);

答案 2 :(得分:1)

使用NOT EXISTS也会解决使用NULL的{​​{1}}问题

IN

如果您想使用IN执行此操作

SELECT pictureid, id, points
FROM questions q
WHERE NOT EXISTS
(
    SELECT questions_id
    FROM history h
    WHERE h.questions_id = q.questions_id
    AND h.users_id = 3
)