MySQL查询 – 使用ORDER BY rand强制区分大小写()
作者:互联网
是否可以对查询强制区分大小写?
我的听起来像这样:
"SELECT g_path FROM glyphs WHERE g_glyph = :g_glyph ORDER BY rand()"
如果g_glyph = r,结果可以是R或r而且它不是我所期望的.
我正在寻找一个区分大小写的回报.
我搜索了我的问题,我发现了这个解决方案:
/*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;
但是以下行根本不起作用:
"SELECT g_path FROM glyphs WHERE g_glyph = :g_glyph ORDER BY BINARY rand()"
有什么建议吗?
非常感谢您的帮助.
解决方法:
字符的顺序和相等性由collation定义.在大多数情况下,使用不区分大小写的排序规则.
如果需要对特定数据使用严格的区分大小写的比较,请使用BINARY
operator:
mysql> SELECT 'a' = 'A';
-> 1
mysql> SELECT BINARY 'a' = 'A';
-> 0
mysql> SELECT 'a' = 'a ';
-> 1
mysql> SELECT BINARY 'a' = 'a ';
-> 0
所以在你的情况下:
SELECT g_path FROM glyphs WHERE BINARY g_glyph = :g_glyph ORDER BY rand()
标签:mysql,case-sensitive 来源: https://codeday.me/bug/20190729/1573645.html