occal [问题解决]ORA-01427: 单行子查询返回多个行
有人问题我一个问题,情况如下:
他要用根据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.根据A表的某列去update B表的某列时,一定要找出A B两张表可以关联的所有字段,这样基本上不会出现"ORA-01427: 单行子查询返回多个行";
2.如果A表中真的有重复行,那就加上rownum<2条件解决。
occal [问题解决]ORA-01427: 单行子查询返回多个行的更多相关文章
- oracle ORA-01427: 单行子查询返回多个行
ORA-01427: 单行子查询返回多个行 前几天开发的同事反馈一个问题,说前台系统报出了ORA错误,希望我们能看看是什么原因.java.sql.SQLException: ORA-01427: si ...
- ORA-01427: 单行子查询返回多个行
有人问题我一个问题,情况如下:他要用根据divide_act_channel_day的new_amount字段去更新divide_stat的new_amount字段.两张表关联的条件:day=log_ ...
- oracle[insert 时报错: 单行子查询返回多行]
-- 错误的写法 insert into t_b_partner_vehicle(id, partner_id, vehicle_id) (seq_t_b_partner_vehicle.nextva ...
- Sql中联合查询中的”子查询返回的值不止一个“的问题
在子查询中,如果想实现如下的功能: select lib,count(*),select sum(newsNo) from Table1 group by lib from Tabel1 T1,Tab ...
- MYSQL 子查询返回多列显示
因工作需要,目前研究出一种mysql 技能,与大家分享一下. 需求:关联查询另一个大表数据的某些(一个以上)字段 方案:因关联查询的表数据太大.多表查询影响效率,单个子查询又有些多余.所以采用多列拼接 ...
- SQL server 查询出现:---“子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”SQL查询错误解析---
最近用select进行数据筛选,碰到下面的这个错误: ---子查询返回的值不止一个.当子查询跟随在 =.!=.<.<=.>.>= 之后,或子查询用作表达式时,这种情况是不允许的 ...
- “子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”SQL查询错误解析
为了实现下述代码,首先得有数据库和相应的表格,本文用的是https://blog.csdn.net/qaz13177_58_/article/details/5575711/中的案例,即先用链接中那些 ...
- a.WHERE使用中单行子查询(适用于>,<,=,>=,<=等条件)
a.单行子查询(适用于>,<,=,>=,<=等条件) //查询工资最高的员工编号和员工名 select empno,ename from emp where ...
- insert into select 引起的 "子查询返回的值不止一个。当子查询跟随在**之后,或子查询用作表达式时,这种情况是不允许的"
目录 1.事故现场 1.1 在使用 Insert into Table2 select * from Table1 将表1的数据插入到表2时,报错如下: 1.2 sql 语句 2.推测 3.解决方案 ...
随机推荐
- C++Primer笔记-----day04
1.函数指针.函数指针指向某种特定类型,函数的类型由它的返回类型和形参类型决定,与函数名无关.比如:bool lengthCompare(const string &,const string ...
- MySQL 主从同步失败,数据表修复
问题描述: 接到报警称一台 MySQL 从库同步失败.登录服务器查看错误日志信息如下: Last_Error: Error 'Incorrect key file for table './bfcc/ ...
- js中格式化时间
//时间格式化 Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, &quo ...
- 从零玩转JavaWeb系列7web服务器-----表单的提交
在企业开放中如何提交一张表单? 例如:用户注册表 <!--表单内容--> <form action="/28-mystore/regist" id="R ...
- Linux实战教学笔记48:openvpn架构实施方案(一)跨机房异地灾备
第一章VPN介绍 1.1 VPN概述 VPN(全称Virtual Private Network)虚拟专用网络,是依靠ISP和其他的NSP,在公共网络中建立专用的数据通信网络的技术,可以为企业之间或者 ...
- 将中国标准时间转成yyyy-MM-dd
public static void main(String[] args) throws ParseException { String s = "Tue Jul 12 00:00:00 ...
- VUE+WebPack实现精美前端游戏:CardBattle的游戏场景设置
C:\Users\ZHONGZHENHUA\cardBattle\src\App.vue src/App.vue <template> <div id="app" ...
- [C++] Memory Retrieval(内存检索)
Traverse the memory by (char*) , because every time it will increase by 1byte when i want get the i ...
- It's not too late to start!
It's not too late to start! 以此鼓励,希望能坚持下去,一个半路自学PHP的准PHPer!
- revert
git revert是用一次新的commit来回滚之前的commit