【问题标题】:How to find duplicate columns which are having same values in more than one columns(column names are given)如何查找在多个列中具有相同值的重复列(给出列名)
【发布时间】:2013-10-17 04:31:39
【问题描述】:

我正在使用 mysql db 并创建了一个表。

 student_id roll_no student_name    date_of_join    email_id
    1   ERD001  Ankit   2013-10-16  ankit@abc.in
    2   ERD002  Aman    2013-10-16  aman@abc.in
    3   ERD003  Alok    2013-10-16  alok@abc.in
    4   ERD004  Anshuman    2013-10-16  anshu@abc.in
    6   ERD002  Anil    2013-10-16  anil@abc.in 
    7   ERD004  Hemant  2013-10-17  hemant@abc.in

在这里,我想选择所有具有相同卷号和加入日期的记录。 即我想获取记录-

  2             ERD002      Aman            2013-10-16  aman@abc.in
    6           ERD002      Anil                2013-10-16  anil@abc.in

因为两条记录具有相同的 (ERD002,2013-10-16 )。

我试过了。

SELECT * FROM students
GROUP BY roll_no,date_of_join
HAVING COUNT(student_id)>1

但它只提供一条记录。

【问题讨论】:

    标签: mysql


    【解决方案1】:

    这是一个应该可以工作的查询:

    SELECT * FROM students s
    INNER JOIN
    (SELECT roll_no, date_of_join FROM students
    GROUP BY roll_no, date_of_join
    HAVING COUNT(student_id)>1) rd on s.roll_no = rd.roll_no and s.date_of_join = rd.date_of_join
    ORDER BY s.roll_no, s.date_of_join
    

    【讨论】:

    • 非常感谢大卫。 :)
    【解决方案2】:

    另一种选择是使用exists:

    select * 
    from students t 
    where exists 
    (
      select 1 
      from students
      where roll_no = t.roll_no and 
            date_of_join = t.date_of_join and 
            student_id <> t.student_id
    )
    

    【讨论】:

    • 感谢 Grisha 的回复。它也可以工作并给出相同的结果
    猜你喜欢
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    相关资源
    最近更新 更多