case when 条件里面可以用 like嘛
时间: 2023-07-09 19:42:32 浏览: 185
可以使用,但是需要注意一些细节。在使用 LIKE 时,需要使用通配符(%或_)来表示模糊匹配。在 CASE WHEN 语句中,通配符需要放在引号内。例如:
```
SELECT
CASE
WHEN column_name LIKE '%abc%' THEN '包含abc'
WHEN column_name LIKE 'abc%' THEN '以abc开头'
ELSE '其他'
END
FROM table_name;
```
需要注意的是,LIKE 语句的性能较差,因此在大数据量的情况下,应该尽可能避免使用。
相关问题
case when 用count
case when 可以配合 count 函数一起使用,用来在统计过程中根据条件进行计数。比如,可以使用 case when 来统计某种条件下的数量。例如:sum(case when xs_code like '23%' or xs_code like '24%' then 1 else 0 end) 就是在统计 xs_code 字段满足 '23%' 或者 '24%' 条件的记录数量。这样的语句可以用来统计符合特定条件的记录数量。
如果执行这个逻辑case when PlaceCode like 'shiqu%' then '370200' when PlaceCode like 'shinan%' then '370202' when PlaceCode like 'shibei%' then '370203' when PlaceCode like 'huangdao%' then '370211' when PlaceCode like 'laoshan%' then '370212' when PlaceCode like 'licang%%' then '370213' when PlaceCode like 'chengyang%' then '370214' when PlaceCode like 'kaifa%' then '370215' when PlaceCode like '%jichang%' then '370216' when PlaceCode like 'laixi%' then '370285' when PlaceCode like 'jiaozhou%' then '370281' when PlaceCode like 'jimo%' then '370282' when PlaceCode like 'pingdu%' then '370283' end应该怎么写呢
你可以把这个case-when语句放在你的SQL语句中,如:
```
SELECT PlaceCode,
CASE
WHEN PlaceCode LIKE 'shiqu%' THEN '370200'
WHEN PlaceCode LIKE 'shinan%' THEN '370202'
WHEN PlaceCode LIKE 'shibei%' THEN '370203'
WHEN PlaceCode LIKE 'huangdao%' THEN '370211'
WHEN PlaceCode LIKE 'laoshan%' THEN '370212'
WHEN PlaceCode LIKE 'licang%%' THEN '370213'
WHEN PlaceCode LIKE 'chengyang%' THEN '370214'
WHEN PlaceCode LIKE 'kaifa%' THEN '370215'
WHEN PlaceCode LIKE '%jichang%' THEN '370216'
WHEN PlaceCode LIKE 'laixi%' THEN '370285'
WHEN PlaceCode LIKE 'jiaozhou%' THEN '370281'
WHEN PlaceCode LIKE 'jimo%' THEN '370282'
WHEN PlaceCode LIKE 'pingdu%' THEN '370283'
END AS PlaceCodeNew
FROM your_table;
```
这个语句会根据PlaceCode的值匹配对应的值,并返回一个新的列 PlaceCodeNew。
阅读全文