问题描述
是否可以对查询强制区分大小写?
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()"
如果g_glyph = r,结果可以是R或r,这不是我期望的.我正在寻找区分大小写的退货.
if g_glyph = r, the result can be R or r and it's not what I expect.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()"
有什么建议吗?
非常感谢您的帮助.
推荐答案
字符的顺序和相等性由排序规则.在大多数情况下,使用不区分大小写的排序规则.
The order and equality of characters is defined by the collation. In most cases, a case-insensitive collation is used.
如果需要对特定的基准使用严格的,区分大小写的比较,请使用 BINARY
运算符:
If you need to use a strict, case-sensitive comparison for a specific datum, use the 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查询-使用ORDER BY rand()强制区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!