我的数据库中有一个像这样的表
HatID HatName HatPrice LowestStock HighestStock Count
________________________________________________________________________
ERA New Era $25.99 1 5 -
STA Starter $20 1 5 -
但是,我想要实现这样的表,
HatID HatName HatPrice LowestStock HighestStock Count
________________________________________________________________________
ERA000001 New Era $25.99 1 5 1
ERA000002 New Era $25.99 1 5 2
ERA000003 New Era $25.99 1 5 3
ERA000004 New Era $25.99 1 5 4
ERA000005 New Era $25.99 1 5 5
STA000001 Starter $20 1 5 1
STA000002 Starter $20 1 5 2
STA000003 Starter $20 1 5 3
STA000004 Starter $20 1 5 4
STA000005 Starter $20 1 5 5
我知道您无法在SQL Schema中对此进行编码,但我很好奇我将如何在Java中执行此操作
我如何用Java编写代码?
我之所以想要像后者这样的桌子,以便在我进行销售后,我可以删除与该购买相关的HatID。
答案 0 :(得分:0)
你需要创建一个数字表并与源表连接。最好用100个数字创建数字表
create table numbers(number);
insert into numbers
select 1 union all select 2 union all
select 3 union all select 4 union all
select 5;
select
t1.HatID , t1.HatName ,t1.HatPrice ,t1.LowestStock
,t1.HighestStock
,t2.number
from
your_table as t1 inner join numbers as t2
on t2.number>=t1.LowestStock and t2.number<=t1.HighestStock;
答案 1 :(得分:-1)
要做到这一点,您可以采用两种方式:
Insert Into
SQl Query插入表中。因此,您不必编写多个插入查询。