MariaDB:合并中的子选择抛出错误 1064 (42000)

MariaDB: subselect in COALESCE throws ERROR 1064 (42000)

意图:我想在 MariaDB (10.7.1) 的 coalesce 函数中子选择值。

问题: 执行此 SQL 语句 ...

select coalesce(select id from MY_TABLE limit 1);

... 抛出此错误消息:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'select id from MY_TABLE limit 1)' at line 1

我尝试过的解决方法/检查

COALESCE 函数通常需要 2 个或更多参数。返回的值是第一个值,即 而不是 NULL。您可能打算这样:

SELECT COALESCE(id, 'missing') AS id
FROM MY_TABLE
ORDER BY <some column>
LIMIT 1;

您必须使 select 的结果成为表达式。语句不是表达式

with MY_TABLE as (
      select 1 as id
     )
select coalesce((select id from MY_TABLE limit 1)) c;

注意没有 ORDER BY 的 LIMIT 的结果是任意行。