看板 Database 關於我們 聯絡資訊
是這樣的 假如我有以下兩個table product productimage ------------------------ ----------------------------------- id | name | stock | productid | filename | order | 1 | apple | 2 | 1 | img1.png | 2 | 2 | egg | 5 | 1 | img2.jpg | 0 | 3 | pen | 3 | 2 | pic3.gif | 4 | 2 | pic5.png | 2 | 2 | pic1.gif | 5 | 我想實現的是按product裏面的記錄 合拼 productimage中相對應id中order最小的 一筆記錄 運行完SQL後期望得到的結果會長這樣 : product ----------------------------------------------------------------- id | name | stock | productid | filename | order | 1 | apple | 2 | 1 | img2.jpg | 0 | 2 | egg | 5 | 2 | pic5.png | 2 | 3 | pen | 3 | NULL | NULL | NULL | 目前我使用以下SQL可以達成 : SELECT * FROM product left join(select productid,filename,min(`order`) from productimage Group by productid) images on product.id = images.productid order by product.id asc 我想請問的是比起以上這個用subquery的方法 有沒有更高效的解法? 感謝各位 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.246.129.62 ※ 編輯: rockmanalpha 來自: 60.246.129.62 (09/04 01:08)
tedcat:你還需要把那個最小order對應的image列出來 09/09 02:15