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

11,790

Solution 1

SELECT min(ID) as ID, min(Name), `group`, Category
FROM movie
GROUP BY `group`, Category
ORDER BY ID

Solution 2

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;
Share:
11,790
user2511667
Author by

user2511667

Updated on June 04, 2022

Comments

  • user2511667
    user2511667 almost 2 years

    I use mysql. My table look like this:

    my table

    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 enter image description here

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

    enter image description here

    IF I use this QUERY

    SELECT * FROM movie GROUP BY `group`
    

    It give me this result

    enter image description here

    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)

  • user2511667
    user2511667 over 9 years
    Thanks for your help, but this is not my answer,, Please check my first image with arrows, and i want 2nd image result,, now please tell me, what query you use to get 2nd image result,.. AND Best if you use GROUP by group with your query...
  • Gordon Linoff
    Gordon Linoff over 9 years
    @user2511667 . . . I did and have again looked at your image and that is what this query should return.
  • user2511667
    user2511667 over 9 years
    with your query ID 9 not showing,, i change your query with where clouse but still not working,,,, You query result same as LAST image
  • user2511667
    user2511667 over 9 years
    Thank you soo much,, I change little bit in your query and i get my result... Thanks, :* what is change in query see my ANSWER
  • user2511667
    user2511667 over 9 years
    NO, sorry its not working,, i check properly,,if i use where clause and remove ON m.id = g.minid all repeat result show,,if i use then ID 9 not showing,,
  • Gordon Linoff
    Gordon Linoff over 9 years
    @user2511667 . . . As a note: I don't believe this really works in general. This returns the minimum id and the minimum name -- but not the name for the minimum id. You seem to want the row with the minimum id (which is what my answer does), not two separate minimums.
  • Gordon Linoff
    Gordon Linoff over 9 years
    @user2511667 . . . If you want both group and category for the "groups", then they both need to be included in the group by clause.
  • user2511667
    user2511667 over 9 years
    @Gordon Linoff You are right, but i dont want minimum value, my result is, SELECT * FROM movie GROUP BY group, Category
  • mickmackusa
    mickmackusa over 5 years
    Please add some explanation to this answer.