gpt4 book ai didi

mysql - 如何在 MySQL 中将子查询行的结果显示为一列?

转载 作者:可可西里 更新时间:2023-11-01 06:36:06 25 4
gpt4 key购买 nike

我有三个表 Category、Movies 和 RelCatMov

分类表

    categoryid, categoryName
1 thriller
2 supsense
3 romantic
4 action
5 sci-fi

电影表

movieid, movieName
1 Avataar
2 Titanic
3 NinjaAssassin

RelCatMov 表

categoryid, MovieID
1 1
2 2
3 2
4 2
5 2

现在我想将记录显示为

MovieName     Categories
Titanic Suspense,Romantic,Sci-fi,action

如何做到这一点。

我正在写一个查询

select MovieName,(select categoryname from category b,relcatmov c where b.categoryid=c.categoryid and c.movieid=a.movieid) as categories from movies a;

Error: Subquery returns more than one row!!!

如何在一列中显示行的结果?

请帮忙!!!

最佳答案

在 Oracle 中它被称为 stragg。在 MySQL 中它是 GROUP_CONCAT。

select MovieName,(select GROUP_CONCAT(categoryname) from category b,relcatmov c where b.categoryid=c.categoryid and c.movieid=a.movieid) as categories from movies a;

作为引用,您的问题是 MySQL 希望您返回单个值,而您返回的是多行。

关于mysql - 如何在 MySQL 中将子查询行的结果显示为一列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2499250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com