简体   繁体   English

SQL:如何使用条件where子句构造选择查询?

[英]SQL : How do I construct a select query with conditional where clauses?

I would like to perform an SQL select query with conditional 'and' clauses but am having difficulty. 我想使用条件“和”子句执行SQL选择查询,但是遇到了困难。

I would like to do something like this: 我想做这样的事情:

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;

How do I go about structuring my query to do this? 我该如何构建查询结构呢?

Thanks in advance for any guidance provided. 在此先感谢您提供的任何指导。

Do you mean? 你的意思是?

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

I think you can use or instead of case . 我认为您可以使用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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM