gpt4 book ai didi

postgresql - 选择案例 : more than one row returned by a subquery used as an expression

转载 作者:行者123 更新时间:2023-11-29 12:11:01 28 4
gpt4 key购买 nike

我的问题得到了很好的帮助 PostgreSQL: return message after count = 0

这个查询工作正常:

SELECT  CASE WHEN COUNT(*)  = 0 THEN 'NO RESULT'
ELSE CAST(COUNT(*) as TEXT)
END as myfield from mytable

但是今天我需要如果表中有一些行,我需要显示选择结果。

我试过:

SELECT  CASE WHEN COUNT(*)  = 0 THEN 'NO RESULT'
ELSE (select name from mytable)
END as myfield from mytable

我得到了错误:

ERROR: more than one row returned by a subquery used as an expression

我发现了 IN 功能,但不知道如何改进它。

最佳答案

我认为你可以这样做:

select name from mytable
UNION ALL
SELECT 'NO RESULT'
WHERE NOT EXISTS(SELECT NULL FROM mytable)

关于postgresql - 选择案例 : more than one row returned by a subquery used as an expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34784219/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com