mysql错误:Subquery returns more than 1 row
mysql报错:sqlstate[21000]: cardinality violation: 1242 subquery returns more than 1 row
错误的意思是指子查询结果多于一行。
报错如下

解决方法
以这个sql语句为例
select * from table1 where table1.colums=(select columns from table2);
1)如果是写入重复,去掉重复数据。在写入数据时,可以使用逻辑判断(PHP)或外键(MySQL)来防止重复写入数据。
(我实际开发中遇到的就是数据重复写入的情况,在数据库查到有相同的数据两条,这不符原本的原本的业务需求)
挖错网
一款支持文本、图片、视频纠错和AIGC检测的内容审核校对平台。
28 查看详情
2)在子查询条件语句加limit 1,找到一个符合条件的就可以了
select * from table1 where table1.colums=(select columns from table2 limit 1);
3)在子查询前加any关键字
select * from table1 where table1.colums=any(select columns from table2);
错误代码:1242 Subquery returns more than 1 row
错误描述
1 queries executed, 0 success, 1 errors, 0 warnings查询:SELECT t.id, DATE_FORMAT( t.statisTime, ‘%Y-%m-%d %H:%i:%s’ ) statisTime, (SELECT `id` FROM t_truck_info WHERE id = t.plateId…错误代码: 1242Subquery returns more than 1 row执行耗时 : 0.009 sec传送时间 : 0.002 sec总耗时 : 0.012 sec
错误原因
在编写查询SQL语句时,其中有个字段是从另一张表里获取
select t.id,(select num from t_user_info where id = stuNo) as amount from t_stu_info t left join t_user_info t0on t0.id = t.stuNo
查询出num是多条数据,而外层查询结果是要求num为一条数据
解决办法
select t.id,(select sum(num) from t_user_info where id = stuNo) as amount from t_stu_info t left join t_user_info t0on t0.id = t.stuNo
以上就是mysql错误Subquery returns more than 1 row怎么解决的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/270263.html
微信扫一扫
支付宝扫一扫