關於文章回複問題
我有個文章表 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變數裡面拼接