Jump to content

group only certain columns together


newb

Recommended Posts

ok i have a table thats setup like this:

 

id | category | name | varset

1 | catname1 | item | 0

2 | catname2 | item | 1

3 | catname2 | item | 1

4 | catname2 | item | 1

5 | catname3 | item | 0

6 | catname3 | item | 0

7 | catname4 | item | 1

8 | catname4 | item | 1

9 | catname4 | item | 1

 

how can i group everything together where its varset is = 1 so its something like this:

 

id | category | name | varset

1 | catname1 | item | 0

2 | catname2 | item | 1

5 | catname3 | item | 0

6 | catname3 | item | 0

7 | catname4 | item | 1

 

is this possible? how can i do this in mysql /php

 

any help appreciated, thanks

Link to comment
https://forums.phpfreaks.com/topic/207547-group-only-certain-columns-together/
Share on other sites

To do literally what you show in your pseudo example -

$query = "(SELECT * FROM your_table WHERE varset = 1 GROUP BY category) UNION (SELECT * FROM your_table WHERE varset != 1) ORDER BY id";

 

However I have my doubts that is really what you want in all cases with the real data, in which case you will need to use a mysql user variable to keep track of when the varset value changes in order to form groups.

You do realize that the answers provided in a forum cannot be any better than the question that was asked and has already been pointed out your question and example is lacking in specifics.

 

Also, if you don't state what "doesn't work either" in relation to your question and expected result means, no one can help you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.