2017-03-13 1 views
0

맨 오른쪽의 "category"필드에 표시된대로 각 카테고리에서 6 개 항목을 선택하는 mysql 명령을 작성하려고합니다. 기본적으로 범주의 각 값에 대해 6 개의 레코드가 선택되기를 원합니다. Heres는MySQL : 필드의 모든 고유 값에 대해 고정 된 양의 레코드 선택

내가 사용 오전 SELECT *, 데이타베이스

+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------------------+ 
| imageLocation      | productName      | manufacturer | price | availability | category     | 
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------------------+ 
| ./images/Strawberry_Pi_3_Model_B.jpg | Camera Module XF181    | Toro   | 35.99 |   10 | Strawberry Pi    | 
| ./images/Strawberry_Pi_3_Model_B.jpg | Strawberry Pi Extension Kit SG218 | Apollo  | 22.99 |   4 | Popular Items    | 
| ./images/Strawberry_Pi_Zero.jpg  | Strawberry Pi Extension Kit AU252 | Gorella  | 194.99 |   1 | Strawberry Pi Accessories | 
| ./images/Strawberry_Pi_2_Model_B.jpg | Strawberry Pi Case ZM942   | Corona  | 182.99 |   7 | Popular Items    | 
| ./images/Strawberry_Pi_2_Model_B.jpg | Compute Module Kit GP664   | Corona  | 16.99 |   1 | Strawberry Pi Accessories | 
| ./images/Strawberry_Pi_3_Model_B.jpg | Camera Module CL638    | Apollo  | 256.99 |   7 | Strawberry Pi Accessories | 

에 명령이이 명령의 처음 몇 줄의 예. 나는 잠시 루프 (가)마다 모든 카테고리에서 루프를 6 개 항목을 선택하여 (이 경우 3) 종류가 있기 때문에이 여러 번 출마 때까지 계속

SET x = 0; WHILE (x < COUNT(SELECT UNIQUE category FROM PRODUCTS) DO SELECT * FROM Products WHERE category IN (SELECT UNIQUE category FROM Products LIMIT x-1,1) LIMIT 6; SET x = x + 1; END WHILE; 

내가 오류를 얻고을 실행 사용하고 있습니다

ERROR 1193 (HY000): Unknown system variable 'x' 
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHILE (x < COUNT(SELECT UNIQUE category FROM PRODUCTS) DO SELECT * FROM Product' at line 1 
ERROR 1193 (HY000): Unknown system variable 'x' 
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END WHILE' at line 1 

싶습니다 출력의 예

+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------+ 
| imageLocation      | productName      | manufacturer | price | availability | category  | 
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------+ 
| ./images/Strawberry_Pi_3_Model_B.jpg | Camera Module XF181    | Toro   | 35.99 |   10 | Strawberry Pi | 
| ./images/Camera_Module_V2.jpg  | Compute Module Kit GX416   | Belrubi  | 98.99 |   1 | Strawberry Pi | 
| ./images/Sense_Hat.jpg    | Strawberry Pi Extension Kit JJ556 | Toro   | 92.99 |   1 | Strawberry Pi | 
| ./images/Strawberry_Pi_Zero.jpg  | Camera Module FI378    | Belrubi  | 44.99 |   5 | Strawberry Pi | 
| ./images/Compute_Module.jpg   | Compute Module Kit HP564   | Elsanta  | 239.99 |   5 | Strawberry Pi | 
| ./images/Strawberry_Pi_1_Model_A.jpg | Compute Module UZ736    | Revada  | 24.99 |   10 | Strawberry Pi | 
| ./images/Strawberry_Pi_3_Model_B.jpg | Strawberry Pi Extension Kit SG218 | Apollo  | 22.99 |   4 | Popular Items | 
| ./images/Strawberry_Pi_2_Model_B.jpg | Strawberry Pi Case ZM942   | Corona  | 182.99 |   7 | Popular Items | 
| ./images/placeholder.png    | Compute Module VO511    | Darstar  | 188.99 |   3 | Popular Items | 
| ./images/Strawberry_Pi_2_Model_B.jpg | Strawberry Pi DB112    | Tufts  | 79.99 |   1 | Popular Items | 
| ./images/Compute_Model_Kit.jpg  | Compute Module DX828    | Aliso  | 83.99 |   3 | Popular Items | 
| ./images/Strawberry_Pi_Zero.jpg  | Camera Module SZ841    | Glasso  | 115.99 |   6 | Popular Items | 
| ./images/Strawberry_Pi_Zero.jpg  | Strawberry Pi Extension Kit AU252 | Gorella  | 194.99 |   1 | Strawberry Pi Accessories | 
| ./images/Strawberry_Pi_2_Model_B.jpg | Compute Module Kit GP664   | Corona  | 16.99 |   1 | Strawberry Pi Accessories | 
| ./images/Strawberry_Pi_3_Model_B.jpg | Camera Module CL638    | Apollo  | 256.99 |   7 | Strawberry Pi Accessories | 
| ./images/Strawberry_Pi_Case.jpg  | Strawberry Pi LG178    | Tufts  | 26.99 |   10 | Strawberry Pi Accessories | 
| ./images/Sense_Hat.jpg    | Strawberry Pi OW299    | Darstar  | 35.99 |   4 | Strawberry Pi Accessories | 
| ./images/Compute_Module.jpg   | Compute Module Kit QR216   | Confitura | 41.99 |   6 | Strawberry Pi Accessories | 
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------------------+ 

답변

1
SELECT * 
    FROM 
    (SELECT *, 
      @category_rank := IF(@current_category = category, @category_rank + 1, 1) AS category_rank, 
      @current_category := category 
     FROM Products 
     ORDER BY category 
    ) ranked 
    WHERE category_rank <= 6; 
관련 문제