【问题标题】:MySQL: Selecting a fixed amount of records for every unique value in a fieldMySQL:为字段中的每个唯一值选择固定数量的记录
【发布时间】:2017-03-13 22:41:12
【问题描述】:

我正在尝试编写一个 mysql 命令来选择每个类别中的 6 个项目,如最右侧的“类别”字段所示。我基本上希望为每个类别值选择 6 条记录。

这是从 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 |

这是我正在使用的命令。我正在使用 while 循环继续,直到每次循环运行时从每个类别中选择 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 |
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------------------+

【问题讨论】:

    标签: mysql select limit


    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-15
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多