2017-03-13 9 views
0

最後に、「カテゴリ」の欄に示すように、各カテゴリの6項目を選択するmysqlコマンドを書こうとしています。私は基本的に、カテゴリごとに6つのレコードを選択したいと思っています。 databseへの相続人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 | 

これは私が使用していますコマンドです。私はすべてのカテゴリからループが

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; 

を実行するたびに6つの項目を選択することにより、カテゴリ(この場合は3)があるとして、それは私がエラーを取得していますように何回ものために実行されるまで、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; 
です
関連する問題