SQL QUERY"子查询返回的值超过1"

时间:2018-01-29 14:04:33

标签: sql sql-server-2000

我尝试从应用程序修改SQL查询但无法使其正常工作

错误:

  

"子查询返回的值超过1。这是不允许的   子查询跟随=,!=,<,< =,>,> =或当子查询用作   一个表达。"

原始查询:

SELECT fycode, fyname, class3, '', '', '', defje, zjm, '', ''
FROM zy_fy

UNION ALL
SELECT Ypcode, Ypname,
    (SELECT a.lbname FROM YK_yplb a where a.lbid = b.yplb)
, gg, sldw, '', '', '', '', ''
FROM YK_ypzd b

UNION ALL
SELECT FYID, NAME, DYKS+'-'+CLASS2, '', '次', '', FYMONEY, ZJM, ZJM1, '' 
FROM mz_fy

修改后的查询:

SELECT fycode, fyname, class3, '', '', '', defje, zjm, '', ''
FROM zy_fy

UNION ALL
SELECT Ypcode, Ypname, 
(select a.lbname from YK_yplb a  where a.lbid = b.yplb)
,gg,sldw, '', 
(select c.dj from YK_kc c  where c.Ypcode = b.Ypcode)
 ,'','',''
FROM YK_ypzd b

UNION ALL
SELECT FYID, NAME, DYKS+'-'+CLASS2, '', '次', '', FYMONEY, ZJM, ZJM1, ''
FROM mz_fy

我刚刚在第3个选择语句的第7列中插入了一个子查询

谢谢!

3 个答案:

答案 0 :(得分:3)

    select fycode,fyname,class3,'','','',defje,zjm,'',''  from zy_fy union all
select  Ypcode, Ypname,(select a.lbname from YK_yplb a  where a.lbid =
b.yplb limit 1),gg,sldw,'',(select c.dj from YK_kc c  where c.Ypcode =
b.Ypcode),'','',''  from YK_ypzd b  union all  select FYID,NAME,DYKS+'-
+CLASS2,'','次','',FYMONEY,ZJM,ZJM1,''  from mz_fy

使用限制

答案 1 :(得分:2)

您只需使用TOP 1来限制子查询只返回一行

SELECT fycode, fyname, class3, '', '', '', defje, zjm, '', ''
FROM zy_fy

UNION ALL
SELECT Ypcode, Ypname, 
(select TOP 1 a.lbname from YK_yplb a  where a.lbid = b.yplb)
,gg,sldw, '', 
(select TOP 1 c.dj from YK_kc c  where c.Ypcode = b.Ypcode)
 ,'','',''
FROM YK_ypzd b

UNION ALL
SELECT FYID, NAME, DYKS+'-'+CLASS2, '', '次', '', FYMONEY, ZJM, ZJM1, ''
FROM mz_fy

答案 2 :(得分:-1)

请使用TOP 1限制子查询只返回一行