简体   繁体   English

多对多查询,MySQL

[英]Many-to-many query, MySQL

I have 3 tables like this: 我有3个这样的表:

movies: 电影:

  • movie_id (int) movie_id(int)
  • movie_title (varchar) movie_title(varchar)
  • movie_runtime (int) movie_runtime(int)
  • movie_releaseYear (int) movie_releaseYear(int)
  • ... ...

genres: 类型:

  • genre_id (int) genre_id(int)
  • genre_name (varchar) genre_name(varchar)

movieGenres: movieGenres:

  • movieGenre_id (int) movieGenre_id(int)
  • movieGenre_movieId (int) movieGenre_movieId(int)
  • movieGenre_genreId (int) movieGenre_genreId(int)

A movie can include many genres. 电影可以包括许多类型。
I want to grab the movie title, runtime and release year together with its genres. 我想抓住电影片名,运行时和发行年及其类型。 How can I do that in a single query? 我怎么能在一个查询中做到这一点?

I tried to do it like this: 我试着这样做:

SELECT u.movie_title, u.movie_runtime, u.movie_releaseYear, a.genre_name 
FROM movieGenres ua 
LEFT JOIN movies u ON u.movie_id = ua.movieGenre_filmId 
LEFT JOIN genres a ON a.genre_id = ua.movieGenre_genreId
LIMIT 0,10

This return something like: 这返回类似于:

Robin Hood _ 101 _ 2001 _ Adventure 罗宾汉_ 101 _ 2001 _冒险
Robin Hood _ 101 _ 2001 _ Action 罗宾汉_ 101 _ 2001 _行动
Robin Hood _ 101 _ 2001 _ Drama 罗宾汉_ 101 _ 2001 _戏剧
Firewall _ 98 _ 2003 _ Action 防火墙_ 98 _ 2003 _行动
Firewall _ 98 _ 2003 _ Drama 防火墙_ 98 _ 2003 _戏剧

I don't want to repeat the information I already know. 我不想重复我已经知道的信息。 I want it to display like this: 我希望它显示如下:

Robin Hood _ 101 _ 2001 _ Adventure _ Action _ Drama 罗宾汉_ 101 _ 2001 _冒险_动作_戏剧
Firewall _ 98 _ 2003 _ Action _ Drama 防火墙_ 98 _ 2003 _动作_戏剧

Please help me figure this out:) 请帮我搞清楚:)

I had this problem a couple of weeks ago, and my professor gave me a pretty clever solution: 几周前我遇到了这个问题,我的教授给了我一个非常聪明的解决方案:

Use the Group_Concat function in a sub query to get the results as comma separated values, then use explode/split/whatever language equivalent to loop through the CSV results. 在子查询中使用Group_Concat函数以逗号分隔值的形式获取结果,然后使用explode / split /等效的语言来循环CSV结果。

http://mahmudahsan.wordpress.com/2008/08/27/mysql-the-group_concat-function/ http://mahmudahsan.wordpress.com/2008/08/27/mysql-the-group_concat-function/

Hope this helps! 希望这可以帮助! This problem had me frustrated for awhile, but I think this is the simplest solution. 这个问题让我感到沮丧一段时间,但我认为这是最简单的解决方案。

SELECT
    u.movie_title,
    u.movie_runtime,
    u.movie_releaseYear,
    GROUP_CONCAT(a.genre_name) genre
FROM
    movieGenres ua  
    INNER JOIN movies u ON u.movie_id = ua.movieGenre_filmId  
    INNER JOIN genres a ON a.genre_id = ua.movieGenre_genreId
GROUP BY
    u.movie_title,
    u.movie_runtime,
    u.movie_releaseYear
LIMIT 0,10 
;

This is essentially your query. 这基本上是您的查询。 I just added the GROUP BY clause and GROUP_CONCAT function. 我刚刚添加了GROUP BY子句和GROUP_CONCAT函数。 The default delimiter is a comma(,). 默认分隔符是逗号(,)。 If you want the list of genres space-separated, do this with the GROUP_CONCAT function: 如果您希望以空格分隔的流派列表,请使用GROUP_CONCAT函数执行此操作:

SELECT
    u.movie_title,
    u.movie_runtime,
    u.movie_releaseYear,
    GROUP_CONCAT(a.genre_name SEPARATOR ' ') genre
FROM
    movieGenres ua  
    INNER JOIN movies u ON u.movie_id = ua.movieGenre_filmId  
    INNER JOIN genres a ON a.genre_id = ua.movieGenre_genreId
GROUP BY
    u.movie_title,
    u.movie_runtime,
    u.movie_releaseYear
LIMIT 0,10
;

This is the way databases work. 这是数据库工作的方式。 There 'are' ways to do what you're wanting but honestly the database is usually not the right place to accomplish it. 有'方法'可以做你想要的,但老实说,数据库通常不是完成它的正确位置。 Is this purely just an ad-hoc query or are these results going to an application? 这纯粹只是一个临时查询还是将这些结果发送到应用程序? The application would be the right place to format the results the way you want them if so. 如果是这样,应用程序将是按照您希望的方式格式化结果的正确位置。

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

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