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
Share on other sites

Can you clarify that a little? The diagram seems unrelated to what you asked . . .

 

what do u need clarified? i pretty much drew out exactly what im trying to achieve in the diagram. im just not sure as to how it should be approached through mysql.

Link to comment
Share on other sites

the diagrams arent that confusing..atleast not to me.. looks pretty straightforward as to what the table looks like atm and what im trying to get it to look like.

 

meh..looks like i just have to figure this out myself then >_<

Link to comment
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.