开发者

How to perform a case sensitive search on a case insensitive MySQL database?

开发者 https://www.devze.com 2023-02-28 15:05 出处:网络
In my database which is utf8_general_ci, 99.99% of searches should be done case insensitive. Now there\'s a specific situation where I need to find some data in a case sensitive manner. The field is a

In my database which is utf8_general_ci, 99.99% of searches should be done case insensitive. Now there's a specific situation where I need to find some data in a case sensitive manner. The field is a varchar field, which I usually search case insensitive.

My question is: Can I perform a case sensitive 开发者_运维技巧search on a field that is usually case insensitive?


select * from page where convert(pageTitle using latin1) collate utf8_ = 'Something'

i found this answer descriptive enough :)


You have to change the collation of operands which are used in the search query using COLLATE operator

OR

If you want a column always to be treated in case-sensitive fashion, declare it with a case sensitive or binary collation.

See here http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html


I would suggest changing the collation type so that it becomes case sensitive and then changing your code in the 99.99% of other cases. For information on changing the case of the field check: http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html

You can use the LOWER() and UPPER() functions in SQL to standardise the case for insensitive searches once the field type is set to be case sensitive.


You can make a query case sensitive, for example if you want to check for userid and password:

Select * from TableName Where WHERE LoginId=@userid COLLATE SQL_Latin1_General_CP1_CS_AS AND Password=@password COLLATE SQL_Latin1_General_CP1_CS_AS

it checks for userid and password and returns the values if they are true (case sensitive).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号