返回介绍

solution / 1600-1699 / 1613.Find the Missing IDs / README_EN

发布于 2024-06-17 01:03:16 字数 2206 浏览 0 评论 0 收藏 0

1613. Find the Missing IDs

中文文档

Description

Table: Customers

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| customer_id   | int   |
| customer_name | varchar |
+---------------+---------+
customer_id is the column with unique values for this table.
Each row of this table contains the name and the id customer.

 

Write a solution to find the missing customer IDs. The missing IDs are ones that are not in the Customers table but are in the range between 1 and the maximum customer_id present in the table.

Notice that the maximum customer_id will not exceed 100.

Return the result table ordered by ids in ascending order.

The result format is in the following example.

 

Example 1:

Input: 
Customers table:
+-------------+---------------+
| customer_id | customer_name |
+-------------+---------------+
| 1       | Alice     |
| 4       | Bob       |
| 5       | Charlie     |
+-------------+---------------+
Output: 
+-----+
| ids |
+-----+
| 2   |
| 3   |
+-----+
Explanation: 
The maximum customer_id present in the table is 5, so in the range [1,5], IDs 2 and 3 are missing from the table.

Solutions

Solution 1

# Write your MySQL query statement below
WITH RECURSIVE
  t AS (
    SELECT
      1 AS n
    UNION ALL
    SELECT
      n + 1
    FROM t
    WHERE n < 100
  )
SELECT
  n AS ids
FROM t
WHERE
  n < (
    SELECT
      MAX(customer_id)
    FROM Customers
  )
  AND n NOT IN (
    SELECT
      customer_id
    FROM Customers
  );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文