【问题标题】:unexpected output from sql query -oraclesql查询-oracle的意外输出
【发布时间】:2019-02-18 22:58:07
【问题描述】:

我有 4 个表 products 、 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 并从订单表中删除该记录。

我试过了,但它没有给我预期的结果,因为只有一个带有 flg='Y' 的 product_id 即 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';

非常感谢任何帮助。

提前致谢。

【问题讨论】:

    标签: sql oracle


    【解决方案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')
    

    【讨论】:

    • 感谢@Ted 它对插入有用,但删除呢?它也应该从订单表中删除
    • 想清楚。如果您可以通过从订单等中选择相关的 order_id 值来插入 order_hist,那么您可以通过从 order_hist 中选择相关的 order_id 值从订单中删除。这是一个单独的查询。
    • @kashi 我编辑了我的答案,并为您提供了表订单和 ord_prd_ref 所需的 2 个删除语句。如果这一切都满足您的问题,请不要忘记将答案标记为正确。
    【解决方案2】:

    这个:

    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'
    )
    

    【讨论】:

      猜你喜欢
      • 2011-01-25
      • 2023-03-05
      • 2011-07-17
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 2020-02-01
      相关资源
      最近更新 更多