这个 WITH 子句的语法错误在哪里?

Where is the syntax error in this WITH clause?

我正在尝试 this problem LeetCode。该界面抛出语法错误,但对于我来说我无法弄清楚。这是问题:

Table: Stadium

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| id            | int     |
| visit_date    | date    |
| people        | int     |
+---------------+---------+

visit_date is the primary key for this table. Each row of this table contains the visit date and visit id to the stadium with the number of people during the visit. No two rows will have the same visit_date, and as the id increases, the dates increase as well.

Write an SQL query to display the records with three or more rows with consecutive id's, and the number of people is greater than or equal to 100 for each.

Return the result table ordered by visit_date in ascending order.

The query result format is in the following example.

Stadium table:
+------+------------+-----------+
| id   | visit_date | people    |
+------+------------+-----------+
| 1    | 2017-01-01 | 10        |
| 2    | 2017-01-02 | 109       |
| 3    | 2017-01-03 | 150       |
| 4    | 2017-01-04 | 99        |
| 5    | 2017-01-05 | 145       |
| 6    | 2017-01-06 | 1455      |
| 7    | 2017-01-07 | 199       |
| 8    | 2017-01-09 | 188       |
+------+------------+-----------+

Result table:
+------+------------+-----------+
| id   | visit_date | people    |
+------+------------+-----------+
| 5    | 2017-01-05 | 145       |
| 6    | 2017-01-06 | 1455      |
| 7    | 2017-01-07 | 199       |
| 8    | 2017-01-09 | 188       |
+------+------------+-----------+

The four rows with ids 5, 6, 7, and 8 have consecutive ids and each of them has >= 100 people attended. Note that row 8 was included even though the visit_date was not the next day after row 7. The rows with ids 2 and 3 are not included because we need at least three consecutive ids.

Here's 带有数据的 fiddle(注意:我仍在尝试弄清楚如何插入日期,所以我将日期保存为字符串)。

谁能找出下面查询中的语法错误?

# Write your MySQL query statement below
SET @rowIndex := 0;

WITH s1 as (
    SELECT @rowIndex := @rowIndex + 1 as rowIndex, s.*
    FROM Stadium as s
    WHERE s.people >= 100
    GROUP BY s.id
)

SELECT s2.id, s2.visit_date, s2.people
FROM s1 as s2
GROUP BY s2.rowIndex - s2.id, s2.id, s2.visit_date, s2.people
ORDER BY s2.visit_date

错误信息:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WITH s1 (rowIndex, id, visit_date, people) as ( SELECT @rowIndex := @rowInde' at line 4

此外,LeetCode 界面使用 MySQL v8.0,所以我认为这不是问题所在。

我使用下面的查询作为参考。 (Original.)

SET @rowIndex := -1;
SELECT ROUND(AVG(t.LAT_N), 4) FROM
(
SELECT @rowIndex := @rowIndex+1 AS rowIndex, s.LAT_N FROM STATION AS s ORDER BY s.LAT_N
) AS t
WHERE t.rowIndex IN (FLOOR(@rowIndex / 2), CEIL(@rowIndex / 2));

谢谢。

编辑:

为了将来参考,这是我提出的最终查询:

# Write your MySQL query statement below
WITH s1 as (
    SELECT ROW_NUMBER() OVER (ORDER BY s.id) as rowIndex, s.*
    FROM Stadium as s
    WHERE s.people >= 100
    GROUP BY s.id, s.visit_date, s.people
), s2 as (
    SELECT COUNT(s.id) OVER (PARTITION BY s.id-s.rowIndex) as groupSize, s.*
    FROM s1 as s
)

SELECT s3.id, s3.visit_date, s3.people
FROM s2 as s3
GROUP BY s3.groupSize, s3.id, s3.visit_date, s3.people
HAVING s3.groupSize >= 3
ORDER BY s3.visit_date
  • 不确定你可能会遇到什么问题,但这会通过 LeetCode:

  • 不确定这样做是否正确:

SELECT DISTINCT S1.id,
                S1.visit_date,
                S1.people
FROM stadium AS S1,
     stadium AS S2,
     stadium AS S3
WHERE S1.people > 99
  AND S2.people > 99
  AND S3.people > 99
  AND ( (S2.id = S1.id + 1
         AND S3.id = S1.id + 2)
       OR (S2.id = S1.id - 1
           AND S3.id = S1.id + 1)
       OR (S2.id = S1.id - 1
           AND S3.id = S1.id - 2) )
ORDER BY id ASC;

您已确认您使用的是 MySQL 8.0.21 服务器,因此我唯一的其他建议是您尝试在一个调用中 运行 两个 SQL 语句:

SET @rowIndex := 0;

WITH s1 as (
  SELECT...

大多数 MySQL 连接器默认不支持 multi-query。换句话说,每次调用只能执行一个语句。一旦 MySQL 在您的第一个 ; 之后看到 any 语法,它就会将其视为语法错误。

您没有理由使用 multi-query。只是 运行 将这两个语句分开。只要您使用同一个会话,您的 @rowIndex 值将可用于后续语句。

MySQL 的前工程总监曾经告诉我,“没有理由 multi-query 应该存在。”