0

issue with distinct in joint table

I don't understand why it's would still display duplicate students id eventhough I 'm using DISTINCT : SELECT DISTINCT students.id, students.firstname, students.lastname, teachers.lastname as teachers FROM students, teachers ORDER BY students.ID;

24th Jan 2021, 8:24 PM
Kevin Nadjarian
3 Answers
- 1
Also I think you shouldn't use cross join. If tables teachers and students contain common columns you could use inner or left join.
24th Jan 2021, 10:58 PM
ŠœŠøхŠ°ŠøŠ» Š˜Š²Š°Š½Š¾Š²
ŠœŠøхŠ°ŠøŠ» Š˜Š²Š°Š½Š¾Š² - avatar
- 1
Kevin Nadjarian If you use distinct with multiple columns then it will apply to all columns. You need to use INNER JOIN here. SELECT s.id, s.firstname, s.lastName, t.lastName AS "teacher" FROM students s INNER JOIN teachers t ON s.teacherid = t.id ORDER BY s.id
25th Jan 2021, 3:30 AM
AĶ¢J
AĶ¢J - avatar
- 2
Because rows with the same students.id contain different teachers.lastname. Distinct keyword is applied to all columns.
24th Jan 2021, 10:54 PM
ŠœŠøхŠ°ŠøŠ» Š˜Š²Š°Š½Š¾Š²
ŠœŠøхŠ°ŠøŠ» Š˜Š²Š°Š½Š¾Š² - avatar