简体   繁体   English

使MySQL IN子句区分大小写

[英]Making MySQL IN Clause Case Sensitive

Does anyone know how I can make an IN clause behave in a case sensitive manner? 有谁知道如何使IN子句以区分大小写的方式运行? I have seen that COLLATE can be used with LIKE for string searching but I don't know if or how it can be used with IN. 我已经看到COLLATE可以和LIKE一起用于字符串搜索,但我不知道它是否或如何与IN一起使用。 For example I want to do something like 例如,我想做类似的事情

SELECT * FROM pages_table WHERE topic IN ('Food','NightLife','Drinks')

And I want it to return pages where the topic is 'Food' but not those where the topic is 'food' which is currently what happens on this query. 我希望它返回主题为“食物”的页面,而不是那些主题为“食物”的页面,这是当前在此查询中发生的事情。 Thanks. 谢谢。

You can use the BINARY operator. 您可以使用BINARY运算符。 Something like: 就像是:

SELECT *
FROM pages_table
WHERE CAST(topic AS BINARY) IN ('Food','NightLife','Drinks');

SQL Fiddle Demo SQL小提琴演示

You can actually use it as you have likely seen in other examples: 您可以实际使用它,就像您在其他示例中看到的那样:

SELECT *
FROM pages_table
WHERE CAST(topic AS CHAR CHARACTER SET latin1)
        COLLATE latin1_general_cs IN ('Food','NightLife','Drinks')

This changes the character set into one that supports case sensitivity and then collates the column (you may not have to do this depending on your own character encoding). 这会将字符集更改为支持区分大小写的字符集,然后整理列(您可能不必执行此操作,具体取决于您自己的字符编码)。

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

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