导读:
SELECTa.sb_id,a.sb_title,a.sb_description,a.sb_postedon,b.sb_img_url FROMb2b_offersa,b2b_offer_imagesbwhere a.sb_id=b.sb_offer_id and a.sb_uid=30
用上面的语句查询只能查到a.sb_id=b.sb_offer_id(完全相等)的语句,而我的要求是以A表为主表,
如果对应的B表的img有记录则显示出来,没有则不显示.下面的语句达到要求,并且查到的记录数是与A表一样的:
SELECTa.sb_id,a.sb_title,a.sb_description,a.sb_postedon,b.sb_img_url FROMb2b_offersa
LEFTJOINb2b_offer_imagesbONa.sb_id=b.sb_offer_id where a.sb_uid=30
(在ORACLE里只加个"(+)"符号就可以解决的问题,在MYSQL里好麻烦喔,在ORACLE里可以这么写:
SELECTa.sb_id,a.sb_title,a.sb_description,a.sb_postedon,b.sb_img_url
FROMb2b_offersa,b2b_offer_imagesbwhere a.sb_id=b.sb_offer_id(+) and a.sb_uid=30
好像在oracle里称为外关联,呵呵,搞不清楚
本文转自
http://hi.baidu.com/guangchun/blog/item/10d4983d39f60802baa16724.html
SELECTa.sb_id,a.sb_title,a.sb_description,a.sb_postedon,b.sb_img_url FROMb2b_offersa,b2b_offer_imagesbwhere a.sb_id=b.sb_offer_id and a.sb_uid=30
用上面的语句查询只能查到a.sb_id=b.sb_offer_id(完全相等)的语句,而我的要求是以A表为主表,
如果对应的B表的img有记录则显示出来,没有则不显示.下面的语句达到要求,并且查到的记录数是与A表一样的:
SELECTa.sb_id,a.sb_title,a.sb_description,a.sb_postedon,b.sb_img_url FROMb2b_offersa
LEFTJOINb2b_offer_imagesbONa.sb_id=b.sb_offer_id where a.sb_uid=30
(在ORACLE里只加个"(+)"符号就可以解决的问题,在MYSQL里好麻烦喔,在ORACLE里可以这么写:
SELECTa.sb_id,a.sb_title,a.sb_description,a.sb_postedon,b.sb_img_url
FROMb2b_offersa,b2b_offer_imagesbwhere a.sb_id=b.sb_offer_id(+) and a.sb_uid=30
好像在oracle里称为外关联,呵呵,搞不清楚
本文转自
http://hi.baidu.com/guangchun/blog/item/10d4983d39f60802baa16724.html