Jump to content

[SOLVED] Controlling how GROUP BY groups columns


Recommended Posts

First off, please forgive me for not being able to find what I need on Google. I know I'm not the first person to ask this, I just can't find the answer....so

 

I have an sql statement like so:

"SELECT id, tid, asset FROM assets

WHERE id IN(1,2,3)

AND tid IN ($tid,0)

GROUP BY id";

 

The problem is that my tid gets grouped in a way I don't want. Pretty much I want to be able to run a query where fetches all assets with a tid of "x" or if it doesn't exist use the default of "0". My sql groups all items into tid's of 0 and not favoring the $tid.

 

Any idea what I need?

Thank you.

If you are saying that there will be a row with tid of "x" and another row with tid of "0" for the same id, and you don't want the row with tid = 0 unless the row with tid = x does not exist, then I think I would try something like this:

 

SELECT id, tid, asset FROM assets a1
WHERE id IN(1,2,3)
AND ( tid = $tid 
    OR (tid = 0
        AND NOT EXISTS (SELECT * FROM assets a2 WHERE a2.id = a1.id and a2.tid = $tid)))
GROUP BY id

 

Although that looks ugly, so there must be a better way.

Thanks for the responses, guys. here's a table

 

table: assets
assetid | tid | name | asset
------------------------------
1         |  0  | image | foobar.jpg
2         |  0  | pdf    | default.pdf
1         |  1  | image | specific.jpg
-------------------------------

 

The result of my query would be to return assetid 1 tid 1 if possible. If not, return assetid 1 tid0 as the default content.

 

Make sense? Perhaps it's a bad database structure. If that's the case, please suggest a structure and perhaps give a reason as well....so I can learn and not make the same mistake again.

 

Thanks for the attention to this. Your help and time is greatly appreciated! :)

It would help if you explain what you are trying to do.

but maybe you could just order by ?

 

"SELECT id, tid, asset FROM assets

WHERE id IN(1,2,3)

AND tid IN ($tid,0)

ORDER BY id ASC, tid DESC";

 

and then check with php if the first tid value of a newid is 0 or 1

 

hmmm...i think all of you guys are right. Perhaps I'm really just should be doing two queries or one query/sub-query. And yeah, if I'm grouping the other columns are meaningless...perhaps I should rethink a couple of things.

 

But let me get a final answer to my question: If I use GROUP BY, can I control how other column values get flatted. For instance:

<i>"SELECT assetid, asset FROM assets

WHERE assetid = 1

GROUP BY id";</i>

Can I control how the asset column will be flatted to make it favor specific.jpg over foobar.jpg or because I'm doing a GROUP BY, the asset colum should be irrelative?

 

Thanks again guys! :)

Hi

 

Looks to me that you want the max tid:-

 

SELECT id, tid, asset

FROM assets a

INNER JOIN (SELECT id, MAX(tid) AS MaxTid FROM assets WHERE tid IN ($tid,0) GROUP BY id) b

ON a.id = b.id

AND a.tid = b.MaxTid

WHERE a.id IN(1,2,3)

 

All the best

 

Keith

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.