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

时间:2017-07-05 10:23:05

标签: mysql sql sql-order-by aggregate-functions distinct-values

这是我的SQL Fiddle

正如您在此处所见,如果我使用DISTINCT,则有2个问题

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

2。)只打印2个号码。

如果我不使用DISTINCT,所有数字都是7.5(即first 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小提琴代码:

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

2 个答案:

答案 0 :(得分:2)

很难从你的问题中猜出你想要什么。你没有定义它就提到了“正确的顺序”。

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添加到最后一个应用程序的含义。

如果您尝试按相应的顺序获取两个连接列,则不能在任何一个中使用DISTINCT; DISTINCT有可能删除重复值。

您的结果集列提到了_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表中的行没有固有顺序。如果您不止一次说SELECT * FROM table(没有ORDER BY子句),并且每次都以相同的顺序排列,这是一个意外。您的内容中没有任何内容推荐表 - 例如没有唯一的id值 - 给出除分数和标题之外的那些项的顺序。所以你可能无法得到你想要的确切订单。

许多表都包含一个自动增量id列(但不是你的)。在id子句中使用这样的ORDER BY列是一种获得可重复排序的方法。

专业提示:非规范化数据(例如,列中以逗号分隔的数据)通常被认为是有害的。 GROUP_CONCAT()将标准化数据(如输入)转换为非规范化数据。因此,只有在需要时才使用它。

答案 1 :(得分:1)

要获得请求的结果,我认为您需要做的就是删除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

更多信息。 以下查询会反转表优先级,并且不使用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');