MYSQL不存在不起作用

时间:2017-03-02 05:22:54

标签: mysql

我有2张桌子

courseobjects表

id_objects |  type      |  id_type | completed | id_courses
41         |movie       |25        |0          |27 
42         |presentation|11        |0          |27
43         |movie       |26        |0          |27

courseobjectsresults

id_courseresult | id_courseobject |id_user |result
92              |41               |62      |1      
  

我想显示其中没有id的courseobjects表行   courseobjectsresults(id_courseobject)

我试过:

SELECT * FROM courseobjects WHERE NOT EXISTS (SELECT * FROM courseobjectsresults WHERE courseobjectsresults.id_courseobject <> '41')

我得到的输出是Null但它应该显示来自courseobject的id为42和43

的行

感谢。

2 个答案:

答案 0 :(得分:1)

您的查询在逻辑上有点不对,而且您也只是针对ID 41进行测试。请尝试相反:

SELECT *
FROM courseobjects
WHERE NOT EXISTS (
    SELECT 1
    FROM courseobjectsresults
    HERE courseobjectsresults.id_courseobject = courseobjects.id_objects
)

答案 1 :(得分:0)

替代不存在:

您可以使用(where column IN)和(where column NOT IN