不能作:“子查询用作表达式时”
select id from BhB
where Bh in (select bh from bhb where glid=(select glid from bhb where bh='10838318'))
这句是可以执行的,当放在case when中,
select id from BhB where Bh in (
case when(select glid from bhb where bh='10838318')=0
then (select bh from bhb where bh='10838318')
else (select bh from bhb where glid=(select glid from bhb where bh='10838318'))
end)
就出现错误,“子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”
是不能作:“子查询用作表达式时”
该怎么解决?
请高手帮助
[解决办法]
分支呗
if
elseif
else
[解决办法]
子查询里面有大于1条的数据,要是取得的bh有不同的几条数据,就会报错;
如果是重复的,可以用一下方法改正:
select id from BhB where Bh in (
case when(select distinct glid from bhb where bh='10838318')=0
then (select distinct bh from bhb where bh='10838318')
else (select distinct bh from bhb where glid=(select distinct glid from bhb where bh='10838318'))
end)
--建议改用exists
select id from BhB t1
where exists (select bh from bhb t2 where t2.bh=t1.bh
and exists(select 1 from bhb b3 where t2.glid=t3.glid and bh='10838318'))