我想将列的值与多个列表进行比较,并在每种情况下打印其他内容。它是一个汽车品牌列表,如雪佛兰、福特等。我的逻辑应该是这样的:
if(mycolumn like carlist1, 'this', '')
if(mycolumn like carlist2, 'that', '')
if(mycolumn like carlist3, 'foobar', '')
mycolumn 的值类似于 Chevy-1234
,因此它必须是类似的值,因为数字会发生变化。我怎样才能在 MySQL 中做到这一点?
谢谢!
最佳答案
最好的选择是将汽车列表存储为表格,其中包含以下数据:
Chevy
Ford
等等。
然后你可以通过连接来做你想做的事情:
select t.*,
max(case when cl.brand = 'Chevy' then 'this' end) as Chevy,
max(case when cl.brand = 'Ford' then 'this' end) as Ford,
. . .
from t left outer join
carlist cl
on left(t.col, length(cl.brand)) = cl.brand
group by t.id
否则,你必须处理这个数字:
select (case when left(col, locate('-', col) - 1) = carlist1 then 'this' end),
(case when left(col, locate('-', col) - 1) = carlist2 then 'that' end),
(case when left(col, locate('-', col) - 1) = carlist3 then 'foobar' end)
关于MySQL:列表等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15246231/