简体   繁体   English

MySQL查询 - 使用ORDER BY rand强制区分大小写()

[英]MySQL query - force case-sensitive with a ORDER BY rand( )

Is it possible to force case-sensitive for a query? 是否可以对查询强制区分大小写?

Mine sounds like this: 我的听起来像这样:

"SELECT g_path FROM glyphs WHERE g_glyph = :g_glyph ORDER BY rand()"

if g_glyph = r, the result can be R or r and it's not what I expect. 如果g_glyph = r,结果可以是R或r而且它不是我所期望的。 I'm looking for a case-sensitive return. 我正在寻找一个区分大小写的回报。

I googled my issue and I found this solution: 我搜索了我的问题,我发现了这个解决方案:

/*Case-sensitive sort in descending order.
In this query, ProductName is sorted in 
case-sensitive descending order.
*/
SELECT ProductID, ProductName, UnitsInStock
FROM products
ORDER BY BINARY ProductName DESC;

But the following line doesn't work at all: 但是以下行根本不起作用:

"SELECT g_path FROM glyphs WHERE g_glyph = :g_glyph ORDER BY BINARY rand()"

Any Suggestion? 有什么建议吗?

Thank you very much for your help. 非常感谢您的帮助。

The order and equality of characters is defined by the collation . 字符的顺序和相等性由排序规则定义。 In most cases, a case-insensitive collation is used. 在大多数情况下,使用不区分大小写的排序规则。

If you need to use a strict, case-sensitive comparison for a specific datum, use the BINARY operator : 如果需要对特定数据使用严格的区分大小写的比较,请使用BINARY运算符

mysql> SELECT 'a' = 'A';
        -> 1
mysql> SELECT BINARY 'a' = 'A';
        -> 0
mysql> SELECT 'a' = 'a ';
        -> 1
mysql> SELECT BINARY 'a' = 'a ';
        -> 0

So in your case: 所以在你的情况下:

SELECT g_path FROM glyphs WHERE BINARY g_glyph = :g_glyph ORDER BY rand()

This is covered in the manual page Case Sensitivity in String Searches . 在字符串搜索的手册页案例敏感性中有所介绍。

You need to specify a case sensitive or binary collation. 您需要指定区分大小写或二进制排序规则。

The default character set and collation are latin1 and latin1_swedish_ci, so nonbinary string comparisons are case insensitive by default. 默认字符集和排序规则是latin1和latin1_swedish_ci,因此非二进制字符串比较默认情况下不区分大小写。 This means that if you search with col_name LIKE 'a%', you get all column values that start with A or a. 这意味着如果使用col_name LIKE'a%'进行搜索,则会获得以A或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: 例如,如果要比较具有latin1字符集的列和字符串,则可以使用COLLATE运算符使任一操作数具有latin1_general_cs或latin1_bin排序规则:

 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. 如果希望始终以区分大小写的方式处理列,请使用区分大小写或二进制排序规则来声明它。 See Section 13.1.14, “CREATE TABLE Syntax”. 请参见第13.1.14节“CREATE TABLE语法”。

The _cs in the collation name stands for "case sensitive". 排序规则名称中的_cs代表“区分大小写”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM