SQLite group_concat returns 相同数据的多个实例,不能使用 DISTINCT

SQLite group_concat returns many instances of the same data, can't use DISTINCT

我正在尝试使用 JOIN 和 group_concat

从 4 个表中获取数据 return

这是我当前的查询:

SELECT  m.ID AS num
    , m.Name AS MOVIE
    , group_concat(v.Codec, '-') AS v_CODEC
    , group_concat(a.Codec, '-') AS a_CODEC
    , group_concat(s.Language, '-') AS SUBs
FROM File AS m
LEFT JOIN Video AS v ON m.ID = v.FileID
LEFT JOIN Audio AS a ON m.ID = a.FileID
LEFT JOIN Subtitle AS s ON m.ID = s.FileID
GROUP BY m.ID
ORDER BY m.ID

对于我当前的查询,我的结果如下所示:

+-----+----------+----------------+-------------+----------+
| num |   MOVIE  |     v_CODEC    |   a_CODEC   |   SUBs   |
+-----+----------+----------------+-------------+----------+
| 1   | Superman | H264-H264-H264 | DTS-DTS-DTS | en-en-en |
|     |          | H264-H264-H264 | DTS-DTS-DTS | en-en-en |
+-----+----------+----------------+-------------+----------+
| 2   | Batman   | MPEG2-MPEG2    | MP3-MP3     |          |
+-----+----------+----------------+-------------+----------+

我希望我的结果是这样的:

+-----+----------+---------+-------------+-------+
| num |   MOVIE  | v_CODEC |   a_CODEC   |  SUBs |
+-----+----------+---------+-------------+-------+
| 1   | Superman | H264    | DTS-DTS-DTS | en-en |
+-----+----------+---------+-------------+-------+
| 2   | Batman   | MPEG2   | MP3-MP3     |       |
+-----+----------+---------+-------------+-------+

这是我的数据库结构:

File
+----+----------+
| ID |   Name   |
+----+----------+
| 1  | Superman |
+----+----------+
| 2  | Batman   |
+----+----------+

Video
+--------+-------+
| FileID | Codec |
+--------+-------+
| 1      | H264  |
+--------+-------+
| 2      | MPEG2 |
+--------+-------+

Audio
+--------+-------+
| FileID | Codec |
+--------+-------+
| 1      | DTS   |
+--------+-------+
| 1      | DTS   |
+--------+-------+
| 1      | DTS   |
+--------+-------+
| 2      | MP3   |
+--------+-------+
| 2      | MP3   |
+--------+-------+

Subtitle
+--------+----------+
| FileID | Language |
+--------+----------+
| 1      | en       |
+--------+----------+
| 1      | en       |
+--------+----------+

如何使我的查询达到我的需要?

提前感谢您的帮助。

我相信以下是您想要的:-

SELECT 
    m.ID AS num, 
    m.name AS movie, 
    (SELECT group_concat(codec,'-') FROM Video WHERE FileID = m.ID) AS v_codec, 
    (SELECT group_concat(codec,'-') FROM Audio WHERE FileID = m.ID) AS a_codec,
    (SELECT group_concat(Language,'-') FROM Subtitle WHERE FileID = m.ID) AS SUBs
FROM File AS m ORDER BY m.id;

产生:-

这不是基于笛卡尔积的提取,如果不分组,它会是这样的:-

答案group_concat只根据关系