多表關聯查詢

兩表關聯查詢的sql語句,咱們會發現限制條件不一樣是,查詢出來的結果是不同的。sql

1.下面我們使用左鏈接查詢:post

select a.id,a.articlename,a.articledes,a.listimg,a.author,a.click,b.userid from yxdj_post as a left join yxdj_postfavorite as b on a.id=b.articlecode and b.userid='$userid' where a.`status`=1 order by $sort $order limit $offset,$pagesize

當咱們設置$userid變量爲1041288時,咱們將會看到以下結果: 輸出結果code

2.去掉左連接(left)查詢:圖片

select a.id,a.articlename,a.articledes,a.listimg,a.author,a.click,b.userid from yxdj_post as a join yxdj_postfavorite as b on a.id=b.articlecode and b.userid='$userid' where a.`status`=1 order by $sort $order limit $offset,$pagesize

輸出結果

select a.id,a.articlename,a.articledes,a.listimg,a.author,a.click,b.userid from yxdj_post as a left join yxdj_postfavorite as b on a.id=b.articlecode  where a.`status`=1 and b.userid='$userid' order by $sort $order limit $offset,$pagesize

3.若是咱們把限制條件更換一下位置同時也設置$userid變量爲1041288時會發現: 輸出結果it

相關文章
相關標籤/搜索