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;
相关推荐
NightDW9 分钟前
连续周更任务模块的设计与实现
java·后端·mysql
一起努力啊~41 分钟前
算法题打卡力扣第34题:在排序数组中查找元素的第一个和最后一个位置(mid)
数据结构·算法·leetcode
码农阿豪1 小时前
KingbaseES数据库增删改查操作分享
数据库·oracle
言之。1 小时前
Django REST框架核心:GenericAPIView详解
数据库·python·django
DemonAvenger1 小时前
MySQL存储引擎深度对比:InnoDB vs MyISAM及其应用场景解析
数据库·mysql·性能优化
1白天的黑夜12 小时前
链表-143.重排链表-力扣(LeetCode)
数据结构·leetcode·链表
paid槮2 小时前
MySQL的简单介绍
数据库·mysql
快去睡觉~9 小时前
力扣73:矩阵置零
算法·leetcode·矩阵
岁忧9 小时前
(nice!!!)(LeetCode 每日一题) 679. 24 点游戏 (深度优先搜索)
java·c++·leetcode·游戏·go·深度优先
小欣加油9 小时前
leetcode 3 无重复字符的最长子串
c++·算法·leetcode