我想從同一個SP中選擇所有客戶或一個客戶。現在我們正在mantaining兩個SP來獲得這些信息:我的SQL:如果在where語句中添加條件?
即要獲得所有客戶:
select id, name from customers
,並獲得一個客戶:
select id, name from customers
where id=id_from_input
使它們成爲通用,我想的將id_from_input
定爲空以獲得所有客戶。我嘗試了幾種方法在sql中創建一個條件where語句,該語句可用於此計劃,但沒有任何工作。例如:
select id, name from customers
where if(id_from_input <> null) id=id_from_input; end if
給我語法錯誤。
如何在id_from_input
爲空時返回所有行的where
子句,否則爲匹配行?
'CASE WHEN'是查詢了'IF'語法。 – Hexxed