我有数据库,其中包含三个表product、product_size (变体)和handbook。Product包含有关产品的所有数据,handbook知道我的大小,product_size包含关于手册中的产品大小的信息,也包含size_value。
因此,我需要在列product_size中插入值,其中size_price将包含产品价格,handbook_id = 666,product_id是没有产品大小的产品ID。有什么办法吗?
产品
id
price
nameProduct_Size
id
product_id
handbook_id
size_price手册
id
name发布于 2018-06-15 04:22:18
所有产品都意味着产品表中的行。因此,大小可能不在手册中,也可能不在product_size表中。
Select p.name
From product p left join
Product_Size ps
On p.id= ps.product_id
Left join Handbook h
On h.id=ps.handbook_id
Where handbook_id is null发布于 2018-06-15 11:42:57
您可以用这样的短语来表示插入:
insert into product_size (product_id, handbook_id, size_price)
select p.product_id, 666, p.price
from product p
where not exists (select 1
from product_size ps
where ps.product_id = p.product_id and ps.handbook_id = 666
);注意:如果尝试同时运行其中两个插入,这可能会产生意外的结果。为了保护您的数据不受重复的影响,您应该在product_size(product_id, handbook_id)上有一个唯一的约束/索引。
https://stackoverflow.com/questions/50869018
复制相似问题