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;
相关推荐
6Hzlia4 分钟前
【Hot 100 刷题计划】 LeetCode 1143. 最长公共子序列 | C++ 二维DP 与 哨兵技巧
c++·算法·leetcode
农村小镇哥6 分钟前
PHP递归遍历+MYSQL介绍+MYSQL基本操作
开发语言·mysql·php
东北甜妹12 分钟前
Redis Cluster 集群
数据库
(Charon)18 分钟前
【kv存储】基于 C 的 KV 存储项目:主从单向同步是怎么实现的
数据库
Jul1en_21 分钟前
【Redis】String 类型命令、编码方式与应用场景
数据库·redis·缓存
lifallen30 分钟前
一篇文章讲透 Flink State
大数据·数据库·python·flink
赵渝强老师34 分钟前
【赵渝强老师】MySQL数据库的分库与分表
数据库·mysql
XDHCOM41 分钟前
利用MSSQL解析优化数据库性能,提升效率,驱动业务创新与稳定发展
数据库·sqlserver
·云扬·1 小时前
MySQL分区实战指南:从原理到落地的完整攻略
数据库·mysql
雨墨✘1 小时前
PHP怎么执行Shell命令_exec与shell_exec区别说明【说明】
jvm·数据库·python