一张表中有id,而且这张表中id 是重复的 , 在这重复的基础上有不同的值。
写一个sql 查询出来所有id相等的并且重复值大于1的。

解决方案 »

  1.   

    select * from tb group by id having count(*)>1
      

  2.   


    select * from tb group by id >1凡是大于1的都是多出来重复的
      

  3.   

    select id from table group by id having count(id)>1
      

  4.   

    #创建test表
    DROP TABLE IF EXISTS `test`;
    CREATE TABLE `test` (
      `ID` int(11) default '0',
      `name` varchar(255) NOT NULL default 'name'
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;#添加模拟数据
    INSERT INTO `test` VALUES (1,'name');
    INSERT INTO `test` VALUES (4,'name');
    INSERT INTO `test` VALUES (3,'name');
    INSERT INTO `test` VALUES (4,'name');
    INSERT INTO `test` VALUES (4,'name');
    INSERT INTO `test` VALUES (4,'name');
    INSERT INTO `test` VALUES (3,'name');
    INSERT INTO `test` VALUES (5,'name');
    INSERT INTO `test` VALUES (3,'name');
    INSERT INTO `test` VALUES (2,'name');
    INSERT INTO `test` VALUES (1,'name');
    INSERT INTO `test` VALUES (1,'name');#条件:查询ID重复条数>1的记录
    #测试数据有十条,ID只有2和5不满足
    #所以应该除了这两条记录都可以查询到
    select test.* from test
    inner join 

    select ID,count(*) num from test group by ID
    ) tmp
    where test.ID = tmp.ID and tmp.num >1
      

  5.   

    select * from tb group by id having count(*)>1这个了
      

  6.   

    select
     * 
    from
     tb 
    where
     id 
    in 
     (select id from tb group by id having count(1)>1)
      

  7.   

    select id
    from tb
    group by id
    having count(*)>1
      

  8.   

    select * from tb group by id having count(*)>1 加分了
      

  9.   

    select * from table group by id having count(*)>1
    重复大于1次的id都会出来