1

I have two tables entries(id,name,desc,cat_id) and categories(id,name,parent)

entries.cat_id and categories.id are FK and between entries and categories there is a 1:n relationship. So how to get distinct cat_id from entries and its name in categories. I tried with

SELECT DISTINCT
  entries.cat_id
FROM entries
JOIN categories ON categories.id = entries.cat_id

but I get only cat_id as result but I want categories name also. Where is my mistake? Thank You in advance.

2 Answers 2

1
SELECT DISTINCT 
cat_id, categories.name
FROM entries JOIN categories ON categories.id = entries.cat_id
0
0

Simply add the column you want to your select:

SELECT DISTINCT entries.cat_id
                categories.name
FROM            entries    
JOIN            categories ON categories.id = entries.cat_id
1
  • I thought it was allowed only to add columns from entries in that part of the sql. Thank you :)
    – user370306
    Feb 24, 2012 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.