2017-05-09 83 views
0

我想在MySQL select查詢中使用Case語句。 我得到一個錯誤(子查詢返回多個1行)在MySQL SELECT查詢中使用Case函數,然後選擇

SELECT mony.come,mony.go,mony.details,mony.id_bill, 
    (
    case mony.details 
     when 'collect' then (SELECT collect_from_customer.num FROM collect_from_customer INNER JOIN mony ON mony.id_bill = collect_from_customer.id WHERE collect_from_customer.id=mony.id_bill) 
     when 'pay_to_cust' then (SELECT pay_to_customer.num FROM pay_to_customer INNER JOIN mony ON mony.id_bill = pay_to_customer.id WHERE pay_to_customer.id=mony.id_bill) 
    end 
) as idd 
,mony.date FROM mony 

請幫我

+0

我沒有看到任何'if' ......你正在使用'case'語句,並且您遇到的錯誤是因爲您的子查詢返回了更多的一行......您可以測試爲每個子查詢添加「limit 1」。 – Hackerman

+0

你是正確的...對不起,當選擇(mony.id_bill)需要使用這個值來選擇另一個 –

+0

如果你可以*編輯*你的問題,幷包括這個細節,這將使其他人更容易回答':@ )' – Martin

回答

-1

感謝我找到解決方案

SELECT m.come,m.go,m.details,m.id_bill, 
(
case m.details 
     when 'collect' then (SELECT collect_from_customer.num FROM collect_from_customer INNER JOIN mony m1 ON m1.id_bill = collect_from_customer.id WHERE collect_from_customer.id=m.id_bill LIMIT 1) 
     when 'pay_to_cust' then (SELECT pay_to_customer.num FROM pay_to_customer INNER JOIN mony m1 ON m1.id_bill = pay_to_customer.id WHERE pay_to_customer.id=m.id_bill LIMIT 1) 
end 
) as idd 
,m.date FROM mony m 
+0

這看起來不像任何問題的解決方案:-( – Strawberry