MySQL:列表等?

标签 mysql sql-like

我想将列的值与多个列表进行比较,并在每种情况下打印其他内容。它是一个汽车品牌列表,如雪佛兰、福特等。我的逻辑应该是这样的:

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/

相关文章:

mysql - 将我的 sql 行转换为列

mysql - 无法在 macOS Sierra 上安装 mysql2 gem

c# - Linq 类似或其他结构

php - 多个 LIKE 引用多行的 WHERE 条件

java - 如何从 SQL LIKE 子句中转义特殊字符?

java - 在实时服务器上部署 Java EE 应用程序

java - 使用 hibernate 用数据填充表的正确方法

使用 SHA256 的 PHP 登录脚本

PHP MySQL 搜索组合列

MySQL LIKE 语句不起作用