Jump to content

Ordering a Union but preserving distinction


tissue901

Recommended Posts

Im having some trouble here with getting my data to be properly gathered. What im doing is making a messaging system. You don't make messages by subject, rather its just by people. So all my messages towards someone shows up under the same thing. I need to get this data from the table and then order it by the time it was posted. The issue is i have to make 2 selects and then i cant get the date to order properly.

 

$userid_1 = sender, $userid_2 is reciever

 

What i have now, issue is it doesnt sort it by the date:

 

SELECT userid_2

FROM messages

WHERE userid_1='$dbid'

UNION

SELECT userid_1

FROM messages

WHERE userid_2='$dbid'

 

adding order by date doesnt work because the date column isnt loaded. Problem with this is you cant just do

 

SELECT userid_2 ,date

FROM messages

WHERE userid_1='$dbid'

UNION

SELECT userid_1,date

FROM messages

WHERE userid_2='$dbid'

order by date desc

 

because then the select doesnt stay distinct. Another solution could be adding the distinct property, but id only need one column to be distinct and idk if thats possible. so like SELECT (DISTINCT userid_2) date but thats not allowed. Im so confused, and i bet you are too, but basically its taking a bunch of messages, sorting it by the user and then ordering it by the date.

Link to comment
Share on other sites

I'm trying to find everything with certain usernames that meet a certain condition. I can get it to load it all right but i cant get it to order by the date sent, or i can get it to load all the messages that someones sent a message back and order it by the time, but i cant get it if you send the first message and they don't reply back to it.

 

 

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.