开发者

SQL Join Statement

I have the following SQL SELECT statement

SELECT bar_id, bar_name, town_name, advert_text     
FROM bar, towns, baradverts
WHERE town_id = town_id_fk
AND bar_id = bar_id_fk

My problem is that since not every bar has an advert in table "baradverts", these bars are not coming up in the results.开发者_运维问答 In other words I need a NULL for those bars that do not have an advert string.


It's hard to tell which column goes with which table, but I think this is what you are looking for. If you post your schema that would help.

SELECT bar_id, bar_name, town_name, advert_text      
FROM bar b
inner join towns t on t.town_id = b.town_id_fk 
left outer join baradverts ba on t.town_id = ba.town_id_fk 
    AND b.bar_id = ba.bar_id_fk 


you may find this helpful reading:

Understanding SQL Joins

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜