2017-07-06 27 views
1

例如,我有表產品數據如下圖所示:在MYSQL中通過group_id獲取記錄組?

----------------------------------------- 
id | name   | price | group_id 
----------------------------------------- 
1 | iPhone 8   | xxx | 
2 | iPhone 7 black | xxx | 0001 
3 | iPhone 7 white | xxx | 0001 
4 | iPhone 7 red  | xxx | 0001 
5 | iPhone 6 gol  | xxx | 0002 
6 | iPhone 6 white | xxx | 0002 
7 | iPhone 6 silver | xxx | 0002 
8 | iPhone 5 SE  | xxx | 
9 | iPhone 5 gold | xxx | 0003 
10| iPhone 5 white | xxx | 0003 
11| iPhone 5 gary | xxx | 0003 
12| Samsung Galaxy | xxx | 
----------------------------------------- 

我需要(如果存在與關聯),以顯示產品列表,每頁限制3。

例如

第一頁:

  • iPhone 8
  • iPhone 7黑色和它們的組(iPhone 7白色,iPhone 7紅色)
  • iPhone 6金和他們的組(iPhone 6白色,iPhone 6銀)

第二頁:

  • iPhone 5 SE
  • iPhone 5的黃金和他們的小組(iPhone 5的白色,iPhone 5加里)
  • 三星galaxy

如何它可以做到使用MySQL?

+2

選擇在MySQL中每個組的前N是已經很好地覆蓋堆棧溢出的話題。無論如何,爲什麼iPhone 8和iPhone 5 SE不屬於一個組? –

+0

iPhone 5 SE,iPhone 8和三星Galaxy沒有需要一起顯示在第 –

+0

的產品如同明智,分頁完全覆蓋其他地方 – Strawberry

回答

-2

希望這是工作:

SELECT name FROM Products group by group_id ORDER BY id ASC, LIMIT 3, 0 

下一次

SELECT name FROM Products group by group_id ORDER BY id ASC, LIMIT 3, 3 
+0

我可以保證它不是 – Strawberry