資料表關聯查詢

來源:互聯網
上載者:User
我有兩個表,表a是商品表,表b是留言表也就是使用者對商品的評論,
a表 id(自增id) name(商品名稱) money(商品價格) b表 id(自增id) content(留言內容) pid(對應的商品id) uid(使用者id)

商品id 商品名稱 商品價格 留言數量
xxx xxx xxx xxx
xxx xxx xxx xxx

< 1 2 3 4 5 6 >



要遍曆出來且分頁顯示,主要是資料表之間怎麼關聯擷取資料後再組成新的二維數組。


回複討論(解決方案)

select a.id as aid,a.name as aname,a.money as amoney,b.id as bid from tableb as b left join table as a on b.pid=a.id where 條件 order by time desc limit 0,10 //每頁10條,pageNow第幾頁
這樣查出來的已經是二維數組了

select a.*,ifnull(b.c,0) from a left join (select pid,count(0)c from b group by pid)b on a.id = b.pid order by a.id desc limit 0,10;

謝謝,大家的熱心解答

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.