我的sql,遍历一个限制语句中的变量

时间:2017-03-20 01:36:31

标签: mysql while-loop

我在c9.io上使用mysql,我想在限制语句中查看一些变量 我本质上想要的是

While (x<count(SELECT DISTINCT category FROM Products){

SELECT * FROM Products WHERE category=(SELECT DISTINCT category FROM Products LIMIT x, 1) LIMIT 6;

}

当我将x设置为等于0-3之间的任何数字时,while循环内的查询工作正常。 恩。 MySQL的&GT; SELECT * FROM Products WHERE category =(SELECT DISTINCT category FROM

Products LIMIT 2, 1) LIMIT 6;                                                
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------------------+
| imageLocation                        | productName                        | manufacturer | price  | availability | category                  |
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------------------+
| ./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 |
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------------------+
6 rows in set (0.00 sec)

我希望查询在x = 0时选择6条记录,在单个查询中x = 1,x = 2和x = 3时。

我尝试过的while循环是

delimiter // CREATE PROCEDURE wholeblock:while_loop() BEGIN DECLARE x INT; SET x = 0; WHILE x < COUNT(SELECT DISTINCT category FROM Products) DO SELECT * FROM Products WHERE category=(SELECT DISTINCT category FROM Products LIMIT x, 1) LIMIT 6; SET x = x + 1; END WHILE; END//

不返回任何内容。

...

...

...

修改

我希望从1个查询的结果输出此输出。不是4个查询

mysql> SELECT * FROM Products WHERE category=(SELECT DISTINCT category FROM Products LIMIT 0, 1) LIMIT 6;+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------+
| 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 |
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------+
6 rows in set (0.00 sec)

mysql> SELECT * FROM Products WHERE category=(SELECT DISTINCT category FROM Products LIMIT 1, 1) LIMIT 6;                                                
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------+
| imageLocation                        | productName                        | manufacturer | price  | availability | category      |
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------+
| ./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 |
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------+
6 rows in set (0.00 sec)

mysql> SELECT * FROM Products WHERE category=(SELECT DISTINCT category FROM Products LIMIT 2, 1) LIMIT 6;                                                
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------------------+
| imageLocation                        | productName                        | manufacturer | price  | availability | category                  |
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------------------+
| ./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 |
+--------------------------------------+------------------------------------+--------------+--------+--------------+---------------------------+
6 rows in set (0.00 sec)

mysql> SELECT * FROM Products WHERE category=(SELECT DISTINCT category FROM Products LIMIT 3, 1) LIMIT 6;                                                
+--------------------------------------+---------------------------+--------------+--------+--------------+--------------------------+
| imageLocation                        | productName               | manufacturer | price  | availability | category                 |
+--------------------------------------+---------------------------+--------------+--------+--------------+--------------------------+
| ./images/Strawberry_Pi_Zero.jpg      |  Strawberry Pi DS812      | Tufts        | 172.99 |            6 | Industrial Compute Model |
| ./images/Strawberry_Pi_Case.jpg      |  Compute Module Kit CL851 | Confitura    | 219.99 |            5 | Industrial Compute Model |
| ./images/placeholder.png             |  Compute Module Kit CM175 | Tago         | 298.99 |            3 | Industrial Compute Model |
| ./images/Strawberry_Pi_3_Model_B.jpg |  Camera Module MY484      | Tago         |  33.99 |            8 | Industrial Compute Model |
| ./images/Sense_Hat.jpg               |  Compute Module Kit EB177 | Glasso       |  66.99 |            5 | Industrial Compute Model |
| ./images/Strawberry_Pi_3_Model_B.jpg |  Compute Module PW477     | Elsanta      |  29.99 |            5 | Industrial Compute Model |
+--------------------------------------+---------------------------+--------------+--------+--------------+--------------------------+
6 rows in set (0.01 sec)

1 个答案:

答案 0 :(得分:1)

您尝试构建的查询类型称为&#34;每个组的前n个&#34;并且通常使用ROW_NUMBER窗口函数在其他RDMBS中有效地完成,遗憾的是在MySQL中它仍然缺失。好消息是MySQL中的会话变量可以用来模拟它。

SELECT *
  FROM (
    SELECT p.*,        
           @i := IF(@p = p.category, @i + 1, 1) rn, -- increment a row number if it's still the same category
           @p = p.category -- preserve the category
      FROM products p JOIN (
        SELECT DISTINCT category
          FROM products
         ORDER BY category -- always explicitly set an order when using limit
         LIMIT 4
      ) c ON p.category = c.category CROSS JOIN (
        SELECT @i := 0, @p := NULL -- Initialize vars in-line
      ) i
     ORDER BY p.category, id -- order by category first, then by something else i.e. id, productname
) q
 WHERE rn < 7 -- return only top 6 per group (category in your case)