i have table containing last comments posted on website, , i'd join different table depending on comment type.
comments table similar structure:
id | type | ressource_id | ---+------+--------------+ 1 | 1 | 10 | 2 | 3 | 7 | 3 | 3 | 12 | 4 | 1 | 22 | 5 | 4 | 22 | 6 | 5 | 23 | news table:
news_id | notes| date | --------+------+--------------+ 10 | | 2015-08-12 | 22 | | 2015-07-12 | tutorial table:
tuto_id | notes| date | --------+------+--------------+ 7 | | 2015-06-15 | 12 | | 2015-05-14 | ... similar table type = 4, 5, 6
now in order specific comments doing left join on 2 tables.
select co.* comments co left join news n on co.id = n.news_id , co.type = 1 left join tutorial t on co.id = t.tuto_id , co.type = 3 (co.type in (1,3)) i interested in getting date left table. how can include column in output list.
result desired: (date joining table)
id | type | ressource_id | date | ---+------+--------------+--------------+ 1 | 1 | 10 | 2015-08-12 | 2 | 3 | 7 | 2015-06-15 | 3 | 3 | 12 | 2015-05-14 | 4 | 1 | 22 | 2015-07-12 | thanks.
since never date news , tutorial same comment might go withcoalesce`:
select co.*, coalesce(n.date,t.date) comments co left join news n on co.id = n.news_id , co.type = 1 left join tutorial t on co.id = t.tuto_id , co.type = 3 (co.type in (1,3)) coalesce return first argument not null, if there matching news return date news , if there no matching news matching tutorial return date tutorial.
Comments
Post a Comment