Sql表查询

时间:2015-07-11 19:14:14

标签: sql

我有下面的表有重复记录。我想要检索具有重复ID但不同名称,日期的记录。

CREATE TABLE Student1
    (`id` int,`status` int,`amount` int , `Name` varchar(10), `date` varchar(55))
;
    INSERT INTO Student1
    (`id`,`status`,`amount`, `Name`, `date`)
VALUES
    (1,0,4500, 'ram', '04/02/2012'),
    (2,0,2000, 'shyam', '05/09/2013'),
    (4,0,1500, 'ghanshyam', '08/11/2014'),
    (3,0,4500, 'gopal', '04/02/2012'),
    (2,0,8000, 'radheshyam', '15/11/2013'),
    (4,1,1500, 'ghanshyam', '18/10/2015'),
     (1,1,4500, 'ram', '14/02/2012'),
    (2,0,6500, 'radhe', '11/11/2014'),
    (3,1,4500, 'gopal', '14/02/2015')
;

预期结果:

     id status    amount     Name        date
       2    0       2000      shyam      05/09/2013
       2    0       8000    radheshyam   15/11/2013
       2    0       6500      radhe      11/11/2014

1 个答案:

答案 0 :(得分:0)

select * from Student1
where id in
(
  select id from Student1
  group by id
  having count(distinct name) > 1
  and count(distinct date) > 1
)