2015-10-16 96 views
0

我想用條件'和'從句執行一個SQL select查詢,但遇到了困難。SQL:我如何使用條件where子句構造一個select查詢?

我願做這樣的事情:

select * from customer 
    where active = true 
    and 
    case 
    when state = 'AK' 
     then zipcode like '%701' 
    when state = 'NV' 
     then zipcode like '%523' 
    else 
     then phone not null; 

我該如何去如何組織我的查詢做到這一點?

在此先感謝您提供的任何指導。

+0

也許你應該嘗試閱讀文檔。這是** [SQL SERVER](https://msdn.microsoft.com/en-us/library/ms181765.aspx)** –

回答

1

我認爲你可以使用or而不是case

select * from customer 
where active = true 
and ((state = 'AK' and zipcode like '%701') or (state = 'NV' and zipcode like '%523')) 
and phone is not null; 
1

你的意思是?

select * from customer 
where active = true 
and ((state = 'AK' and zipcode like '%701') 
    or (state = 'NV' and zipcode like '%523') 
    or (phone not null)) 
+0

我認爲'或'可以作爲'和/或'。所以他會需要一個排他性的或者我想 – AleOtero93