ionfish
ionfish

Reputation: 1

making conditional inside mysql query

'SELECT conversation_id, viewed_on,('max_unixtime().' - last_reply) AS newest_conversation FROM `Conversation_Participant` WHERE `user_id`='.$self->{user}->get('id').' ORDER BY newest_conversation DESC'

I need to set newest_conversation only when max_unixtime() - last_reply > last_viewed..

how can i set the if statement within my query?

Upvotes: 0

Views: 83

Answers (1)

Joe Stefanelli
Joe Stefanelli

Reputation: 135928

Use a CASE statement.

...CASE WHEN max_unixtime() - last_reply > last_viewed 
        THEN ('max_unixtime().' - last_reply) 
        ELSE NULL END AS newest_conversation...

Upvotes: 2

Related Questions