本文介紹了'SELECT' 語句中的 'IF' - 根據列值選擇輸出值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
SELECT id, amount FROM report
如果 report.type='P'
和 -amount
如果 <代碼>report.type='N'.如何將其添加到上述查詢中?
I need amount
to be amount
if report.type='P'
and -amount
if report.type='N'
. How do I add this to the above query?
推薦答案
SELECT id,
IF(type = 'P', amount, amount * -1) as amount
FROM report
見http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html.
此外,您可以在條件為空時進行處理.在金額為空的情況下:
Additionally, you could handle when the condition is null. In the case of a null amount:
SELECT id,
IF(type = 'P', IFNULL(amount,0), IFNULL(amount,0) * -1) as amount
FROM report
IFNULL(amount,0)
部分表示當金額不為空時返回金額,否則返回0.
這篇關于'SELECT' 語句中的 'IF' - 根據列值選擇輸出值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!