2014-03-03 20 views
1

我在MySQL的查詢是新...MySQL查詢 - 改變正值負上特定的行...

我有以下查詢:

SELECT 
    DISTINCT gp_style_gr.code_groupe, po_lot.num_style, po_lot_sp.Num_lot, 
    po_lot_sp.num_secti, po_lot_se.code_secti, po_lot.terminer, 
    po_lot.date_livraison, po_lot_sp.qte_1, po_lot_sp.qte_2, 
    po_lot_sp.qte_3, po_lot_sp.qte_4, po_lot_sp.qte_5, 
    po_lot_sp.qte_6, po_lot_sp.qte_7, po_lot_sp.qte_8, 
    po_lot_sp.qte_9, po_lot_sp.qte_10, po_lot_sp.qte_11, 
    po_lot_sp.qte_12, po_lot_sp.qte_13, po_lot_sp.qte_14, 
    po_lot_sp.qte_15, po_lot_sp.qte_16, po_lot_sp.qte_17, 
    po_lot_sp.qte_18, po_lot_sp.qte_19, po_lot_sp.qte_20, 
    po_lot_sp.qte_21, po_lot_sp.qte_22, po_lot_sp.qte_23, 
    po_lot_sp.qte_24, po_lot_sp.qte_25, po_lot_sp.qte_26, 
    po_lot_sp.qte_27, po_lot_sp.qte_28, po_lot_sp.qte_29, 
    po_lot_sp.qte_30 

FROM po_lot_sp 

LEFT OUTER JOIN po_lot_se 
    ON po_lot_se.num_lot = po_lot_sp.num_lot 
     and po_lot_se.num_secti = po_lot_sp.num_secti 

LEFT OUTER JOIN po_lot 
    ON po_lot.num_lot = po_lot_sp.num_lot 
LEFT OUTER JOIN gp_style_gr 
    ON gp_style_gr.num_style = po_lot.num_style 

WHERE 
((gp_style_gr.code_groupe = 'INSTOCK') and (po_lot.terminer = '0') 
    and (po_lot_se.code_secti = '01')) or 
    ((gp_style_gr.code_groupe = 'INSTOCK') and (po_lot.terminer = '0') 
    and (po_lot_se.code_secti = '09')) 

ORDER BY gp_style_gr.code_groupe, po_lot.num_style, po_lot_sp.Num_lot, 
    po_lot_sp.num_secti, po_lot_se.code_secti, po_lot.terminer, 
    po_lot.date_livraison, po_lot_sp.qte_1, po_lot_sp.qte_2, 
    po_lot_sp.qte_3, po_lot_sp.qte_4, po_lot_sp.qte_5, 
    po_lot_sp.qte_6, po_lot_sp.qte_7, po_lot_sp.qte_8, 
    po_lot_sp.qte_9, po_lot_sp.qte_10, po_lot_sp.qte_11, 
    po_lot_sp.qte_12, po_lot_sp.qte_13, po_lot_sp.qte_14, 
    po_lot_sp.qte_15, po_lot_sp.qte_16, po_lot_sp.qte_17, 
    po_lot_sp.qte_18, po_lot_sp.qte_19, po_lot_sp.qte_20, 
    po_lot_sp.qte_21, po_lot_sp.qte_22, po_lot_sp.qte_23, 
    po_lot_sp.qte_24, po_lot_sp.qte_25, po_lot_sp.qte_26, 
    po_lot_sp.qte_27, po_lot_sp.qte_28, po_lot_sp.qte_29, 
    po_lot_sp.qte_30 

返回以下數據:

code_groupe num_style Num_lot num_secti code_secti 
    terminer date_livraison qte_1 qte_2 
INSTOCK ACROBAT D2954 001 01 0 2014-04-04 5 10 
INSTOCK ACROBAT D2954 010 09 0 2014-04-04 1 8 
INSTOCK ACROBAT D2999 001 01 0 2014-04-04 30 90 
INSTOCK ACROBAT D2999 010 09 0 2014-04-04 15 70 

我想查詢更改num_secti 010爲負值...

預期結果:

code_groupe num_style Num_lot num_secti code_secti 
    terminer date_livraison qte_1 qte_2 
INSTOCK ACROBAT D2954 001 01 0 2014-04-04 5 10 
INSTOCK ACROBAT D2954 010 09 0 2014-04-04 -1 -8 
INSTOCK ACROBAT D2999 001 01 0 2014-04-04 30 90 
INSTOCK ACROBAT D2999 010 09 0 2014-04-04 -15 -70 

OR

直接減去num_secti行...

code_groupe num_style Num_lot num_secti code_secti 
    terminer date_livraison qte_1 qte_2 
INSTOCK ACROBAT D2954 001 01 0 2014-04-04 4 2  
INSTOCK ACROBAT D2999 001 01 0 2014-04-04 15 20 

是什麼事情是否可行?

回答

0

你會使用一個case聲明:

select . . . 
     (case when num_secti = '010' then - po_lot_sp.qte_1 else po_lot_sp.qte_1 end) as qte_1, 
     (case when num_secti = '010' then - po_lot_sp.qte_2 else po_lot_sp.qte_2 end) as qte_2, 
     . . .