如果没有行,如何 return 取值?

How to return value if there is no row?

我通过 Qml + JS 从 table 读取数据,我需要 100 return 是查询未找到数据:

SELECT (CASE
        WHEN pourcenthour < pourcentcycle AND pourcenthour  < pourcentmonth THEN pourcenthour
        WHEN pourcentcycle < pourcenthour AND pourcentcycle < pourcentmonth THEN pourcentcycle
        WHEN pourcentmonth < pourcenthour AND pourcentmonth < pourcentcycle THEN pourcentmonth
        ELSE 100
        END) AS pourcent,
       ata,
       immatriculation
FROM part_log
WHERE removed = "false" AND immatriculation =? AND ata=?
ORDER BY pourcent DESC LIMIT 1

我试试这个:

SELECT IFNULL((CASE WHEN ... ELSE 100 END),100) AS pourcent, ...

我收到错误:

Cannot read property 'pourcent' of undefined

我试试这个:

SELECT IFNULL(SELECT (CASE WHEN ... ELSE 100 END) ,100) AS pourcent

我收到这个错误:

Error: near "SELECT": syntax error Impossible d'exécuter l'instruction

你在 SELECT 子句中写的内容每行计算一次。这意味着如果没有行,SELECT 子句中的任何内容都无法帮助您。

您可以使用 compound query 添加另一行。最后的 LIMIT 确保只有 part_log 行(如果找到)被返回:

SELECT *
FROM (SELECT CASE ... END AS pourcent, ata, immatriculation
      FROM part_log
      WHERE removed = "false" AND immatriculation =? AND ata=?
      ORDER BY pourcent DESC LIMIT 1)
UNION ALL
SELECT 100, NULL, NULL
LIMIT 1;