sql 子查询返回值多于一个,当子查询跟随在=、!=、<、>、>=之后,或子查询用作表达式时,这种。。。
selectsalordcodeas'合同号',(selectcnamefromccodewhereccode=salorder.ccode)as'关联方',(selec...
select salordcode as '合同号',
(select cname from ccode where ccode=salorder.ccode) as '关联方',
(select contact from ccode where ccode=salorder.ccode) as '对方当事人',
(select qtyunit from salorderg where salordicode like salorder.salordicode) as '单位',
(select qty from salorderg where salordicode like salorder.salordicode)as '数量' ,
rmb as '人民币',
odate as '生效日期',
signsite as '合同履行地点',
(select bname from bcode where bcode =salorder.bcode ) as '业务员/签订人'
from salorder where rmb>= 6000000 and odate between '2010-1-1' and '2011-8-31'
order by odate;
sql 子查询返回值多于一个,当子查询跟随在=、!=、<、>、>=之后,或子查询用作表达式时,这种情况是不允许的。
问题出在第四、五行,大侠们,salordicode是两个表的主码,能解释下吗,应该怎么调整呢?
谢谢高手,很清晰,照你写的做可以实现。
再请教一个问题,可不可以实现这个要求:
查询到salordicode和qtyunit 两行或几行是一样的,求sum(qty),两行或几行合并为一行。
如 salordicode qtyunit qty
aaa1 035 100
aaa1 035 150
显示为:
salordicode qtyunit sum(qty)
aaa1 035 250 展开
(select cname from ccode where ccode=salorder.ccode) as '关联方',
(select contact from ccode where ccode=salorder.ccode) as '对方当事人',
(select qtyunit from salorderg where salordicode like salorder.salordicode) as '单位',
(select qty from salorderg where salordicode like salorder.salordicode)as '数量' ,
rmb as '人民币',
odate as '生效日期',
signsite as '合同履行地点',
(select bname from bcode where bcode =salorder.bcode ) as '业务员/签订人'
from salorder where rmb>= 6000000 and odate between '2010-1-1' and '2011-8-31'
order by odate;
sql 子查询返回值多于一个,当子查询跟随在=、!=、<、>、>=之后,或子查询用作表达式时,这种情况是不允许的。
问题出在第四、五行,大侠们,salordicode是两个表的主码,能解释下吗,应该怎么调整呢?
谢谢高手,很清晰,照你写的做可以实现。
再请教一个问题,可不可以实现这个要求:
查询到salordicode和qtyunit 两行或几行是一样的,求sum(qty),两行或几行合并为一行。
如 salordicode qtyunit qty
aaa1 035 100
aaa1 035 150
显示为:
salordicode qtyunit sum(qty)
aaa1 035 250 展开
2个回答
展开全部
这是因为第4,5行查询时返回了多个结果,就会报错,如果你只需要一个结果的话,应该改为:
(select first 1 qtyunit from salorderg where salordicode = salorder.salordicode) as '单位',
(select first 1 qty from salorderg where salordicode = salorder.salordicode)as '数量' ,
这样它只是返回找到的第一个结果,就不会发生错误了。
另外,如果你需要它返回所有单位/数量的结果,应该用left outer join,而不是直接select。
select salordcode as '合同号',
(select cname from ccode where ccode=salorder.ccode) as '关联方',
(select contact from ccode where ccode=salorder.ccode) as '对方当事人',
salorderg.qtyunit as '单位',
salorderg.qty as '数量' ,
rmb as '人民币',
odate as '生效日期',
signsite as '合同履行地点',
(select bname from bcode where bcode =salorder.bcode ) as '业务员/签订人'
from salorder
left outer join salorderg on salorderg.salordicode = salorder.salordicode --新加入的
where rmb>= 6000000 and odate between '2010-1-1' and '2011-8-31'
order by odate;
(select first 1 qtyunit from salorderg where salordicode = salorder.salordicode) as '单位',
(select first 1 qty from salorderg where salordicode = salorder.salordicode)as '数量' ,
这样它只是返回找到的第一个结果,就不会发生错误了。
另外,如果你需要它返回所有单位/数量的结果,应该用left outer join,而不是直接select。
select salordcode as '合同号',
(select cname from ccode where ccode=salorder.ccode) as '关联方',
(select contact from ccode where ccode=salorder.ccode) as '对方当事人',
salorderg.qtyunit as '单位',
salorderg.qty as '数量' ,
rmb as '人民币',
odate as '生效日期',
signsite as '合同履行地点',
(select bname from bcode where bcode =salorder.bcode ) as '业务员/签订人'
from salorder
left outer join salorderg on salorderg.salordicode = salorder.salordicode --新加入的
where rmb>= 6000000 and odate between '2010-1-1' and '2011-8-31'
order by odate;
追问
谢谢高手,很清晰,照你写的做可以实现。
再请教一个问题,可不可以实现这个要求:
查询到salordicode和qtyunit 两行或几行是一样的,求sum(qty),两行或几行合并为一行。
如 salordicode qtyunit qty
aaa1 035 100
aaa1 035 150
显示为:
salordicode qtyunit sum(qty)
aaa1 035 250
追答
select salordcode as '合同号', (select cname from ccode where ccode=salorder.ccode) as '关联方', (select contact from ccode where ccode=salorder.ccode) as '对方当事人', salorderg.qtyunit as '单位', sum(salorderg.qty) as '数量' , rmb as '人民币', odate as '生效日期', signsite as '合同履行地点', (select bname from bcode where bcode =salorder.bcode ) as '业务员/签订人' from salorder left outer join salorderg on salorderg.salordicode = salorder.salordicode where rmb>= 6000000 and odate between '2010-1-1' and '2011-8-31' group by salorder.salorder, salorderg.qtyunit, salordcode.rmb, salordcode.odata, salordcode.signsite --新加入的 order by odate;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询