SQL:如何使用条件where子句构造一个select查询?

时间:2015-10-16 19:42:48

标签: sql

我想用条件'和'执行SQL选择查询。条款但有困难。

我想做这样的事情:

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;

如何构建查询来执行此操作?

提前感谢您提供的任何指导。

2 个答案:

答案 0 :(得分: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))

答案 1 :(得分: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;