mysql - 区分大小写的字符串出现

标签 mysql case-sensitive

我正在使用以下查询来拆分列值。它适用于不区分大小写的情况,但我希望它能够区分大小写。

例如,在字符串 'Oil is a product Ingredients are' 中,如果我的搜索关键字是 'ingredients',它应该返回 false 并且应该只返回 true 如果搜索关键字是“成分”。 mysql 中是否有任何函数允许这样做?

SELECT 
  SUBSTRING_INDEX(description, 'Ingredients', 1),
    if(LOCATE('Ingredients', description)>0, SUBSTRING_INDEX(description, 'Ingredients', -1), '')
FROM `product`

最佳答案

来自mysql的文档LOCATE 功能:

This function is multibyte safe, and is case-sensitive only if at least one argument is a binary string.

也就是说,您需要cast/convert您的参数以执行区分大小写的匹配。

例如:如果您的第一条记录是Oil is a product Ingredients are...,您的第二条记录是Oil is a product ingredients are.. . 然后是以下查询:

SELECT 
  LOCATE('ingredients', description) AS match_both_1,
  LOCATE('Ingredients', description) AS match_both_2,
  LOCATE(CAST('ingredients' AS BINARY), CAST(description AS BINARY)) AS match_second,
  LOCATE(CAST('Ingredients' AS BINARY), CAST(description AS BINARY)) AS match_first
FROM product

会给你预期的结果:

| match_both_1 | match_both_2 | match_second | match_first |
|     18       |     18       |      0       |     18      |
|     18       |     18       |     18       |      0      |

参见 DEMO .

关于mysql - 区分大小写的字符串出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49371591/

相关文章:

mysql - 检查两个表中是否存在一行

php - 选择 DISTINCT 或 array_unique?

php - 每五个 MYSQL 搜索结果显示一个广告

macos - OSX 10.10.1 上的文件系统区分大小写不匹配(Android Studio 和 IntelliJIDEA)

Perl GetOptions() 区分大小写

mysql - 强制 INNER JOIN/LEFT JOIN 优先级和组

mysql - 按计数选择行组?

postgresql - kettle 从其他数据库转换为 PostgreSQL

ms-access - 如何为 MS Access 编写区分大小写的查询?

powershell 命令行参数以大写形式执行?