Table A
id b_id type size
Table B
id name
在表中重要的是类型列的三个值:新建,已激活和存档。
我在插入命令后将默认值0设置为A表的大小列
insert into A (id, b_id, type, size) values (1, 101, 'New', 10)
我预期在A表中有以下结果:
select * from A;
ID | 出价 | 类型 | 尺寸 |
---|---|---|---|
1个 | 101 | 新 | 10 |
2 | 101 | 活性 | 0 |
3 | 101 | 封存 | 0 |
但是,它不起作用。那么如何实现呢?
您只插入一行,因此不会自动创建三行。
如果要三行,包括两行具有默认size
值:
insert into A (id, b_id, type, size) values (1, 101, 'New', 10);
insert into A (id, b_id, type) values (1, 101, 'Activated');
insert into A (id, b_id, type) values (1, 101, 'Archive');
如果要为其他两种状态自动复制插入的行,则可以考虑以下内容:
insert into A (id, b_id, type, size)
values d.id, d.b_id, t.type, case when d.type = t.type then d.size else default end
from (values (1, 101, 'New', 10)) d(id, b_id, type, size)
cross join (values ('New'), ('Activated'), ('Archive')) t(type)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句