简体   繁体   中英

show all data only group by specific rows : Select * from table group by column having column = 'value'

I use mysql. My table look like this:

我的桌子

Last I try to use this query

SELECT * FROM movie GROUP BY `group`  HAVING cateogry = 'TV'

I want with this query result as: show all but only GROUP BY TV category, where category = 'TV'

I want this Result 在此输入图像描述

But my query give me this result (HAVING in query work as WHERE clouse)

在此输入图像描述

IF I use this QUERY

SELECT * FROM movie GROUP BY `group`

It give me this result

在此输入图像描述

I want -> QUERY -> GROUP BY group (ID no 9 and ID 1,2,3 treat as different group name)

IF group has all same values BUT category ='movie' (RETURN ALL ROWS group by NOT APPLY)

IF group has all same values BUT category ='TV' (RETURN 1 ROW group by APPLY)

You seem to want this query:

select m.*
from movie m join
     (select `group`, min(id) as minid
      from movie
      group by `group`
     ) g
     on m.id = g.minid;
SELECT min(ID) as ID, min(Name), `group`, Category
FROM movie
GROUP BY `group`, Category
ORDER BY ID

Have you tried the below? I think you are pretty close. As when you are grouping your 'group' t. You are also grouping the one whose category is movie as well. So you just need to create a separate group Category.

SELECT * FROM movie
WHERE group = 't'
GROUP BY group, Category
ORDER BY ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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