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;
相关推荐
Owen_Q11 分钟前
Leetcode百题斩-二分搜索
算法·leetcode·职场和发展
czhc114007566326 分钟前
LINUX712 MYSQL;磁盘分区;NFS
数据库·mysql·adb
叁沐1 小时前
MySQL 13 为什么表数据删掉一半,表文件大小不变?
mysql
不太可爱的大白1 小时前
Mysql:分库分表
数据库·mysql
四季豆豆豆1 小时前
博客项目 laravel vue mysql 第四章 分类功能
vue.js·mysql·laravel
zstar-_1 小时前
Claude code在Windows上的配置流程
笔记·算法·leetcode
十五年专注C++开发1 小时前
hiredis: 一个轻量级、高性能的 C 语言 Redis 客户端库
开发语言·数据库·c++·redis·缓存
憨堡包^—^2 小时前
Docker —— MySQL主从复制集群
mysql·docker·容器
bianguanyue3 小时前
SQLite密码修改故障排查:RSA加密随机性导致的数据库匹配问题
数据库·sqlite·c#
亚马逊云开发者3 小时前
将 Go 应用从 x86 平台迁移至 Amazon Graviton:场景剖析与最佳实践
linux·数据库·golang