Mysql
 sql >> Database >  >> RDS >> Mysql

Query MySQL utilizzando CASE per SELECT più colonne

Mi piace il IF() funzione per affermazioni semplici:

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;