我正在使用 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
但它给出了单一记录。
答案 0 :(得分: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
答案 1 :(得分: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
)