MySQL's where condition string is case sensitive

By default in MySQL, the where condition encounters a string is case-insensitive.

The results of the following two SQL statements are the same:

MySQL [datawarehouse]> select * from temp_01 t where browser = 'ie:11' limit 3;
+-----+----------+-----------+---------+--------+
| id  | platform | version   | browser | counts |
+-----+----------+-----------+---------+--------+
|  48 |        1 | website:1 | IE:11   |     70 |
| 117 |       20 | website:1 | IE:11   |     70 |
+-----+----------+-----------+---------+--------+
2 rows in set (0.01 sec)

MySQL [datawarehouse]> select * from temp_01 t where browser = 'IE:11' limit 3;
+-----+----------+-----------+---------+--------+
| id  | platform | version   | browser | counts |
+-----+----------+-----------+---------+--------+
|  48 |        1 | website:1 | IE:11   |     70 |
| 117 |       20 | website:1 | IE:11   |     70 |
+-----+----------+-----------+---------+--------+
2 rows in set (0.01 sec)

Solution one:

Add keywords after the string field when building the tableBINARY

--创建相同结果的表,将browser字段加上关键字BINARY
CREATE TABLE `temp_02` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `platform` int(10) DEFAULT NULL,
  `version` varchar(50) DEFAULT NULL,
  `browser` varchar(50) BINARY DEFAULT NULL,
  `counts` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=181 DEFAULT CHARSET=utf8;

--将temp_01表的数据导入temp_02中
insert into temp_02(id, platform, version, browser, counts) select id, platform, version, browser, counts from temp_01;

The result of this query is completely different

MySQL [datawarehouse]> select * from temp_02 t where browser = 'ie:11' limit 3;
Empty set (0.00 sec)
;

MySQL [datawarehouse]> select * from temp_02 t where browser = 'IE:11' limit 3;
+-----+----------+-----------+---------+--------+
| id  | platform | version   | browser | counts |
+-----+----------+-----------+---------+--------+
|  48 |        1 | website:1 | IE:11   |     70 |
| 117 |       20 | website:1 | IE:11   |     70 |
+-----+----------+-----------+---------+--------+
2 rows in set (0.00 sec)

Solution two:

Add keywords before the query statement where conditional string field:BINARY

MySQL [datawarehouse]> select * from temp_01 t where BINARY browser = 'IE:11' and version = 'Website:1' limit 3;
+-----+----------+-----------+---------+--------+
| id  | platform | version   | browser | counts |
+-----+----------+-----------+---------+--------+
|  48 |        1 | website:1 | IE:11   |     70 |
| 117 |       20 | website:1 | IE:11   |     70 |
+-----+----------+-----------+---------+--------+
2 rows in set (0.00 sec)

MySQL [datawarehouse]> select * from temp_01 t where BINARY browser = 'IE:11' and BINARY version = 'Website:1' limit 3;
Empty set (0.00 sec)

Solution three:

Modify the field character set:

  • utf8_general_ci ---case insensitive
  • utf8_bin-case sensitive
MySQL [datawarehouse]> ALTER TABLE temp_01 MODIFY COLUMN browser VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL;
Query OK, 180 rows affected (0.01 sec)
Records: 180  Duplicates: 0  Warnings: 0

MySQL [datawarehouse]> 
MySQL [datawarehouse]> select * from temp_01 t where browser = 'ie:11' limit 3;
Empty set (0.00 sec)

Guess you like

Origin blog.csdn.net/lz6363/article/details/107441840