2011-05-19 23 views
12
select email from mailing list 

這是我想要使用if條件的查詢。我在郵件列表中有一列爲unsub。如果這unsubtrue電子郵件應在所選擇的結果;否則返回null電子郵件將是選擇的結果如果在選擇查詢中的條件

回答

30
select case unsub when true then null else email end as email,.... 
from ... 

看到the docs for more examples

+0

我嘗試像這樣select case contacts.email_unsub when true then NULL else contact_details.email from contacts join contacts_details contacts.id = contact_details.contact_id。它會在「from」或接近「from」處發出類似此語法錯誤的錯誤。你能解釋一下嗎? – Rafiu 2011-05-19 11:51:40

+0

對不起,我忘記了「結束」關鍵字。我已經將它添加到了我的答案中(即,您需要:「.... else contact_details.email以...作爲電子郵件結束)」 – davek 2011-05-19 12:33:04

+0

@Abdul,您在contact_details.email之後和之前忘記了「END」。請閱讀精美的手冊。 – Unreason 2011-05-19 12:34:54