關聯表問題

來源:互聯網
上載者:User
關於文章回複問題
我有個文章表 id 和內容 2個欄位 然後回複也有2個欄位 文章id 和回複內容 欄位
怎麼查詢語句 渲染出以下html 我用leftjoin貌似不好使。。
文章內容

回複內容

回複內容


回複討論(解決方案)

貼出你的代碼看看。

貼出你的代碼看看。

你認為應該怎麼寫leftjoin後 然後怎麼辦?

建議你把你寫的代碼貼出來,人家可以在你的基礎上改。


文章表 a id title content cid 回複表b cid content

select a.title,a.content,b.content from a left join b a.cid=b.cid

僅供參考 沒考慮任何 需求和最佳化

你可以查詢兩次


1.查詢文章id=1的明細
select * from article where id=1

2.查詢文章article_id=1的回複
select *from comment where article_id=1

大概是這麼個意思,供參考

select a.title,a.content,b.content from a left join b on a.cid=b.cid
看紅色部分

你可以查詢兩次


1.查詢文章id=1的明細
select * from article where id=1

2.查詢文章article_id=1的回複
select *from comment where article_id=1

大概是這麼個意思,供參考

我能不能不要2次 我這個要發給用戶端呢 我用leftjoin後 怎麼渲染呢


你可以查詢兩次


1.查詢文章id=1的明細
select * from article where id=1

2.查詢文章article_id=1的回複
select *from comment where article_id=1

大概是這麼個意思,供參考

我能不能不要2次 我這個要發給用戶端呢 我用leftjoin後 怎麼渲染呢



這個2次查詢效率高點,left join 完全沒有必要,你不是還有php的麼,在php變數裡面拼接
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.