gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:34:54 25 4
gpt4 key购买 nike

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

例如,在字符串 '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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com