LeetCode //MySQL - 183. Customers Who Never Order

183. Customers Who Never Order

Table: Customers

±------------±--------+

| Column Name | Type |

±------------±--------+

| id | int |

| name | varchar |

±------------±--------+

id is the primary key (column with unique values) 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 with unique values) for this table.

customerId is a foreign key (reference columns) 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 a solution to find all customers who never order anything.

Return the result table in any order.

The result format is in the following example.

Example 1:

Input:

Customers table:

±---±------+

| id | name |

±---±------+

| 1 | Joe |

| 2 | Henry |

| 3 | Sam |

| 4 | Max |

±---±------+

Orders table:

±---±-----------+

| id | customerId |

±---±-----------+

| 1 | 3 |

| 2 | 1 |

±---±-----------+
Output:

±----------+

| Customers |

±----------+

| Henry |

| Max |

±----------+

From: LeetCode

Link: 183. Customers Who Never Order


Solution:

Ideas:
  1. LEFT JOIN is used to join the Customers table with the Orders table. This will include all rows from the Customers table and the matching rows from the Orders table.
  2. The WHERE O.customerId IS NULL clause filters out customers who have placed orders (i.e., those who have matching rows in the Orders table).
Code:
sql 复制代码
SELECT C.name AS Customers
FROM Customers C
LEFT JOIN Orders O ON C.id = O.customerId
WHERE O.customerId IS NULL;
相关推荐
Yeats_Liao33 分钟前
时序数据库系列(六):物联网监控系统实战
数据库·后端·物联网·时序数据库
小安同学iter38 分钟前
SQL50+Hot100系列(11.7)
java·算法·leetcode·hot100·sql50
珊珊而川40 分钟前
MAC-SQL:黄金标准错误
数据库·sql
一 乐2 小时前
智慧党建|党务学习|基于SprinBoot+vue的智慧党建学习平台(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·学习
许愿OvO3 小时前
MySQL触发器
android·mysql·adb
谈笑也风生3 小时前
只出现一次的数字 II(一)
数据结构·算法·leetcode
lcanfly4 小时前
Mysql作业4
数据库·mysql
aloha_7894 小时前
测试开发工程师面经准备(sxf)
java·python·leetcode·压力测试
蓝象_4 小时前
docker安装配置mysql
mysql·docker·容器
喜欢踢足球的老罗4 小时前
认证与授权:详解大型系统中用户中心与RBAC的共生关系
数据库·rbac