MySQL query using CASE to SELECT multiple columns

后端 未结 1 1944
灰色年华
灰色年华 2021-01-23 21:10

I have the following query which works correctly:

SELECT Future.enemy_type,
CASE WHEN Future.enemy_type = \'square\' THEN Users.color
ELSE \'\'
END AS color,
CAS         


        
相关标签:
1条回答
  • 2021-01-23 21:56

    I like the IF() function for simple statements:

    SELECT
        Future.enemy_type,
        IF (Future.enemy_type = 'square', Users.color, '') AS color,
        IF (Future.enemy_type = 'square', Users.user_ID, '') AS ID,
        IF (Future.enemy_type = 'headquarters', Users.username, '') AS username,
        IF (Future.enemy_type = 'headquarters', Users.home_lat, '') AS lat
    FROM Future
    LEFT JOIN Users
        ON Future.user_ID_affected = Users.user_ID
    WHERE
        Future.time > 1539503510  AND
        Future.time <= 1539503512 AND
        Future.user_ID = 10;
    
    0 讨论(0)
提交回复
热议问题