mysql怎么查大写字符 mysql 查询区分大小写
Mysql怎样在SQL中实现区分大小写的查询
1.问题描述
成都创新互联-专业网站定制、快速模板网站建设、高性价比双城网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式双城网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖双城地区。费用合理售后完善,十多年实体公司更值得信赖。
mysql版本:mariadb 10.1.16,采用utf8-ci,不区分大小写的排序规则。
开发人员反应业务要求要区分大小写,就是说当输入小写条件时,只能返回小写条件对应的数据,不能返回大写。
dba@192.168.19.215:3306mdls01:58:57SELECT
- cabinet_no
- FROM
- go_task_ycon_refrence gtyr
- WHERE
- 1 = 1
- AND cabinet_no = 'abcd4499503'
- ORDER BY
- 1 ASC;
+-------------+
| cabinet_no |
+-------------+
| ABCD4499503 |
+-------------+
1 row in set (0.00 sec)
2.问题解决
在条件字段后面加COLLATE utf8_bin ,或在表级或字段级指定COLLATE utf8_bin
dba@192.168.19.215:3306mdls02:08:08SELECT
- cabinet_no
- FROM
- gtyr
- WHERE
- 1 = 1
- AND cabinet_no COLLATE utf8_bin = 'ABCDU4499503'
- ORDER BY
- 1 ASC;
+-------------+
| cabinet_no |
+-------------+
| ABCD4499503 |
+-------------+
1 row in set (0.00 sec)
dba@192.168.19.215:3306mdls02:08:43SELECT
- cabinet_no
- FROM
- gtyr
- WHERE
- 1 = 1
- AND cabinet_no COLLATE utf8_bin = 'abcd4499503'
- ORDER BY
- 1 ASC;
Empty set (0.00 sec)
使MySQL查询区分大小写的实现方法
1、一种方法是可以设置表或行的collation,使其为binary或case
sensitive。在MySQL中,对于Column
Collate其约定的命名方法如下:
*_bin:
表示的是binary
case
sensitive
collation,也就是说是区分大小写的
*_cs:
case
sensitive
collation,区分大小写
*_ci:
case
insensitive
collation,不区分大小写
###########
#
Start
binary
collation
example
###########
mysql
create
table
case_bin_test
(word
VARCHAR(10))
CHARACTER
SET
latin1
COLLATE
latin1_bin;
Query
OK,
rows
affected
(0.02
sec)
mysql
INSERT
INTO
case_bin_test
VALUES
('Frank'),('Google'),('froogle'),('flickr'),('FlicKr');
Query
OK,
5
rows
affected
(0.00
sec)
Records:
5
Duplicates:
Warnings:
mysql
SELECT
*
FROM
case_bin_test
WHERE
word
LIKE
'f%';
+---------+
|
word
|
+---------+
|
froogle
|
|
flickr
|
+---------+
2
rows
in
set
(0.00
sec)
mysql
SELECT
*
FROM
case_bin_test
WHERE
word
LIKE
'F%';
+---------+
|
word
|
+---------+
|
Frank
|
|
FlicKr
|
+---------+
4
rows
in
set
(0.00
sec)
###########
#
End
###########
2、另外一种方法
###########
#
Start
case
sensitive
collation
example
###########
mysql
create
table
case_cs_test
(word
VARCHAR(10))
CHARACTER
SET
latin1
COLLATE
latin1_general_cs;
Query
OK,
rows
affected
(0.08
sec)
mysql
INSERT
INTO
case_cs_test
VALUES
('Frank'),('Google'),('froogle'),('flickr'),('FlicKr');
Query
OK,
5
rows
affected
(0.00
sec)
Records:
5
Duplicates:
Warnings:
mysql
SELECT
*
FROM
case_cs_test
WHERE
word
LIKE
'F%';
+---------+
|
word
|
+---------+
|
Frank
|
|
FlicKr
|
+---------+
4
rows
in
set
(0.00
sec)
mysql
SELECT
*
FROM
case_cs_test
WHERE
word
LIKE
'f%';
+---------+
|
word
|
+---------+
|
froogle
|
|
flickr
|
+---------+
2
rows
in
set
(0.00
sec)
###########
#
end
###########
3、还有一种方法就是在查询时指定collation
mysql
create
table
case_test
(word
VARCHAR(10))
CHARACTER
SET
latin1;
Query
OK,
rows
affected
(0.01
sec)
mysql
INSERT
INTO
case_test
VALUES
('Frank'),('Google'),('froogle'),('flickr'),('FlicKr');
Query
OK,
7
rows
affected
(0.01
sec)
Records:
7
Duplicates:
Warnings:
mysql
SELECT
*
FROM
case_test
WHERE
word
LIKE
'f%';
+---------+
|
word
|
+---------+
|
Frank
|
|
froogle
|
|
flickr
|
|
FlicKr
|
+---------+
6
rows
in
set
(0.01
sec)
mysql
SELECT
*
FROM
case_test
WHERE
word
LIKE
'F%';
+---------+
|
word
|
+---------+
|
Frank
|
|
froogle
|
|
flickr
|
|
FlicKr
|
+---------+
6
rows
in
set
(0.01
sec)
mysql
SELECT
*
FROM
case_test
WHERE
word
COLLATE
latin1_bin
LIKE
'F%';
+---------+
|
word
|
+---------+
|
Frank
|
|
FlicKr
|
+---------+
4
rows
in
set
(0.05
sec)
mysql
SELECT
*
FROM
case_test
WHERE
word
COLLATE
latin1_bin
LIKE
'f%';
+---------+
|
word
|
+---------+
|
froogle
|
|
flickr
|
+---------+
2
rows
in
set
(0.00
sec)
mysql
SELECT
*
FROM
case_test
WHERE
word
LIKE
'f%'
COLLATE
latin1_bin;
+---------+
|
word
|
+---------+
|
froogle
|
|
flickr
|
+---------+
2
rows
in
set
(0.00
sec)
mysql
SELECT
*
FROM
case_test
WHERE
word
LIKE
'F%'
COLLATE
latin1_bin;
+---------+
|
word
|
+---------+
|
Frank
|
|
FlicKr
|
+---------+
4
rows
in
set
(0.01
sec)
mysql
SELECT
*
FROM
case_test
WHERE
word
LIKE
'F%'
COLLATE
latin1_general_cs;
+---------+
|
word
|
+---------+
|
Frank
|
|
FlicKr
|
+---------+
4
rows
in
set
(0.04
sec)
MySql,oracle,sql中对于大小写字符串的查询怎么设置?
字符串都是区分大小写的,都是用upper(字符串)转大写
lower(字符串)转小写
网站栏目:mysql怎么查大写字符 mysql 查询区分大小写
文章来源:http://azwzsj.com/article/ddgcesi.html