简体   繁体   English

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

[英]Where is the syntax error in this WITH clause?

I'm trying out this problem on LeetCode.我正在 LeetCode 上尝试这个问题 The interface is throwing a syntax error, but for the life of me I can't figure it out.界面抛出了一个语法错误,但对于我的生活,我无法弄清楚。 Here's the question:这是问题:

Table: Stadium表:体育场

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

visit_date is the primary key for this table. visit_date 是该表的主键。 Each row of this table contains the visit date and visit id to the stadium with the number of people during the visit.该表的每一行都包含访问日期和访问 id 以及访问期间的人数。 No two rows will have the same visit_date, and as the id increases, the dates increase as well.没有两行会具有相同的visit_date,并且随着id 的增加,日期也会增加。

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.写一个SQL查询,显示三行或三行以上的连续id的记录,并且每行的人数大于或等于100。

Return the result table ordered by visit_date in ascending order.返回按visit_date升序排列的结果表。

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. id 为 5、6、7 和 8 的四行具有连续的 id,每行都有 >= 100 人参加。 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.请注意,即使访问日期不是第 7 行之后的第二天,第 8 行也被包括在内。 id 为 2 和 3 的行不包括在内,因为我们需要至少三个连续的 id。

Here's a fiddle with the data (NOTE: I'm still trying to figure out how to insert dates, so I saved the dates as strings instead).这是对数据的摆弄(注意:我仍在尝试弄清楚如何插入日期,所以我将日期保存为字符串)。

Can anyone spot the syntax error in the query below?任何人都可以在下面的查询中发现语法错误吗?

# 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

Error message:错误信息:

You have an error in your SQL syntax;您的 SQL 语法有错误; 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检查与您的 MySQL 服务器版本相对应的手册,以获取在 'WITH s1 (rowIndex, id, visit_date, people) 附近使用的正确语法作为 ( SELECT @rowIndex := @rowInde' at line 4

Also, the LeetCode interface uses MySQL v8.0, so I don't think that's the problem.另外,LeetCode 接口使用的是 MySQL v8.0,所以我认为这不是问题。

I was using the query below as a reference.我使用下面的查询作为参考。 ( 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));

Thanks.谢谢。

Edit:编辑:

For future reference, here's the final query I came up with:为了将来参考,这是我提出的最终查询:

# 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

You confirmed that you are using MySQL 8.0.21 server so the only other suggestion I have is that you're trying to run two SQL statements in one call:您确认您使用的是 MySQL 8.0.21 服务器,因此我唯一的其他建议是您尝试在一次调用中运行两个 SQL 语句:

SET @rowIndex := 0;

WITH s1 as (
  SELECT...

Most MySQL connectors do not support multi-query by default.大多数 MySQL 连接器默认不支持多查询。 In other words, you can only do one statement per call.换句话说,每次调用只能执行一个语句。 As soon as MySQL sees any syntax following your first ;一旦 MySQL 在您的第一个之后看到任何语法; it treats this as a syntax error.它将此视为语法错误。

There's no reason you need to use multi-query.您没有理由需要使用多查询。 Just run the two statements separately.只需分别运行这两个语句。 As long as you use the same session, your value of @rowIndex will be available to subsequent statements.只要您使用同一个会话, @rowIndex的值就可用于后续语句。

The former Director of Engineering for MySQL once told me, "there's no reason multi-query should exist." MySQL 的前工程总监曾经告诉我,“没有理由应该存在多查询。”

  • Not sure what problem you might be facing, yet this would pass on LeetCode:不确定您可能面临什么问题,但这会传递给 LeetCode:

  • Not sure if that's a right way to do it:不确定这是否是正确的方法:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM