区分大小写的字符串出现
Case Sensitive String Occurrence
我正在使用以下查询拆分列值。它适用于不区分大小写的情况,但我希望它能够区分大小写。
例如,在字符串 'Oil is a product Ingredients are' 中,如果我的搜索关键字是 'ingredients',它应该 return false
而应该只 return true
如果搜索关键字是 'Ingredients'。 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
您的参数才能执行 case-sensitive 匹配。
例如:如果你的第一条记录是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。
我正在使用以下查询拆分列值。它适用于不区分大小写的情况,但我希望它能够区分大小写。
例如,在字符串 'Oil is a product Ingredients are' 中,如果我的搜索关键字是 'ingredients',它应该 return false
而应该只 return true
如果搜索关键字是 'Ingredients'。 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
您的参数才能执行 case-sensitive 匹配。
例如:如果你的第一条记录是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。