2015-10-16 97 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