如何回显所有行(与 DISTINCT 相关)

How to echo all rows (related to DISTINCT)

这是我的SQL Fiddle

正如你在这里看到的,如果我使用 DISTINCT 那么,有 2 个问题

1.)只有第一个recommendations_vote_average coloum 的号码是正确的。所有其他号码顺序错误

2.) 只打印了 2 个数字。

如果我不使用 DISTINCT,所有数字都是 7.5(即第一个 vote_average)

如何以正确的顺序显示所有 (10) 个数字?

预期输出

movie_title       recommendations_vote_average                    recommendations_title
                                                                  The Dark Knight Rises,Batman Begins,Iron Man,The Lord of the Rings: The Return of the King,The Lord of the Rings: The The Fellowship of the Ring,The Lord of the Rings: The Two Towers,The Matrix,Inception,Iron Man 2,Captain America: The First Avenger
The Dark Knight   7.5,7.5,7.3,8.1,8,7.9,7.9,8,6.6,6.6

SQL Fiddle代码:

CREATE TABLE tmdb_movies (
  tmdb_id INTEGER NOT NULL PRIMARY KEY,
  movie_title TEXT NOT NULL
);

INSERT INTO tmdb_movies (tmdb_id, movie_title) VALUES
(1, 'The Dark Knight');


CREATE TABLE recommendations (
  recommendations_tmdb_id INTEGER NOT NULL,
  recommendations_title TEXT NOT NULL,
  recommendations_vote_average TEXT NOT NULL
);




INSERT INTO recommendations (recommendations_tmdb_id, recommendations_title, recommendations_vote_average) VALUES
(1, 'The Dark Knight Rises', '7.5'),
(1, 'Batman Begins', '7.5'),
(1, 'Iron Man', '7.3'),
(1, 'The Lord of the Rings: The Return of the King', '8.1'),
(1, 'The Lord of the Rings: The The Fellowship of the Ring', '8'),
(1, 'The Lord of the Rings: The Two Towers', '7.9'),
(1, 'The Matrix', '7.9'),
(1, 'Inception', '8'),
(1, 'Iron Man 2', '6.6'),
(1, 'Captain America: The First Avenger', '6.6');


SELECT tmdb_movies.movie_title
,GROUP_CONCAT(DISTINCT recommendations.recommendations_vote_average) as recommendations_vote_average
,GROUP_CONCAT(DISTINCT recommendations.recommendations_title) as recommendations_title
FROM tmdb_movies 

LEFT JOIN recommendations ON recommendations.recommendations_tmdb_id=tmdb_movies.tmdb_id

Where tmdb_movies.tmdb_id=1

GROUP BY tmdb_movies.movie_title

很难从你的问题中猜出你想要什么。你提到了 "correct order" 而没有定义它。

You can use GROUP_CONCAT() in these ways.

GROUP_CONCAT(a.b)  -- gets all the items in column b -- cardinality preserved

GROUP_CONCAT(DISTINCT a.b) -- distinct values in column b -- cardinality reduced

GROUP_CONCAT(a.b ORDER BY a.b) -- all items in b in order

GROUP_CONCAT(DISTINCT a.b ORDER BY a.b) -- distinct items in b in order

GROUP_CONCAT(a.b ORDER BY a.c) -- all items in b in the same order as c

我不完全确定将 DISTINCT 添加到最后一个对应用程序意味着什么。

如果您试图以相应的顺序获得两个串联的列,则不能在任何一个中使用 DISTINCTDISTINCT 具有删除重复值的潜力。

您的结果集列提到 _average。你得到一个实际平均值(算术平均值)AVG(value)。这给出了一个总和。

如果您想要一栏中的分数列表和另一栏中的相应标题列表,试试这个。

GROUP_CONCAT(
            recommendations.recommendations_vote_average
   ORDER BY recommendations.recommendations_title
 ) AS recommendations_vote_average,
GROUP_CONCAT(
            recommendations.recommendations_title
   ORDER BY recommendations.recommendations_title
 ) AS recommendations_title

按标题顺序显示两个串联列表。

您可能没有意识到这一点:DBMS table中的行没有固有顺序。如果你多次说 SELECT * FROM table(没有 ORDER BY 子句),并且每次都以相同的顺序出现行,这是一个意外。有您的推荐 table 中没有任何内容——例如没有唯一的 id 值——来给出除分数和标题之外的那些项目的顺序。所以你可能无法获得你想要的确切顺序。

许多 table 包含一个自动递增的 id 列(但你的没有)。在 ORDER BY 子句中使用这样的 id 列是获得重复 table 排序的一种方式。

专业提示:非规范化数据(例如,列中的逗号分隔数据)通常被认为是有害的。 GROUP_CONCAT() 将规范化数据(例如您的输入)转换为非规范化数据。所以请谨慎使用,仅在需要时使用。

要获得请求的结果,我认为您需要做的就是删除 DISTINCT,但我也建议引入 ORDER BY

SELECT
      tmdb_movies.movie_title
    , GROUP_CONCAT(r.recommendations_vote_average
                   ORDER BY r.recommendations_vote_average DESC 
                   SEPARATOR ', ' 
                  ) as recommendations
    , GROUP_CONCAT(r.recommendations_title
                   ORDER BY r.recommendations_vote_average DESC 
                   SEPARATOR ', ' 
                  ) as recommendations
FROM tmdb_movies 
LEFT JOIN recommendations r ON r.recommendations_tmdb_id=tmdb_movies.tmdb_id
Where tmdb_movies.tmdb_id=1
GROUP BY tmdb_movies.movie_title
;

              recommendations                |                                 recommendations                                 |
+----+-----------------+----------------------------------------------+---------------------------------------------------------------------------------+
|  1 | The Dark Knight | 8.1, 8, 8, 7.9, 7.9, 7.5, 7.5, 7.3, 6.6, 6.6 | The Lord of the Rings: The Return of the King, Inception,                       |
|    |                 |                                              | The Lord of the Rings: The The Fellowship of the Ring, The Matrix,              |
|    |                 |                                              | The Lord of the Rings: The Two Towers, Batman Begins,                           |
|    |                 |                                              | The Dark Knight Rises, Iron Man, Captain America: The First Avenger, Iron Man 2 |
+----+-----------------+----------------------------------------------+---------------------------------------------------------------------------------+

如果由我决定,我会将推荐分数与标题相结合(为演示添加手动换行符):

+----+-----------------+-------------------------------------------------------------------------
|    |   movie_title   |  recommendations                                                                                                                                                  |
+----+-----------------+-------------------------------------------------------------------------
|  1 | The Dark Knight | 8.1(The Lord of the Rings: The Return of the King); 
                       | 8(Inception); 8(The Lord of the Rings: The The Fellowship of the Ring); 
                       | 7.9(The Matrix); 7.9(The Lord of the Rings: The Two Towers); 
                       | 7.5(Batman Begins); 7.5(The Dark Knight Rises); 
                       | 7.3(Iron Man);  6.6(Captain America: The First Avenger);  6.6(Iron Man 2)

这是由此查询产生的:

SELECT
      tmdb_movies.movie_title
    , GROUP_CONCAT(DISTINCT concat(r.recommendations_vote_average,'(',r.recommendations_title,')') 
                   ORDER BY r.recommendations_vote_average DESC 
                   SEPARATOR '; ' 
                  ) as recommendations
FROM tmdb_movies 
LEFT JOIN recommendations r ON r.recommendations_tmdb_id=tmdb_movies.tmdb_id
Where tmdb_movies.tmdb_id=1
GROUP BY tmdb_movies.movie_title

更多信息。 以下查询反转 table 优先级并且不使用 group_concat

select
    m.movie_title, r.*
from recommendations r
left join tmdb_movies m  ON r.recommendations_tmdb_id=m.tmdb_id
;

结果:

+----+-----------------+-------------------------+-------------------------------------------------------+------------------------------+
|    |   movie_title   | recommendations_tmdb_id |                 recommendations_title                 | recommendations_vote_average |
+----+-----------------+-------------------------+-------------------------------------------------------+------------------------------+
|  1 | The Dark Knight |                       1 | The Dark Knight Rises                                 |                          7.5 |
|  2 | The Dark Knight |                       1 | Batman Begins                                         |                          7.5 |
|  3 | The Dark Knight |                       1 | Iron Man                                              |                          7.3 |
|  4 | The Dark Knight |                       1 | The Lord of the Rings: The Return of the King         |                          8.1 |
|  5 | The Dark Knight |                       1 | The Lord of the Rings: The The Fellowship of the Ring |                            8 |
|  6 | The Dark Knight |                       1 | The Lord of the Rings: The Two Towers                 |                          7.9 |
|  7 | The Dark Knight |                       1 | The Matrix                                            |                          7.9 |
|  8 | The Dark Knight |                       1 | Inception                                             |                            8 |
|  9 | The Dark Knight |                       1 | Iron Man 2                                            |                          6.6 |
| 10 | The Dark Knight |                       1 | Captain America: The First Avenger                    |                          6.6 |
+----+-----------------+-------------------------+-------------------------------------------------------+------------------------------+

样本数据(应该有问题):

CREATE TABLE tmdb_movies (
  tmdb_id INTEGER NOT NULL PRIMARY KEY,
  movie_title TEXT NOT NULL
);

INSERT INTO tmdb_movies (tmdb_id, movie_title) VALUES
(1, 'The Dark Knight');


CREATE TABLE recommendations (
  recommendations_tmdb_id INTEGER NOT NULL,
  recommendations_title TEXT NOT NULL,
  recommendations_vote_average TEXT NOT NULL
);




INSERT INTO recommendations (recommendations_tmdb_id, recommendations_title, recommendations_vote_average) VALUES
(1, 'The Dark Knight Rises', '7.5'),
(1, 'Batman Begins', '7.5'),
(1, 'Iron Man', '7.3'),
(1, 'The Lord of the Rings: The Return of the King', '8.1'),
(1, 'The Lord of the Rings: The The Fellowship of the Ring', '8'),
(1, 'The Lord of the Rings: The Two Towers', '7.9'),
(1, 'The Matrix', '7.9'),
(1, 'Inception', '8'),
(1, 'Iron Man 2', '6.6'),
(1, 'Captain America: The First Avenger', '6.6');