NOT EXISTS - SQL查询

时间:2017-06-26 17:11:02

标签: mysql sql

我的sql数据库中有两个表

enter image description here

我需要编写SQL查询,找到没有租借任何电影的客户。查看表格,这些客户的ID为3和4

我试过这个,但它没有用。我似乎在创建查询时缺少简单的逻辑

SELECT name, last_Name, movie_name
FROM Customers, Rented_movies
WHERE Rented_movies.customer_id = Customers.customer_id
AND NOT EXISTS(customer_id);

有任何帮助吗?非常感谢!

谢谢

2 个答案:

答案 0 :(得分:2)

尝试这种方式

SELECT name, last_Name, movie_name FROM Customers WHERE customer_id NOT IN (SELECT customer_id FROM Rented_movies)

答案 1 :(得分:1)

试试这个:

 SELECT * FROM Customers WHERE customer_id NOT IN (SELECT customer_id FROM Rented_movies);

如果必须使用NOT EXIST语句,那么这也应该有效:

SELECT * FROM Customers C
WHERE NOT EXISTS(SELECT * FROM Rented_movies WHERE customer_id = C.customer_id);