开发者

How to Display result of subquery rows as one column in MySQL?

开发者 https://www.devze.com 2022-12-24 08:37 出处:网络
I have three tables Category, Movies and RelCatMov Category-table categoryid, categoryName 1thriller 2supsense

I have three tables Category, Movies and RelCatMov

Category-table

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

Movies-table

movieid, movieName
1            Avataar
2            Titanic
3            NinjaAssassin

RelCatMov-table

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

Now i Want to display a the record as

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

How to do this.

I am writing a query

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!!!

How to display the result of rows in one column?

P开发者_如何学运维lease help!!!


In Oracle it's called stragg. In MySQL it's 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;

For reference, your problem is that MySQL wants you to return a single value and you're returning several rows instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消