长SQL查询失败

时间:2019-01-24 19:12:53

标签: mysql sql

我从另一个论坛检索的查询失败。在Sequel PRO中运行它时,出现以下错误消息:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AS bar
)
SELECT
    q.id
FROM
    questions q
INNER JOIN
    concat_data cd
ON
 ' at line 27

这是我要运行的查询:

WITH raw_data AS (
    SELECT
        q.id AS id
      , q.description AS description
      , q.source AS source
      , q.image AS image
      , GROUP_CONCAT(c.choice ORDER BY c.choice) AS foo
    FROM
        questions q
    INNER JOIN
        choices c
    ON
        c.question_id = q.id
    GROUP BY
        q.id
      , q.description
      , q.source
      , q.image
),
concat_data AS (
    SELECT
        MIN(id) AS min_id
      , CONCAT(description, source, image, foo) AS bar
    FROM
        raw_data
    GROUP BY
        CONCAT(description, source, image, foo) AS bar
)
SELECT
    q.id
FROM
    questions q
INNER JOIN
    concat_data cd
ON
    cd.min_id = q.id;

为了它。我试图从具有透视表questions的{​​{1}}表中查找重复项。删除重复项并保留“原始”。

1 个答案:

答案 0 :(得分:2)

虽然在选择部分中需要“ as bar”(以使该串联成为别名“ bar”),但在GROUP BY子句中不需要“ AS bar”。

GROUP BY
    CONCAT(description, source, image, foo)

应该做。