mysql错误Subquery?returns?more?than?1?row怎么解决
时间:2024-10-26 21:20 文章来源于网友投稿,仅供参考!
mysql报错:SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row 错误的意思是指子查询结果多于一行。 报错如下解决方法以这个sql语句为例 select*fromtable1wheretable1.colums=(selectcolumnsfromtable2); 1)如果是写入重复,去掉重复数据。然后写入的时候,可以加逻辑判断(php)或者外键(mysql),防止数据重复写入。 (我实际开发中遇到的就是数据重复写入的情况,在数据库查到有相同的数据两条,这不符原本的原本的业务需求) 2)在子查询条件语句加limit 1,找到一个符合条件的就可以了 select*fromtable1wheretable1.colums=(selectcolumnsfromtable2limit1); 3)在子查询前加any关键字 select*fromtable1wheretable1.colums=any(selectcolumnsfromtable2);错误代码:1242 Subquery returns more than 1 row错误描述 错误原因 在编写查询SQL语句时,其中有个字段是从另一张表里获取 selectt.id,(selectnumfromt_user_infowhereid=stuNo)asamountfromt_stu_infotleftjoint_user_infot0ont0.id=t.stuNo 查询出num是多条数据,而外层查询结果是要求num为一条数据 解决办法selectt.id,(selectsum(num)fromt_user_infowhereid=stuNo)asamountfromt_stu_infotleftjoint_user_infot0ont0.id=t.stuNo |
上一篇:mysql如何修改表的字段
下一篇:mysql怎么修改表的字段