注册 登录
编程论坛 VFP论坛

求助子查询返回多条记录怎么处理

sharpex1 发布于 2023-03-20 16:40, 1226 次点击
如题,在使用 sele *,(sele 字段 from b wher a.x=b.x)  from a 中,由于b有重复记录,经常提示子查询返回多条记录出错,请问各位大佬有什么好的处理方法。
12 回复
#2
sdta2023-03-20 16:45
为什么要这样处理?
原问题应该是如何查询啊?
#3
sharpex12023-03-20 16:50
回复 2楼 sdta
大佬好,那问下如果B中有重复记录,我怎么选择某字段较大的一个作为返回值,谢谢
#4
星光悠蓝2023-03-20 16:54
既然b有重复记录,就先处理b的重复数据
#5
sdta2023-03-20 16:57
是不是这个意思
CREATE CURSOR tt (a1 c(10),a2 n(2))
INSERT INTO tt VALUES ("aaa",6)
INSERT INTO tt VALUES ("aaa",8)
INSERT INTO tt VALUES ("aaa",1)
INSERT INTO tt VALUES ("aaa",2)
INSERT INTO tt VALUES ("bbb",0)
INSERT INTO tt VALUES ("bbb",4)
SYS(3099,70)
SELECT a1, MAX(a2) a2 FROM tt GROUP BY a1
#6
sharpex12023-03-20 16:58
回复 4楼 星光悠蓝
就是想尽量精简一点呗,能一句不写两句
#7
sharpex12023-03-20 17:04
回复 5楼 sdta
不是,B是另外一个表,想在A表 中返回 B表 X字段,条件 是 A.编号=b.编号,我的命令 sele *,(sele x from B wher a.编号=b.编号) from a ,这里出错,提示子查询返回多条记录,因为B编号有重复,所以想问下怎么 在这个   sele *,(sele x from B wher a.编号=b.编号) from a  基础上改一下,如果B返回多条记录,取X字段值较大那个
#8
sdta2023-03-20 17:13
试试
sele *,(sele distinct x from B wher a.编号=b.编号) from a
#9
sharpex12023-03-20 17:45
回复 8楼 sdta
X不重复呢,重复的是编号,所以DIST(X) 还是会提示 返回多条记录
#10
sdta2023-03-20 18:10
上传相关文件
#11
csyx2023-03-20 18:14
Create cursor 表A (编号 I, 姓名 V(10))
Create cursor 表B (编号 I, 年月 N(8,2))

Insert into 表A (编号, 姓名) Values (1, '张三')
Insert into 表A (编号, 姓名) Values (2, '李四')
Insert into 表A (编号, 姓名) Values (3, '王五')

Insert into 表B (编号, 年月) Values (1, 1990.01)
Insert into 表B (编号, 年月) Values (2, 1990.02)
Insert into 表B (编号, 年月) Values (3, 2000.07)
Insert into 表B (编号, 年月) Values (2, 1990.05)

Select a.*, b.年月 from 表a a ;
inner join 表b b on a.编号 == b.编号 ;
inner join ( ;
    select 编号, Max(年月) 年月 from 表b group by 编号) c ;
    on b.编号 == c.编号 and b.年月 = c.年月
#12
倦猫19732023-03-20 18:20
Top 1 + Order By b.字段 [desc]
#13
mywisdom882023-03-21 00:13
以下是引用sharpex1在2023-3-20 17:04:36的发言:

不是,B是另外一个表,想在A表 中返回 B表 X字段,条件 是 A.编号=b.编号,我的命令 sele *,(sele x from B wher a.编号=b.编号) from a ,这里出错,提示子查询返回多条记录,因为B编号有重复,所以想问下怎么 在这个   sele *,(sele x from B wher a.编号=b.编号) from a  基础上改一下,如果B返回多条记录,取X字段值较大那个

取B表中,X字段的最大值
select max(x) x ,编号 from b group by 编号
联合起来就是
select t1.*,t2.x from a t1;
inner join (select max(x) x ,编号 from b group by 编号) t2 ;
on t1.编号=t2.编号
1