문제

I have a messaging system (very basic) that has a table like this:

**MESSAGE_ID**  **RUSER_ID**    **SUSER_ID**    **MESSAGE_DATA**    **DATE**

RUSER is the receiving user, and SUSER is the sending user. If I wanted to output a query that would output a certain users messages, I would currently do:

Select * from PRIVATE_MESG where RUSER_ID=$USER_ID or SUSER_ID=$USER_ID

That would give me all message_id's that are associated with that USER_ID. What I would like, is to create a column that would produce only the ID associated with RUSER_ID or SUSER_ID associated with a specific user. I need it to choose the messages that RUSER_ID or SUSER_ID are equal to a USER_ID but only display the one that isn't USER_ID

I would then like to do a group by the output of that query.

Any help is greatly appreciated!

Thanks!

update I am not really looking for a message_id, I am just looking for a list of users who that person has written to or received from.

UPDATE Just so everyone knows, I recieved the answer to this question perfectly! I tweaked it later on so that it would also display them by date from newest to oldest. I did this by spliting the DATETIME into DATE and TIME USING the DATE() and TIME() Function. Here was my final query:

    SELECT
    IF(RUSER_ID = $USER, SUSER_ID, RUSER_ID) as THE_OTHER_GUY, DATE(DATE) as DAY, TIME(DATE) as TIME
    FROM PRIVATE_MESG
    WHERE RUSER_ID = $USER
OR SUSER_ID = $USER;
group by THE_OTHER_GUY ORDER BY DAY DESC, TIME DESC

Hope this helps the next person!

도움이 되었습니까?

해결책

You can query:

SELECT
    *,
    IF(RUSER_ID = $USER_ID, SUSER_ID, RUSER_ID) as THE_OTHER_GUY
FROM PRIVATE_MESG
WHERE RUSER_ID = $USER_ID
    OR SUSER_ID = $USER_ID;

다른 팁

SELECT SUSER_ID FROM PRIVATE_MESG WHERE RUSER_ID=$USER_ID
UNION
SELECT RUSER_ID FROM PRIVATE_MESG WHERE SUSER_ID=$USER_ID

It retrieves:
- the list of user IDs who sent messages to $USER_ID
- the list of user IDs who received messages from $USER_ID

And UNION groups the 2 lists in a single result set.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top