MySQL:Select 多行基于数量列
MySQL: Select with multiple row based in quantity column
我有一个订单 table 比如:
product|quantity
示例:
bread|3
我需要 select 比如:
row1- bread
row2- bread
row3- bread
我是这样制作的:
SELECT product FROM (
SELECT product FROM order WHERE quantity > 0 UNION ALL
SELECT product FROM order WHERE quantity > 1 UNION ALL
SELECT product FROM order WHERE quantity > 2 UNION ALL
SELECT product FROM order WHERE quantity > 3
) s;
效果很好。
但他们告诉我最大数量是 4。
现在我看到订单有 12、32... 所以我不知道最大值。
有更好的方法吗?
您需要一个可以生成数字的 table。如果您的 orders
table 有足够的行,您可以使用变量来生成数字:
select product
from orders o join
(select (@rn := @rn + 1) as n
from orders o cross join (select @rn := 0) vars
) n
on n.n <= o.quantity;
这可以使用数字 table 来完成,如果您没有数字,可以使用 this answer 中描述的方法即时生成一系列数字。使用该方法,您可以 运行 查询如下:
-- set up test data
CREATE TABLE Table1 (product VARCHAR(20), quantity int);
insert into Table1 values ('bread',3), ('butter',5), ('milk',2);
-- set up views for number series
CREATE VIEW generator_16
AS SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL
SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL
SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL
SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL
SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL
SELECT 15;
CREATE VIEW generator_256
AS SELECT ( ( hi.n * 16 ) + lo.n ) AS n
FROM generator_16 lo, generator_16 hi;
-- and the actual query
SELECT product, t.quantity, i.n
FROM Table1 t
JOIN generator_256 i
ON i.n BETWEEN 1 AND t.quantity
ORDER BY t.product, i.n;
结果:
| PRODUCT | QUANTITY | N |
|---------|----------|---|
| bread | 3 | 1 |
| bread | 3 | 2 |
| bread | 3 | 3 |
| butter | 5 | 1 |
| butter | 5 | 2 |
| butter | 5 | 3 |
| butter | 5 | 4 |
| butter | 5 | 5 |
| milk | 2 | 1 |
| milk | 2 | 2 |
我有一个订单 table 比如:
product|quantity
示例:
bread|3
我需要 select 比如:
row1- bread
row2- bread
row3- bread
我是这样制作的:
SELECT product FROM (
SELECT product FROM order WHERE quantity > 0 UNION ALL
SELECT product FROM order WHERE quantity > 1 UNION ALL
SELECT product FROM order WHERE quantity > 2 UNION ALL
SELECT product FROM order WHERE quantity > 3
) s;
效果很好。 但他们告诉我最大数量是 4。 现在我看到订单有 12、32... 所以我不知道最大值。
有更好的方法吗?
您需要一个可以生成数字的 table。如果您的 orders
table 有足够的行,您可以使用变量来生成数字:
select product
from orders o join
(select (@rn := @rn + 1) as n
from orders o cross join (select @rn := 0) vars
) n
on n.n <= o.quantity;
这可以使用数字 table 来完成,如果您没有数字,可以使用 this answer 中描述的方法即时生成一系列数字。使用该方法,您可以 运行 查询如下:
-- set up test data
CREATE TABLE Table1 (product VARCHAR(20), quantity int);
insert into Table1 values ('bread',3), ('butter',5), ('milk',2);
-- set up views for number series
CREATE VIEW generator_16
AS SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL
SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL
SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL
SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL
SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL
SELECT 15;
CREATE VIEW generator_256
AS SELECT ( ( hi.n * 16 ) + lo.n ) AS n
FROM generator_16 lo, generator_16 hi;
-- and the actual query
SELECT product, t.quantity, i.n
FROM Table1 t
JOIN generator_256 i
ON i.n BETWEEN 1 AND t.quantity
ORDER BY t.product, i.n;
结果:
| PRODUCT | QUANTITY | N |
|---------|----------|---|
| bread | 3 | 1 |
| bread | 3 | 2 |
| bread | 3 | 3 |
| butter | 5 | 1 |
| butter | 5 | 2 |
| butter | 5 | 3 |
| butter | 5 | 4 |
| butter | 5 | 5 |
| milk | 2 | 1 |
| milk | 2 | 2 |