Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Distinct value SQL

I have table like this

-------------------------------------------------------------------
id | title | image  | name |
-------------------------------------------------------------------
1  | xyzab | so.jpg | googl |
2  | acbde | am.jpg | artic |
3  | xyzab | pp.jpg | other |

i want to select unique or distinct title with it's image and name also. DO not want to repeat the values. I use this this code

SELECT DISTINCT title,image,name,id FROM `some_table`

but this is not working fine

NOTE: The OP is working with MySQL

like image 339
Basic Bridge Avatar asked Mar 26 '26 09:03

Basic Bridge


1 Answers

Using DISTINCT will ensure no 2 records have all columns matching, so this is working correctly.

If you want to return unique titles, you need to decide what image and name would be returned.

You could use a group by with an aggregate function to do this. For example:

SELECT title, MIN(image), MIN(name), MIN(id)
FROM `some_table`
GROUP BY title

But it depends on what results you are after...

like image 194
Curtis Avatar answered Mar 28 '26 23:03

Curtis