用于显示数量的SQL查询

时间:2016-07-26 11:26:47

标签: sql

我在表格中有两列:

Item name    quantity
---------------------
A               5

想要显示如下:

Item name    quantity
---------------------
A               1
A               1
A               1
A               1
A               1

2 个答案:

答案 0 :(得分:4)

我假设你有一个数字表,它有一个整数的列(在大多数数据库中很容易生成一个):

select t.item_name, 1 as quantity
from t join
     numbers n
     on t.quantity <= n.n;

例如:

select t.item_name, 1 as quantity
from t join
     (select 1 as n union all select 2 union all select 3 union all select 4 union all select 5
     ) n
     on t.quantity <= n.n;

答案 1 :(得分:1)

如果您使用Oracle,请尝试以下操作:

SELECT i_name, 1 FROM 
  (SELECT 'A' i_name, 5 qty FROM dual) 
CONNECT BY LEVEL <= qty;