我目前正在尝试使用以下SQL语句基于用户输入将白名单/黑名单列入白名单:
SELECT * FROM Customers WHERE City IN ('Paris','London');
SELECT * FROM Customers WHERE City NOT IN ('Paris','London');
需要什么语法,以便巴黎和伦敦不区分大小写并匹配巴黎和伦敦?
感谢。
答案 0 :(得分:3)
只需在数据库字段中定义正确的排序规则,索引也可以正常工作。
http://dev.mysql.com/doc/refman/5.7/en/case-sensitivity.html
正如您在latin1_bin
'MySQL' <> 'mysql'
mysql> SET @s1 = 'MySQL' COLLATE latin1_bin,
-> @s2 = 'mysql' COLLATE latin1_bin;
mysql> SELECT @s1 = @s2;
+-----------+
| @s1 = @s2 |
+-----------+
| 0 |
+-----------+
但latin1_swedish_ci
相等
mysql> SELECT @s1 COLLATE latin1_swedish_ci = @s2;
+-------------------------------------+
| @s1 COLLATE latin1_swedish_ci = @s2 |
+-------------------------------------+
| 1 |
+-------------------------------------+
答案 1 :(得分:2)
您可以执行此操作,但如果您已将City
编入索引以希望优化查询,则添加LOWER
函数将会取消优化:
SELECT * FROM Customers WHERE LOWER(City) IN ('paris', 'london');