mysql - sql subquery return more than 1 row

 mobiledu2502860643 发布于 2022-11-01 19:46
update orders_father set ostatus=5,ofintimesys=now() where oid =
(select oid from
(SELECT oid FROM orders_father where TIMESTAMPDIFF(HOUR,odlvtime,now())>parameter and ostatus=4)
as tempTable)
;

这是代码1。

    update orders_father set ostatus=5,ofintimesys=now() where oid =any
(select oid from
(SELECT oid FROM orders_father where TIMESTAMPDIFF(HOUR,odlvtime,now())>parameter and ostatus=4)
as tempTable)
;

这是代码2,在oid=后面增加了any

我的疑问是,为何代码1会出现Error Code: 1242. Subquery returns more than 1 row
这种错误,而代码2不会? 谢谢各位大神

背景:我是在存储过程中使用的...

2 个回答
  • any 相当 in()

    2022-11-02 17:16 回答
  • where xxx = yyy的时候,右边必须是单一的值,不能是多个值,而你第一个语句里面的

    (SELECT oid FROM orders_father where TIMESTAMPDIFF(HOUR,odlvtime,now())>parameter and ostatus=4)
    as tempTable)

    会查出多个值,所以报Error Code: 1242. Subquery returns more than 1 row的错误

    解决的方法就是把where xxx = yyy变成where xxx in(yyy)或者where xxx = any yyy,这两个表达是一个意思,不过any还可以其他的比较,比如where xxx > any yyy

    2022-11-02 17:20 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有