我在桌子下面,
表产品,
product_id | name
-----------------------
1 | product 1
2 | Product 2
3 | Product 3
4 | product 4
5 | product 5
. | ....
. | ....
表 product_to_zone ,
product_id | zone_id
--------------------
1 | 10
1 | 11
2 | 10
2 | 11
有没有办法在 product_to_zone 表格中为 product_to_zone 表中的所有未分配产品添加 zone_id 10,11 作为默认设置(例如,对于product_id 3,4,5)
答案 0 :(得分:1)
可能是这样的: -
SELECT a.product_id, a.name, b.zone_id
FROM products a
INNER JOIN product_to_zone b
ON a.product_id = b.product_id
UNION
SELECT a.product_id, a.name, 10
FROM products a
LEFT OUTER JOIN product_to_zone b
ON a.product_id = b.product_id
WHERE b.product_id IS NULL
UNION
SELECT a.product_id, a.name, 11
FROM products a
LEFT OUTER JOIN product_to_zone b
ON a.product_id = b.product_id
WHERE b.product_id IS NULL
如果您只想添加未分配的内容,请执行以下操作: -
INSERT INTO product_to_zone (product_id, zone_id )
SELECT a.product_id, 10
FROM products a
LEFT OUTER JOIN product_to_zone b
ON a.product_id = b.product_id
WHERE b.product_id IS NULL
UNION
SELECT a.product_id, 11
FROM products a
LEFT OUTER JOIN product_to_zone b
ON a.product_id = b.product_id
WHERE b.product_id IS NULL
答案 1 :(得分:0)
INSERT INTO product_to_zone (product_id, zone_id)
SELECT * FROM (SELECT (product_id, '10') FROM product WHERE product_id NOT IN
(SELECT a.product_id from product a JOIN product_to_zone b GROUP BY a.product_id)
UNION
SELECT (product_id, '11') FROM product WHERE product_id NOT IN
(SELECT a.product_id from product a JOIN product_to_zone b GROUP BY a.product_id));
这应该会获得product_to_zone中仍然缺少的所有产品,然后使用zone_id 10和11插入到product_to_zone中。
编辑:如果您希望为将来的产品自动完成此操作,请为其触发。
CREATE TRIGGER tr_product_to_zone AFTER INSERT ON product
FOR EACH ROW BEGIN
INSERT INTO product_to_zone (product_id, zone_id)
VALUES (NEW.product_id, '10');
INSERT INTO product_to_zone (product_id, zone_id)
VALUES (NEW.product_id, '11');
END;
答案 2 :(得分:0)
我的回答是指在插入/更新/删除时设置这些默认值,而不是将值设置为其他响应所解决的现有记录。
您可以在插入/更新时执行的存储过程中执行此操作,但我建议您在应用程序级别执行此操作(即在PHP / Perl / Ruby / Python中,或者在您的应用程序中编写的任何内容) )。
由于以下问题,这是一个相当复杂的业务逻辑:
product_to_zone
只有1条记录时会发生什么?product_to_zone
答案 3 :(得分:0)
您可以创建一个过程来插入尚未分配给区域的产品(或创建一个不同的过程),例如:
CREATE PROCEDURE assignProductsToZones ()
BEGIN
DECLARE DECLARE pId INT;
DECLARE cur1 CURSOR FOR SELECT product_id FROM product p where not exists (select 1 from product_to_zone ptz where p.product_id=ptz.product_id);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO pId;
IF done THEN
LEAVE read_loop;
END IF;
INSERT INTO product_to_zone (product_id, zone_id) VALUES (pId,11);
INSERT INTO product_to_zone (product_id, zone_id) VALUES (pId,12);
END LOOP;
CLOSE cur1;
END;
然后调用程序:
CALL assignProductsToZones();
在de products表中插入新产品时,您可能希望安排定期运行或在触发器中使用它。
答案 4 :(得分:0)
INSERT INTO product_to_zone (product_id, zone_id)
SELECT product_id, 10 FROM product WHERE product_id NOT IN (SELECT product_id FROM product_to_zone WHERE zone_id IN (10, 11))
UNION
SELECT product_id, 11 FROM product WHERE product_id NOT IN (SELECT product_id FROM product_to_zone WHERE zone_id IN (10, 11));
答案 5 :(得分:-2)
您可以通过两种方式实现这一目标:
将IGNORE INSERT到product_to_zone (选择product_id,11来自产品);
将IGNORE INSERT到product_to_zone (选择product_id,10来自产品);