MySQL选择问题

时间:2012-06-24 21:12:33

标签: mysql sql database

表#1: qa_returns_items enter image description here

表#2: qa_returns_residues enter image description here

我有很长时间试图得到这个结果:

item_code -   item_quantity
2         -   1
3         -   2

如果qa_returns_items.item_code = qa_returns_residues.item_code AND status_code = 11那么

  

item_quantity = qa_returns_items.item_quantity -   qa_returns_residues.item_quantity

ELSEIF qa_returns_items.item_code = qa_returns_residues.item_code AND status_code = 12那么

  

item_quantity = qa_returns_items.item_quantity +   qa_returns_residues.item_quantity

ELSE

show diferendes

END IF

我尝试了此查询:

select  SubQueryAlias.item_code, 
        item_quantity
from    (
        select  
                ri.item_code
        ,       case status_code
                when 11 then ri.item_quantity - rr.item_quantity 
                when 12 then ri.item_quantity + rr.item_quantity
                end as item_quantity
        from    qa_returns_residues rr
        left join    qa_returns_items ri
        on      ri.item_code = rr.item_code
        WHERE ri.returnlog_code = 1
        ) as SubQueryAlias
where   item_quantity > 0 GROUP BY (item_code);

查询返回此结果:

item_code -   item_quantity
1         -   2
2         -   2

3 个答案:

答案 0 :(得分:0)

尝试此查询。我还没有测试过它。你需要的是非常困难的。但我已经从表结构和给定条件子句构建它。

select qa_returns_items.item_code, 
       (CASE status_code
         WHEN 11 THEN (qa_returns_items.item_quantity - qa_returns_residues.item_quantity) 
         WHEN 12 THEN (qa_returns_items.item_quantity + qa_returns_residues.item_quantity)   END) as item_quantity , 
        qa_returns_residues.item_unitprice,
       ( qa_returns_residues.item_unitprice * item_quant) as item_subtotal,
       (qa_returns_residues.item_discount * item_quantity) as item_discount,
       ( ( qa_returns_residues.item_unitprice * item_quant) -   
         (qa_returns_residues.item_discount * item_quantity) ) as item_total
where
 item_quantity > 0 AND  qa_returns_items.item_code = qa_returns_residues.item_code

<强>更新

select qa_returns_items.item_code, 
           (CASE status_code
             WHEN 11 THEN (qa_returns_items.item_quantity - qa_returns_residues.item_quantity) 
             WHEN 12 THEN (qa_returns_items.item_quantity + qa_returns_residues.item_quantity)   END) as item_quant , 
            qa_returns_residues.item_unitprice,
           ( qa_returns_residues.item_unitprice * item_quant) as item_subtotal,
           (qa_returns_residues.item_discount * item_quantity) as item_discount,
           ( ( qa_returns_residues.item_unitprice * item_quant) -   
             (qa_returns_residues.item_discount * item_quantity) ) as item_total
    where
    (CASE status_code
             WHEN 11 THEN (qa_returns_items.item_quantity - qa_returns_residues.item_quantity) 
             WHEN 12 THEN (qa_returns_items.item_quantity + qa_returns_residues.item_quantity)   END) item_quant  > 0 AND  qa_returns_items.item_code = qa_returns_residues.item_code

答案 1 :(得分:0)

根据我的经验,我建议您重新开始使用新数据库,没有记录并逐步开始调试。

首先创建数据库和表。之后,只插入2条带有简单数据的记录。然后开始调试。

从我能够调试的问题是你的子选择。在您要查看的主题item_code 2 and 3中。这不可能发生,因为:

SELECT ri.item_code,
       CASE status_code
       WHEN 11 then ri.item_quantity - rr.item_quantity 
       WHEN 12 then ri.item_quantity + rr.item_quantity
       END AS item_quantity
FROM      qa_returns_residues rr
LEFT JOIN qa_returns_items ri
ON        ri.item_code = rr.item_code
WHERE     ri.returnlog_code = 1   // Why do you need this for ?

返回

item_code   item_quantity
2           2
1           0
1           0
1           0
1           2

然后主查询仅选择具有item_quantity > 0的结果。
因此你只得到

item_code   item_quantity
1           2
2           2

结果。

我不太确定此操作的目的是什么,但请记住简单解决方案始终最佳!

答案 2 :(得分:0)

我解决了我的问题:

SELECT  
        item_code, 
        item_quantity, 
        item_unitprice, 
        item_subtotal, 
        item_discount, 
        item_total 
FROM (
        SELECT  qa_returns_items.item_code, 
                qa_returns_items.item_quantity, 
                qa_returns_items.item_unitprice, 
                qa_returns_items.item_subtotal, 
                qa_returns_items.item_discount, 
                qa_returns_items.item_total 
        FROM qa_returns_items 
        WHERE returnlog_code = 1
    UNION
        SELECT  qa_returns_residues.item_code, 
                qa_returns_residues.item_quantity, 
                qa_returns_residues.item_unitprice, 
                qa_returns_residues.item_subtotal, 
                qa_returns_residues.item_discount, 
                qa_returns_residues.item_total 
        FROM qa_returns_residues 
        WHERE returnlog_code = 1 
        ORDER BY item_code ASC
) 
AS SubQueryAlias
WHERE item_code NOT IN (
                        SELECT a.item_code 
                        FROM qa_returns_items a 
                        JOIN qa_returns_residues b 
                            ON b.item_code = a.item_code 
                        WHERE a.returnlog_code = 1 
                            AND b.returnlog_code = 1
                        );