我有4个表product,orders,order_hist和order_prod_ref。数据设置如下:
DROP TABLE products;
DROP TABLE orders;
DROP TABLE order_hist;
DROP TABLE ord_prd_ref;
create table products (product_id varchar2(20),FLG CHAR(1));
create table orders (order_id varchar2(20));
create table order_hist (order_id varchar2(20));
create table ord_prd_ref (product_id varchar2(20),order_id varchar2(20),prd_name varchar2(10));
INSERT INTO PRODUCTS values ('1000365007482','Y');
INSERT INTO PRODUCTS values ('1000359547456','N');
INSERT INTO PRODUCTS values ('1000359524206','N');
INSERT INTO PRODUCTS values ('1000082514435','N');
INSERT INTO PRODUCTS values ('1000088066693','N');
commit;
INSERT INTO ORDERS VALUES ('5000099148559');
INSERT INTO ORDERS VALUES ('5000099236099');
INSERT INTO ORDERS VALUES ('5000099705242');
INSERT INTO ORDERS VALUES ('5000002349523');
INSERT INTO ORDERS VALUES ('5000002349523');
INSERT INTO ORDERS VALUES ('5000099148559');
COMMIT;
INSERT INTO ord_prd_ref VALUES ('1000365007482','5000099148559','A');
INSERT INTO ord_prd_ref VALUES ('1000365007482','5000099148559','B');
INSERT INTO ord_prd_ref VALUES ('1000365007482','5000099148559','C');
INSERT INTO ord_prd_ref VALUES ('1000365007482','5000099148559','D');
INSERT INTO ord_prd_ref VALUES ('1000359547456','5000099236099','E');
INSERT INTO ord_prd_ref VALUES ('1000359547456','5000099236099','F');
INSERT INTO ord_prd_ref VALUES ('1000359547456','5000099236099','G');
INSERT INTO ord_prd_ref VALUES ('1000082514435','5000099705242','H');
INSERT INTO ord_prd_ref VALUES ('1000088066693','5000099236099','I');
INSERT INTO ord_prd_ref VALUES ('1000082514435','5000099705242','J');
INSERT INTO ord_prd_ref VALUES ('1000082514435','5000099705242','K');
COMMIT;
我想在products.flg ='Y'时从订单中将数据插入到order_hist中,并从订单表中删除该记录。
我尝试了此操作,但没有给我预期的结果,因为只有一个product_id带有flg ='Y',即1000365007482
INSERT INTO order_hist
select * FROM orders a
WHERE
EXISTS (
SELECT
NULL
FROM
products,
ord_prd_ref,
orders
WHERE
products.product_id = ord_prd_ref.product_id
AND orders.order_id = ord_prd_ref.order_id
AND FLG = 'Y'
AND products.FLG = 'Y'
);
预期结果应该是将对应的order_id插入order_hist表中,即5000099148559。
select order_id from ord_prd_ref where product_id = '1000365007482';
非常感谢您的帮助。
谢谢。
答案 0 :(得分:1)
尝试一下:
INSERT INTO order_hist
SELECT distinct orders.order_id
FROM
products,
ord_prd_ref,
orders
WHERE
products.product_id = ord_prd_ref.product_id
AND orders.order_id = ord_prd_ref.order_id
AND products.FLG = 'Y'
DELETE from orders where order_id = (
SELECT distinct orders.order_id
FROM
products,
ord_prd_ref,
orders
WHERE
products.product_id = ord_prd_ref.product_id
AND orders.order_id = ord_prd_ref.order_id
AND products.FLG = 'Y')
DELETE from ord_prd_ref where order_id = (
SELECT distinct orders.order_id
FROM
products,
ord_prd_ref,
orders
WHERE
products.product_id = ord_prd_ref.product_id
AND orders.order_id = ord_prd_ref.order_id
AND products.FLG = 'Y')
答案 1 :(得分:1)
此:
select distinct o.order_id
from products p inner join ord_prd_ref opr
on opr.product_id = p.product_id
inner join orders o
on o.order_id = opr.order_id
where p.flg = 'Y'
返回所需的所有订单ID。
因此,将它们插入order_hist
:
insert into order_hist
select * from orders
where order_id in (
select distinct o.order_id
from products p inner join ord_prd_ref opr
on opr.product_id = p.product_id
inner join orders o
on o.order_id = opr.order_id
where p.flg = 'Y'
)
并从订单中将其删除:
delete from orders
where order_id in (
select distinct o.order_id
from products p inner join ord_prd_ref opr
on opr.product_id = p.product_id
inner join orders o
on o.order_id = opr.order_id
where p.flg = 'Y'
)