简体   繁体   English

SQL中的IF / ELSE语句

[英]IF / ELSE statements in sql

My SQL statement: 我的SQL语句:

select * 
from categories 
where onair = 1 and City LIKE 's%';

First part: 第一部分:

select * 
from categories 
where onair = 1

second part: 第二部分:

and city like 's%';

If city column is not null , then I want to execute second part too. 如果city列不为null ,那么我也想执行第二部分。 Otherwise, execute only first part. 否则,仅执行第一部分。

You can create this behavior by a combination of the logical and and or operators: 您可以通过逻辑andor运算符的组合来创建此行为:

SELECT *
FROM   categories
WHERE  onair = 1 AND (city IS NULL OR city LIKE 's%');

The case statement is your friend... 案件陈述是你的朋友...

select *, 
case when City like 's%' then City
else '' end as City 
from categories where onair=1

That will only return City if it is like 's%'. 仅当它类似于“ s%”时,才返回City。 Otherwise, it will return blank. 否则,它将返回空白。 Your other criteria is safely separate.. 您的其他条件是安全分开的。

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

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