Toniq
Toniq

Reputation: 5016

Sql select distinct group 2 columns

My table is:

title   type
a       category
b       category
d       tag
a       category
c       tag
r       tag
t       category 
d       category

I am trying to select unique values for each (category and tag)

How would I merge these 2 queries?

SELECT DISTINCT title, type FROM table WHERE type='category'

SELECT DISTINCT title, type FROM table WHERE type='tag'

Upvotes: 0

Views: 29

Answers (2)

Per Malmberg
Per Malmberg

Reputation: 1

It can be done with "group by" which find the unique combinations

select title, type
from table
group by title, type;

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270873

Actually, this would seem to do what you want:

select distinct title, type
from table
where type in ('category', 'tag')
order by type;

I added the order by so the types are all together.

Upvotes: 1

Related Questions