MySQL: Assign column value to variable

Making a messaging system and working on the inbox page.

The inbox page is organized in a 'thread' like manner and I would like to show the latest communication between 2 parties. Think the facebook inbox (the left hand part) where it shows the latest message sent whether you sent the message or the other party did.

This is my current query:

SELECT 
    m.*, 
    s.*, 
    c.*, 
    u.*,
    CASE    
     WHEN m.uid_recipient <> 292 THEN @user_id := m.uid_recipient
     WHEN m.uid_sender <> 292 THEN @user_id := m.uid_sender
    END 
FROM messages m
JOIN signup s ON s.uid = @user_id
LEFT JOIN companies c ON c.uid = s.uid
LEFT JOIN users u ON u.uid = s.uid
WHERE COALESCE(u.uid, c.uid) IS NOT NULL
    AND (m.uid_recipient = 292 AND deleted_recipient = '0')
    OR (m.uid_sender = 292 AND deleted_sender = '0')
ORDER BY datetime DESC

This query runs but returns an empty result set where it should return 3 results.

The messages are in a table with the sender's id and the recipient's id. Now what I'm trying to do is join the information of the other party to the rows. So my case statement evaluates if the recipient id is the current user's id, and if it isn't, it should set the @user_id variable to the other party's id so it can then be used to join the other party's information to that row.

The message table looks like so (relevant columns):

----------------------------------------------
|uid_sender    |uid_recipient    |message    |
----------------------------------------------
|292           |1040             |Test 3     |
----------------------------------------------
|1040          |292              |Test reply |
----------------------------------------------
|292           |1040             |First msg  |
----------------------------------------------

I'm inclined to think that the value of column is not assigned to the variable but I do not know how I could debug this query to return the value assigned to @user_id. Is there anyway to dump the value in a mysql query?

Any help is appreciated.

Thank you.

Asked By: ReX357
||

Answer #1:

This expression:

CASE    
 WHEN m.uid_recipient <> 292 THEN @user_id := m.uid_recipient
 WHEN m.uid_sender <> 292 THEN @user_id := m.uid_sender
END 

is, I believe, assiging values to the @user_id variable. There is no rule on when this is evaluated relative to other expressions in the query. I would make an educated guess that expressions in the SELECT clause are always evaluated after joins and group bys. In that case, the value used in the from clause is always the initial value.

I think you should just move it to the from clause:

FROM messages m join
     signup s
     ON s.uid = (CASE WHEN m.uid_recipient <> 292 THEN m.uid_recipient
                      WHEN m.uid_sender <> 292 THEN  m.uid_sender
                 END)

There are probably other ways to express this, but this is a start.

Answered By: Gordon Linoff
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .



# More Articles