SELECT CASE:用作表达式的子查询返回多行
SELECT CASE: more than one row returned by a subquery used as an expression
我的问题得到了很好的帮助
这个查询工作正常:
SELECT CASE WHEN COUNT(*) = 0 THEN 'NO RESULT'
ELSE CAST(COUNT(*) as TEXT)
END as myfield from mytable
但是今天我需要如果某些行在 table 中,我需要显示 select 结果。
我试过了:
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)
我的问题得到了很好的帮助
这个查询工作正常:
SELECT CASE WHEN COUNT(*) = 0 THEN 'NO RESULT'
ELSE CAST(COUNT(*) as TEXT)
END as myfield from mytable
但是今天我需要如果某些行在 table 中,我需要显示 select 结果。
我试过了:
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)