MySQL 区分大小写的查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7857669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:25:26  来源:igfitidea点击:

MySQL case sensitive query

mysqlcase-sensitive

提问by Michael Liao

This has been asked on this site before but I couldn't find a sufficient answer. If I'm doing a query like:

以前在这个网站上有人问过这个问题,但我找不到足够的答案。如果我正在做这样的查询:

Select Seller from Table where Location = 'San Jose'

How can I make it return only Sellers with Location 'San Jose' instead of 'san jose' or something else?

我怎样才能让它只返回位置为“圣何塞”而不是“圣何塞”或其他东西的卖家?

回答by James mason

MySQL queries are not case-sensitive by default. Following is a simple query that is looking for 'value'. However it will return 'VALUE', 'value', 'VaLuE', etc…

默认情况下,MySQL 查询不区分大小写。以下是一个寻找“值”的简单查询。但是它会返回“VALUE”、“value”、“Value”等……

SELECT * FROM `table` WHERE `column` = 'value'

The good news is that if you need to make a case-sensitive query, it is very easy to do using the BINARYoperator, which forces a byte by byte comparison:

好消息是,如果您需要进行区分大小写的查询,使用BINARY运算符非常容易,它会强制逐字节比较:

SELECT * FROM `table` WHERE BINARY `column` = 'value'

回答by Shenxian

To improve James' excellent answer:

为了改进詹姆斯的出色回答:

It's better to put BINARYin front of the constant instead:

最好放在BINARY常量前面:

SELECT * FROM `table` WHERE `column` = BINARY 'value'

Putting BINARYin front of columnwill prevent the use of any index on that column.

放在BINARY前面column将阻止在该列上使用任何索引。

回答by Thomas Clowes

Whilst the listed answer is correct, may I suggest that if your column is to hold case sensitive strings you read the documentationand alter your table definition accordingly.

虽然列出的答案是正确的,但我可以建议,如果您的列要保存区分大小写的字符串,请阅读文档并相应地更改表定义。

In my case this amounted to defining my column as:

就我而言,这相当于将我的专栏定义为:

`tag` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT ''

This is in my opinion preferential to adjusting your queries.

我认为这优先于调整您的查询。