多对多查询,MySQL

发布于 2024-12-15 16:06:18 字数 1068 浏览 3 评论 0原文

我有3个这样的表:

movies:

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

genres:

  • genre_id (int)
  • genre_name ( varchar)

movieGenres:

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

一部影片可以包含多个流派。
我想获取电影名称、运行时间、发行年份及其类型。我怎样才能在单个查询中做到这一点?

我尝试这样做:

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

这返回类似:

Robin Hood _ 101 _ 2001 _ Adventure
罗宾汉 _ 101 _ 2001 _ 行动
罗宾汉 _ 101 _ 2001 _ 戏剧
防火墙 _ 98 _ 2003 _ 行动
防火墙_98_2003_戏剧

我不想重复我已经知道的信息。我希望它像这样显示:

罗宾汉 _ 101 _ 2001 _ 冒险 _ 动作 _ 戏剧
防火墙 _ 98 _ 2003 _ 动作 _ 戏剧

请帮我解决这个问题:)

I have 3 tables like this:

movies:

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

genres:

  • genre_id (int)
  • genre_name (varchar)

movieGenres:

  • movieGenre_id (int)
  • movieGenre_movieId (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
Robin Hood _ 101 _ 2001 _ Action
Robin Hood _ 101 _ 2001 _ Drama
Firewall _ 98 _ 2003 _ Action
Firewall _ 98 _ 2003 _ Drama

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
Firewall _ 98 _ 2003 _ Action _ Drama

Please help me figure this out:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗喜 2024-12-22 16:06:18

几周前我遇到了这个问题,我的教授给了我一个非常聪明的解决方案:

在子查询中使用 Group_Concat 函数以逗号分隔值的形式获取结果,然后使用爆炸/拆分/任何相当于循环的语言CSV 结果。

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

希望这有帮助!这个问题让我沮丧了一段时间,但我认为这是最简单的解决方案。

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.

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.

番薯 2024-12-22 16:06:18
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 
;

这本质上是您的查询。我刚刚添加了 GROUP BY 子句和 GROUP_CONCAT 函数。默认分隔符是逗号 (,)。如果您想要以空格分隔流派列表,请使用 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
;
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. The default delimiter is a comma(,). If you want the list of genres space-separated, do this with the GROUP_CONCAT function:

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
;
孤独岁月 2024-12-22 16:06:18

这就是数据库的工作方式。有“多种”方法可以完成您想要的事情,但老实说,数据库通常不是完成它的正确位置。这纯粹是一个临时查询还是这些结果会发送到应用程序?如果是这样,应用程序将是按照您想要的方式格式化结果的正确位置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文