leetcode with MySQL:183. Customers Who Never Order

题目:

Table: Customers

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
+-------------+---------+
id is the primary key column for this table.
Each row of this table indicates the ID and name of a customer.

Table: Orders

+-------------+------+
| Column Name | Type |
+-------------+------+
| id | int |
| customerId | int |
+-------------+------+
id is the primary key column for this table.
customerId is a foreign key of the ID from the Customers table.
Each row of this table indicates the ID of an order and the ID of the customer who ordered it.

Write an SQL query to report all customers who never order anything.

Return the result table in any order.

给定两个table,一个是消费者资讯,一个是订单资讯
找出没点过东西的消费者

SELECT name as CustomersFROM CustomersLEFT JOIN Orders ON Customers.id = Orders.customerIdWHERE Orders.customerId IS NULL;

将两个table结合,条件订消费者id和订单消费者id相等
记得用left join这样未达成条件的人customerId才会是NULL
之后将customerId是NULL的人选出就好
最后执行时间377ms(faster than 98.66%)

那我们下题见


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章