DISTINCT Query with multiple fields. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

DISTINCT Query with multiple fields.

Hello! When I need to do a query with multiple fields (columns) and there are repeated records in all columns, how can I return a SELECT based on only one repeated field with DISTINCT (if it's possible...)??

21st Aug 2017, 12:39 PM
Raphael Coelho
Raphael Coelho - avatar
4 Answers
+ 1
maybe "GROUP BY %your fieldname%" will help u
21st Aug 2017, 12:44 PM
Yaroslav Pieskov
Yaroslav Pieskov - avatar
+ 1
I did the following: SELECT cad.ID, cad.Name, cad.Status, pe.Date FROM Register cad LEFT JOIN Office pe ON cad.ID = pe.ID WHERE cad.ID <= 100 GROUP BY cad.ID, cad.Name, cad.Status, pe.Date ...But, it keeps sending me back cad.ID repeated... ...The Query returns 103 rows, but I want only 100 (it starts from 1)
21st Aug 2017, 1:09 PM
Raphael Coelho
Raphael Coelho - avatar
0
I did like this and worked: SELECT cad.ID, max(cad.Name), max(cad.Status), max(pe.Date) FROM Register cad LEFT JOIN Office pe ON cad.ID = pe.ID WHERE cad.ID <= 100 group by cad.ID ...The field cad.Date have only 5 registers, so if I write a RIGHT JOIN it gives to me only 5 rows... If anyone know a better way to do this..
21st Aug 2017, 1:20 PM
Raphael Coelho
Raphael Coelho - avatar
- 1
try LIKE operator
23rd Aug 2017, 8:28 AM
Sam Tezel
Sam Tezel - avatar