zoukankan      html  css  js  c++  java
  • mysql group by遇到的问题

    select userid from t_wxuser group by wxno having count(*) > 1

    像上面这句SQL在oracle中执行就会报错,但mysql不会。

    但是呢,要注意:它只会返回一条数据。这里说的一条不是最终结果只有一条。

    比如说有3个相同wxno的数据(比如wxno=test1),userid分别为101,102,103;还有2个wxno相同的数据(比如test2),userid分别为104,105.

    执行之后返回的数据如下:

    101,104.

    我是在清除t_wxuser表重复数据时发现问题的。执行之后查询发现还有重复的数据,当时以为是sql有问题。

    看了上面这行sql返回的结果才发现是这么回事。

    还有一点呢,你再update/delete一个表的时候,你的from table也是这个表是不行的。

    比如:

    delete from t_wxuser where userid in (select userid from t_wxuser group by wxno having count(*) >1);

    这样是不行的,会报错。

    错误时这样的:

    [Err] 1093 - You can't specify target table 't_wxuser' for update in FROM clause

    所以呢,没办法加一个别名吧。

    delete from t_wxuser where USERID in (select * from (select userid from t_wxuser group by wxno having count(*) > 1) aa);

    这种情况在oracle中是不会有问题的。
  • 相关阅读:
    淡入淡出js
    Comparable和Comparator的区别
    mybatis的动态sql详解
    mybatis动态sql之foreach
    mybatis的动态sql中collection与assoction
    Mybatis中#与$区别
    转JSONObject put,accumulate,element的区别
    Spring配置,JDBC数据源及事务
    销毁session
    IIS express 7.5 设置默认文档
  • 原文地址:https://www.cnblogs.com/luckystar2010/p/4164808.html
Copyright © 2011-2022 走看看