我有一个查询,其中包含3个查询。 根据一个查询的结果,我想运行其他两个查询中的任何一个。
SELECT
CASE type
WHEN 'H'
THEN (select count(*) from tableN where hid = 228)
when 'S'
THEN ( select cid,cname from tableC where cid = (select hid from tableN where sid = 228) )
END
FROM tableC WHERE cid=228;
但是,因为其中一个查询只返回1列,即count(*),而另一个查询返回2列,即cid和cname ,我得到以下内容错误:
操作数应包含1列
如何修改我的查询以便-----
当type为'H'时,运行以下查询:
select count(*) from tableN where hid = 228
当type为'S'时,运行以下查询:
select cid,cname from tableC where cid = (select hid from tableN where sid = 228)
答案 0 :(得分:0)
在解决这个问题之前,你需要考虑一些事情。
例如:
不正确:select col1, (select col2, col3 from...) , col 4 from table 1
Coreect:select col1, (select col2 from..), (select col3 from..), col4 from table 1
考虑到这两个限制,您的问题的解决方案可能是
SELECT col1, col2 FROM
(
SELECT COUNT(*) AS col1, null AS col2, 'H' AS colType from tableN
UNION ALL
SELECT CID AS col1, CNAME AS col2, 'S' AS colType FROM tableC where .....
)
where colType = :input_type
现在,根据input_type
的值,选择查询1的结果或选择查询2。
注意: