sql >> Base de Datos >  >> RDS >> Mysql

Devuelve un valor predeterminado si no se encuentra una sola fila

Una forma de hacerlo

SELECT IFNULL(MIN(`file`), 'default.webm') `file` 
  FROM `show`, `schedule` 
 WHERE `channel` = 1 AND `start_time` <= UNIX_TIMESTAMP() 
   AND `start_time` > UNIX_TIMESTAMP()-1800 AND `show`.`id` = `schedule`.`file` 
 ORDER BY `start_time` DESC LIMIT 1

Dado que solo devuelve una fila, puede usar una función agregada, en ese caso MIN() , eso asegura que obtendrás NULL si no hay registros seleccionados. Entonces IFNULL() o COALESCE() hará su trabajo.