【转】ORA-01427: 单行子查询返回多个行,连表查询去重

转自:http://blog.chinaunix.net/uid-23

实例1

有人问题我一个问题,情况如下:
他要用根据divide_act_channel_day的new_amount字段去更新divide_stat的new_amount字段。
两张表关联的条件:day=log_time,channel=channel

--SQL如下:
update divide_stat 
set divide_stat.new_amount=(select divide_act_channel_day.new_amount from divide_act_channel_day
where divide_stat.day=divide_act_channel_day.log_time 
and divide_stat.channel=divide_act_channel_day.channel 
);

SQL 错误: ORA-01427: 单行子查询返回多个行
01427. 00000 -  "single-row subquery returns more than one row"


--推测子查询中肯定有返回多行的情况,试着在子查询中加入rownum<2,也就是限制返回一行数据。成功!
update divide_stat 
set divide_stat.new_amount=(select divide_act_channel_day.new_amount from divide_act_channel_day
where divide_stat.day=divide_act_channel_day.log_time 
and divide_stat.channel=divide_act_channel_day.channel and rownum<2);


--找出divide_act_channel_day表重复行。有9行重复
select * from 
(
select count(*) total,log_time,channel  from divide_act_channel_day
group by log_time, channel
)
where total>1;

TOTAL                  LOG_TIME                  CHANNEL                                            
---------------------- ------------------------- -------------------------------------------------- 
2                      2012-12-12 00:00:00       0                                                  
2                      2012-12-13 00:00:00       0                                                  
2                      2013-01-07 00:00:00       0                                                  
2                      2012-12-15 00:00:00       0                                                  
2                      2012-12-01 00:00:00       0                                                  
2                      2012-12-31 00:00:00       0                                                  
2                      2012-12-04 00:00:00       0                                                  
2                      2012-12-23 00:00:00       0                                                  
2                      2012-12-21 00:00:00       0                                                  

9 所选行


--观察divide_act_channel_day表,发现它根本没有重复行。看来是where条件精度不够造成的行重复。

--观察divide_act_channel_day和divide_stat两张表,发现它们还有可以关联的列:amount和NEW_USER_AMOUNT。
--这样就没有重复行了。
select * from 
(
select count(*) total,log_time,channel,amount,NEW_USER_AMOUNT  from divide_act_channel_day
group by log_time, channel, amount, NEW_USER_AMOUNT
)
where total>1;

no rows selected



--修改upadte语句
update divide_stat 
set divide_stat.new_amount=(select divide_act_channel_day.new_amount from divide_act_channel_day 
where divide_stat.day=divide_act_channel_day.log_time 
and divide_stat.channel=divide_act_channel_day.channel and  divide_stat.amount=divide_act_channel_day.amount
and  divide_stat.NEW_USER_AMOUNT=divide_act_channel_day.NEW_USER_AMOUNT
);
 

结论1


结论:
1.根据A表的某列去update B表的某列时,一定要找出A B两张表可以关联的所有字段,这样基本上不会出现"ORA-01427: 单行子查询返回多个行";
2.如果A表中真的有重复行,那就加上rownum<2(rownum=1)条件解决。

 

实例2

http://www.bubuko.com/infodetail-2285758.html

如果B表符合条件的记录数大于1条,就会出现1:n的情况,这样left join后的结果,记录数会多于A表的记录数。

例如:member与member_login_log表的结构如下,member记录会员信息,member_login_log记录会员每日的登入记录。member表的id与member_login_log表的uid是对应关系。

member 表

技术分享

 

member_login_log 表

技术分享

 

查询member用户的资料及最后登入日期:
如果直接使用left join

select a.id, a.username, b.logindate
from member as a 
left join member_login_log as b on a.id = b.uid;

因member_login_log符合条件的记录比member表多(a.id = b.uid),所以最后得出的记录为:

技术分享

 

但这并不是我们要的结果,因此这种情况需要保证B表的符合条件的记录是空或唯一,我们可以使用group by来实现。

select a.id, a.username, b.logindate
from member as a 
left join (select uid, max(logindate) as logindate from member_login_log group by uid) as b
on a.id = b.uid;

 

 

 

 

 

select a.id, a.username, b.logindate

from member as 

left join (select uid, max(logindate) as logindate from member_login_log group by uidas b

on a.id = b.uid;

技术分享

技术分享

 

结论2

1、尝试用distinct、max()、group by、分析函数(over()等)等去重

------distinct 去重复查询

select * from  accounts acc join (select distinct accid from roles) r on r.accid=acc.ID

-----不需要distinct select * from (select MAX(ID)roleid,accid from roles group by accid) rr join (select * from accounts) acc on acc.ID=rr.accid

 

--------解释一下不用distinct 去重复查询语句

Select * from (Select max(不重复的字段就行) as roleid,要去重复字段名 From 数据表 Where  条件 Group by   要去重复字段名) as A join 数据库表 on 条件

2、使用left join的两个表,最好是1:1 或 1:0的关系,这样可以保证A表的记录全部显示,B表显示符合条件的记录

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值