简体   繁体   English

MYSQL IN语句,其中值不区分大小写

[英]MYSQL IN statement where values are case-insensitive

I am currently trying to whitelist/blacklist based on user input with the following SQL statements: 我目前正在尝试使用以下SQL语句根据用户输入将白名单/黑名单列入:

SELECT * FROM Customers WHERE City IN ('Paris','London');
SELECT * FROM Customers WHERE City NOT IN ('Paris','London');

What syntax is needed such that Paris and London are case insensitive and match paris and london? 为了使巴黎和伦敦不区分大小写并匹配巴黎和伦敦,需要什么语法?

Thanks. 谢谢。

Just define the proper collation on your db fields and index will work fine too. 只需在数据库字段上定义适当的排序规则,索引也可以正常工作。

http://dev.mysql.com/doc/refman/5.7/en/case-sensitivity.html http://dev.mysql.com/doc/refman/5.7/zh-CN/case-sensitiveivity.html

As you can see with latin1_bin 'MySQL' <> 'mysql' 如您所见,使用latin1_bin 'MySQL' <> 'mysql'

mysql> SET @s1 = 'MySQL' COLLATE latin1_bin,
    ->     @s2 = 'mysql' COLLATE latin1_bin;
mysql> SELECT @s1 = @s2;
+-----------+
| @s1 = @s2 |
+-----------+
|         0 |
+-----------+

But with latin1_swedish_ci are equal 但是与latin1_swedish_ci相等

mysql> SELECT @s1 COLLATE latin1_swedish_ci = @s2;
+-------------------------------------+
| @s1 COLLATE latin1_swedish_ci = @s2 |
+-------------------------------------+
|                                   1 |
+-------------------------------------+

您可以执行此操作,但是如果您索引City希望优化查询,则添加LOWER函数将终止优化:

SELECT * FROM Customers WHERE LOWER(City) IN ('paris', 'london');

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

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