今天做sql查询,发现字段值没区分大小写

mysql> select guid,type,parent_guid from api_assets where guid='3rfI2PsSrCz91mTMDgrZjE';
+------------------------+--------+------------------------+
| guid | type | parent_guid |
+------------------------+--------+------------------------+
| 3rfI2PsSrCz91mTMDgrZjE | Window | 3rfI2PsSrCz91mTMDgry9E |
| 3rfI2PsSrCz91mTMDgrzje | Member | 3rfI2PsSrCz91mTMDgrzj1 |
| 3rfI2PsSrCz91mTMDgrzjE | Plate | 3rfI2PsSrCz91mTMDgrzjU |
+------------------------+--------+------------------------+

系统:

  win7

数据库版本

mysql> select version();
+------------+
| version() |
+------------+
| 5.7.26-log |
+------------+

解决方法

  1.查询时指定大小写敏感

    在查询时指定大小写“敏感”,加关键字“BINARY”

mysql> select guid,type,parent_guid from api_assets where BINARY guid='3rfI2PsSrCz91mTMDgrZjE';
+------------------------+--------+------------------------+
| guid | type | parent_guid |
+------------------------+--------+------------------------+
| 3rfI2PsSrCz91mTMDgrZjE | Window | 3rfI2PsSrCz91mTMDgry9E |
+------------------------+--------+------------------------+

mysql> select guid,type,parent_guid from api_assets where guid= BINARY '3rfI2PsSrCz91mTMDgrZjE';
+------------------------+--------+------------------------+
| guid | type | parent_guid |
+------------------------+--------+------------------------+
| 3rfI2PsSrCz91mTMDgrZjE | Window | 3rfI2PsSrCz91mTMDgry9E |
+------------------------+--------+------------------------+

  2.定义表结构时指定字段大小写敏感

  关键字“BINARY”指定guid字段大小写敏感

CREATE TABLE `api_assets` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`guid` varchar(255) BINARY NOT NULL,
……
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

MySQL允许在大多数字符串类型上使用BINARY关键字,用于指明所有针对该字段的运算是大小写敏感的 

  3.修改排序规则(COLLATION)

mysql> show variables like 'collation\_database';
+--------------------+-----------------+
| Variable_name | Value |
+--------------------+-----------------+
| collation_database | utf8_general_ci |
+--------------------+-----------------+

  Collation以 "_ci"结尾的不区分大小写(ci——Case Ignore),以"_bin"或者"_cs"结尾的区分大小写

  将Collation改为 utf8_bin(大小写敏感的)

  可以为库、表、列指定Collation。

  优先级为 列>表>库

  eg:

mysql> CREATE DATABASE test COLLATE utf8_bin;
Query OK, 1 row affected

mysql> use test;
Database changed
mysql> show variables like 'collation\_database';
+--------------------+----------+
| Variable_name | Value |
+--------------------+----------+
| collation_database | utf8_bin |
+--------------------+----------+

mysql> select guid,type,parent_guid from api_assets where
guid='3rfI2PsSrCz91mTMDgrZjE';
+------------------------+--------+------------------------+
| guid | type | parent_guid |
+------------------------+--------+------------------------+
| 3rfI2PsSrCz91mTMDgrZjE | Window | 3rfI2PsSrCz91mTMDgry9E |
| 3rfI2PsSrCz91mTMDgrzje | Member | 3rfI2PsSrCz91mTMDgrzj1 |
| 3rfI2PsSrCz91mTMDgrzjE | Plate | 3rfI2PsSrCz91mTMDgrzjU |
+------------------------+--------+------------------------+
3 rows in set

mysql> ALTER TABLE `api_assets` DEFAULT CHARACTER SET=utf8 COLLATE=utf8_bin;
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

mysql> select guid,type,parent_guid from api_assets where guid='3rfI2PsSrCz91mTMDgrZjE';
+------------------------+--------+------------------------+
| guid | type | parent_guid |
+------------------------+--------+------------------------+
| 3rfI2PsSrCz91mTMDgrZjE | Window | 3rfI2PsSrCz91mTMDgry9E |
| 3rfI2PsSrCz91mTMDgrzje | Member | 3rfI2PsSrCz91mTMDgrzj1 |
| 3rfI2PsSrCz91mTMDgrzjE | Plate | 3rfI2PsSrCz91mTMDgrzjU |
+------------------------+--------+------------------------+
3 rows in set

mysql> ALTER TABLE `api_assets` MODIFY COLUMN `guid` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL AFTER `id`;
Query OK, 3344 rows affected
Records: 3344 Duplicates: 0 Warnings: 0

mysql> select guid,type,parent_guid from api_assets where guid='3rfI2PsSrCz91mTMDgrZjE';
+------------------------+--------+------------------------+
| guid | type | parent_guid |
+------------------------+--------+------------------------+
| 3rfI2PsSrCz91mTMDgrZjE | Window | 3rfI2PsSrCz91mTMDgry9E |
+------------------------+--------+------------------------+
1 row in set

mysql字段值如何区分大小写的更多相关文章

  1. Mysql的表名/字段名/字段值是否区分大小写

    1.MySQL默认情况下是否区分大小写,使用show Variables like '%table_names'查看lower_case_table_names的值,0代表区分,1代表不区分. 2.m ...

  2. 关于mysql的表名/字段名/字段值是否区分大小写的问题

    http://www.2cto.com/database/201202/121253.html 1.mysql默认情况下是否区分大小写,使用show Variables like '%table_na ...

  3. MySQL 字段值为NULL,PHP用json转换,传给js,显示null

    这个问题出在php的json_encode环节,这个函数返回的json数据中会把空值写作null. 想通过在js端这样把null转为空字符串是不可以的: JSON.parse(JSON.stringi ...

  4. MySQL字段值按照拼音首字母排序

    最简单.快速的方法: 将需要进行排序的字段编码设置为GBK,然后在查询时直接使用asc/desc就可以啦

  5. mysql字段值为null时排序问题

    -- DESC 降序时候默认null值排在后面.ASC升序时默认null值排在前面,可使用 IS NULL处理 ORDER BY score desc,gmPrice IS NULL,gmPrice, ...

  6. MySQL存储的字段是不区分大小写的,你知道吗?

    做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 00 简单回顾 之前写过一篇关于mysql 对表大小写敏感的问题,其实在mysql中字段存储的内容是不区分大小写的,本篇进 ...

  7. Mysql Sql语句令某字段值等于原值加上一个字符串

    MySQL连贯字符串不能利用加号(+),而利用concat. 比方在aa表的name字段前加字符'x',利用: update aa set name=concat('x',name); 替换: UPD ...

  8. mysql字段varchar区分大小写utf8_bin、utf8_general_ci编码区别

    mysql字段varchar区分大小写utf8_bin.utf8_general_ci编码区别 在mysql中存在着各种utf8编码格式:utf8_bin将字符串中的每一个字符用二进制数据存储,区分大 ...

  9. Mysql数据库插入的中文字段值显示问号的问题解决

    最近我使用myeclipse连接mysql数据库查询表中的数据,表中字段值为中文的字段显示问号,查了很多资料将解决方法总结如下: 步骤一:修改mysql数据库的配置文件my.ini或者my-defau ...

随机推荐

  1. Goland在go mod vendor模式下无法识别某些库

    症状:go build可以正常编译,但代码编辑器里面提示找不到相关lib,后来发现是因为go.mod中没有用require这个库,补上库地址和版本.因为项目的mod vendor模式,版本一般不需要写 ...

  2. What is the difference between Reactjs and Rxjs?--React is the V (View) in MVC (Model/View/Controller).

    This is really different, React is view library; and Rxjs is reactive programming library for javasc ...

  3. File upload - MIME type

    Your goal is to hack this photo galery by uploading PHP code.Retrieve the validation password in the ...

  4. [Algorithm] 171. Excel Sheet Column Number

    Given a column title as appear in an Excel sheet, return its corresponding column number. For exampl ...

  5. 2019牛客国庆集训派对day3 买一送一

    题目链接: 题意:有n个点,n-1条单向边,每个点都销售一类商品 问从点1开始走,买第一样商品类型为x,买第二样商品类型为y,问不同有序对<x,y>的数量 解法: col[i]表示这个点的 ...

  6. 腾讯蓝鲸cmdb部署

    蓝鲸配置平台 (CMDB)http://172.16.6.10:8088 环境(单机测试): Centos6 16G 200G 依赖环境: Java 1.8.0_92 python 2.7 ZooKe ...

  7. JAVA基础--MySQL

    环境信息 安装环境 :Ubuntu 14 Desktop(桌面版) m安装版本 :MySQL 5.7.23 安装步骤 linux环境下安装过程很简单 三条命令行即可安装完成: 1. sudo apt- ...

  8. Shared Virtual Memory (SVM) Functions

    Description Shared Virtual Memory (SVM) (Glossary): An address space exposed to both the host and th ...

  9. OpenFOAM——冲击斜坡

    本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL045: Oblique Shock Over an Inclined Ramp ...

  10. 《京东B2B业务架构演变》阅读笔记

    一.京东 B2B 业务的定位 让各类型的企业都可以在京东的 B 平台上进行采购.建立采购关系. 京东 B2B 的用户群体主要分为 2 类: 一类是大 B 用户.另一类是小 B 用户.京东 B 平台需要 ...