如何在MySQL上进行SQL区分大小写的字符串比较? [英] How can I make SQL case sensitive string comparison on MySQL?

查看:280
本文介绍了如何在MySQL上进行SQL区分大小写的字符串比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回大小写混合的五个字符的函数.如果我对此字符串进行查询,则无论大小写都会返回该值.

I have a function that returns five characters with mixed case. If I do a query on this string it will return the value regardless of case.

如何使MySQL字符串查询区分大小写?

How can I make MySQL string queries case sensitive?

推荐答案

默认字符集和排序规则为latin1和latin1_swedish_ci,因此默认情况下非二进制字符串比较不区分大小写.这意味着,如果使用col_name LIKE'a%'搜索,则将获得所有以A或a开头的列值.要使此搜索区分大小写,请确保其中一个操作数具有区分大小写或二进制排序规则.例如,如果要比较均具有latin1字符集的列和字符串,则可以使用COLLATE运算符使两个操作数具有latin1_general_cs或latin1_bin排序规则:

The default character set and collation are latin1 and latin1_swedish_ci, so nonbinary string comparisons are case insensitive by default. This means that if you search with col_name LIKE 'a%', you get all column values that start with A or a. To make this search case sensitive, make sure that one of the operands has a case sensitive or binary collation. For example, if you are comparing a column and a string that both have the latin1 character set, you can use the COLLATE operator to cause either operand to have the latin1_general_cs or latin1_bin collation:

col_name COLLATE latin1_general_cs LIKE 'a%'
col_name LIKE 'a%' COLLATE latin1_general_cs
col_name COLLATE latin1_bin LIKE 'a%'
col_name LIKE 'a%' COLLATE latin1_bin

如果希望始终以区分大小写的方式对待列,请使用区分大小写或二进制排序规则对其进行声明.

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

这篇关于如何在MySQL上进行SQL区分大小写的字符串比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆