這可以通過使用數字表來完成,並且如果你沒有一個可以使用所描述的方法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 |
Sample SQL Fiddle
來源
2015-02-09 11:49:24
jpw
請學習後15個職位格式化您的問題。 – 2015-02-09 11:31:30