力 扣 183. Customers who never order

力 扣 183. Customers who never order

https://leetcode-cn.com/problems/customers-who-never-order/

A website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.

Customers 表:

+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+
Orders 表:

+ ---- + ------------ +
| Id | CustomerId |
+ ---- + ------------ +
| 1 | 3 |
| 2 | 1 |
+ ---- + ------------ +
For example, given the above table, your query should return:

+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+

 

# Write your MySQL query statement below
#两表左连接
select Customers.name as Customers#起别名
from Customers left join Orders on Customers.id=Orders.CustomerId#左连接,条件是两表的Customers.id=Orders.CustomerId
where Orders.CustomerId is null;#查找知id是Null的is null;查找知id不是Null的is not null;

is null 与 is not null

For example,
select from the User table to know that name is Null *
from user
where name is null
-------------------------------- ---
name is not null
select * from use where name is not null

Published 23 original articles · praised 0 · visits 137

Guess you like

Origin blog.csdn.net/qq_35683407/article/details/105426889