zoukankan      html  css  js  c++  java
  • 解决"Subquery returns more than 1 row"sql查询错误

    http://blog.csdn.net/c517984604/article/details/7052186 [Err] 1242 - Subquery returns more than 1 row   --表示子查询返回了多行数据

    例如:

    select * from table1 where table1.colums=(select columns from table2)

    解决方法

    1,select * from table1 where column=any(select columns  from table2)

    2,select * from table1 where column in(select columns  from table2);

    3,select * from table1 a where exists 
    (select columns from tableb2 b
    where b.column=a.column
    );
     
     
     ========================================================================
    mysql报错:SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row
    错误的意思是指子查询结果多于一行。报错如下:
    解决方法:
    以select * from table1 where table1.colums=(select columns from table2);这个sql语句为例。
    1)如果是写入重复,去掉重复数据。然后写入的时候,可以加逻辑判断(php)或者外键(mysql),防止数据重复写入。
       (我实际开发中遇到的就是数据重复写入的情况,在数据库查到有相同的数据两条,这不符原本的原本的业务需求)
    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);
    ---------------------
    作者:ly_dengle
    来源:CSDN
    原文:https://blog.csdn.net/ly_dengle/article/details/78028166
    版权声明:本文为博主原创文章,转载请附上博文链接!
  • 相关阅读:
    centos7 常用工具包安装
    Java.lang.String类
    javaweb系统调优方案
    nginx 优化
    centos7 源码安装nginx
    tomcat8调优
    commons-logging slf4j log4j 区别
    docker 安装centos7并SSH远程连接
    aws mysql 开启慢查询日志, 并利用mysqlsla 分析
    centos7 源码安装goaccess
  • 原文地址:https://www.cnblogs.com/xiaoshen666/p/11075480.html
Copyright © 2011-2022 走看看