在mysql查询中进行条件化

时间:2011-07-15 20:05:15

标签: mysql if-statement conditional

'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'

我需要在max_unixtime() - last_reply>时设置newest_conversation。 last_viewed ..

如何在查询中设置if语句?

1 个答案:

答案 0 :(得分:2)

使用CASE statement

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