1

我正在使用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

但它给出了单一的记录。

4

2 回答 2

2

这是一个应该有效的查询:

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
于 2013-10-17T04:41:04.450 回答
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
)
于 2013-10-17T05:02:49.960 回答