我是使用sql的新手。我试图比较我的表客户端中类型列的内容。如果此列以XXX开头,那么我希望创建具有特定值cat1的新列。如果此列以XXY开头,则为CAT2;如果此列先以XYY开头,然后为CAT3,则为
select name, if SUBSTRING(type, 1, 3)=='XXX' then 'CAT1' as x2
from client.
您需要case
。我推荐like
:
select name, (case when type like 'XXX%' then 'CAT1' end) as x2
from client;
您可以使用CASE
。例如:
select
name,
case when type like 'XXX%' then 'cat1'
when type like 'XXY%' then 'cat2'
when type like 'XYY%' then 'cat3'
end as category
from client