Welcome Guest, Not a member yet? Register   Sign In
How to find dublicate entry in mysql db-table
#1

[eluser]Jan_1[/eluser]
Hi everybody!
How do i find dublicate entries in one database table?

| id | id_class | id_student |
1 1 2
2 2 3
3 1 2
4 2 4

I need a list of 'id_student' with double entries on id_class. (example: line 3)
Is there a way to let MySQL make the 'hard work'?

#2

[eluser]vitoco[/eluser]
Code:
$sql = "
SELECT
id_student ,
COUNT( id_class ) AS number_of_classes
FROM
table
GROUP BY
id_student
HAVING
number_of_classes > 1
";

How this work?, "GROUP BY $field" gives one row per $field and do the operations ( on select ) to the other fields, SUM(), COUNT(), GROUP_CONCAT(), etc...in this case, count the "id_class" per id_student

!! Note that "number_of_classes" can't be conditioned on WHERE, cause it's not a "original" field, and it can be condiotioned on "HAVING"., on this particular case, will limit ( return ) only the students with more than 1 class

Slds.




Theme © iAndrew 2016 - Forum software by © MyBB