sql - how to exclude rows that have duplicates in one field -
i have simple task, cannot find solution. have 2 tables, 'articles' , 'categories'
my article table this:
id | cat_id | title | content 1 1 blah content 1 2 1 blah2 content 2 3 2 blah3 content 3
my categories table this:
id | title 1 category 1 2 category 2
you see have 2 articles have same cat_id. not want duplicate cat_id field. cannot use distinct, because articles, because want fields out.
so if use distinct this:
select distinct a.id, a.cat_id, a.title, a.content articles a
i out, want output this
id | cat_id | title | content 2 1 blah2 content 2 3 2 blah3 content 3
can me please !!!
this query select first article (lowest id) each category
select a.* article left join article a2 on a.id<a2.id , a.cat_id=a2.cat_id a2.id null
it outer join other articles. join clause joins articles same category , smaller id. when there no matches (a2.id null), have article lowest id category.
Comments
Post a Comment