LIKE与等式查询比较
我们知道 char 是定长类型的数据,如果数据长度小于定义的长度,会在字符串尾部加上空格。而对于空格的处理,对于等式匹配,或length等,会忽略空格。而对于like 或模式匹配,空格不能忽略。
一、对于系统自动补全的空格
1、数据类型为varchar
对于varchar 类型,由于字符串尾部没有补全空格,like 与 等式比较结果没有区别。
testdb=# create table tab1(id varchar(9));
CREATE TABLE
testdb=# insert into tab1 values('123');
INSERT 0 1
testdb=# select * from tab1 where id='123';
id
-----
123
(1 row) testdb=# select * from tab1 where id like '123';
id
-----
123
(1 row)
2、数据类型为char
对于char类型,字符串后会补全空格字符。对于等式匹配,这种空白字符会忽略;对于模式匹配(like 或正则匹配),则会考虑空白字符。
testdb=# create table tab2(id char(9));
CREATE TABLE
testdb=# insert into tab2 values('123');
INSERT 0 1
testdb=# select * from tab2 where id='123';
id
-----------
123
(1 row) testdb=# select * from tab2 where id like '123';
id
----
(0 rows)
二、对于数据自身带的空格
这里的空格是指字符串自身带的最后的空格。
1、数据类型为Varchar
对于varchar 类型的空格,在比较时,会比较空格。
testdb=# create table tab3(id varchar(9));
CREATE TABLE
testdb=# insert into tab3 values('123 ');
INSERT 0 1
testdb=# select * from tab3 where id='123';
id
----
(0 rows) testdb=# select * from tab3 where id like '123';
id
----
(0 rows)
2、数据类型为char
插入的空格与系统自动补全的空格是一样的。
testdb=# create table tab4(id char(9));
CREATE TABLE
testdb=# insert into tab4 values('123 ');
INSERT 0 1
testdb=# select * from tab4 where id='123';
id
-----------
123
(1 row) testdb=# select * from tab4 where id like '123';
id
----
(0 rows) testdb=# select length(id) from tab4;
length
--------
3
(1 row)
三、对于varchar 与char 类型的空格
对于varchar 与 char,最后的空格是不一样的。
testdb=# select length(substr(id,4,1)) from tab3;
length
--------
1
(1 row) testdb=# select length(substr(id,4,1)) from tab4;
length
-------- (1 row)
再看个例子:
--KingbaseES
test=# select length(substr('1 3 '::char,2,1)),length(substr('1 3 '::char,4,1)) from dual;
length | length
--------+--------
|
(1 row) test=# select length(substr('1 3 '::varchar,2,1)),length(substr('1 3 '::varchar,4,1)) from dual;
length | length
--------+--------
1 | 1
(1 row) --Postgresql
testdb=# select length(substr('1 3 '::char,2,1)),length(substr('1 3 '::char,4,1)) ;
length | length
--------+--------
0 | 0
(1 row) testdb=# select length(substr('1 3 '::varchar,2,1)),length(substr('1 3 '::varchar,4,1)) ;
length | length
--------+--------
1 | 1
(1 row)
四、oracle 的 char 与 varchar
可以看到,二者是没有区别的。
SQL> create table tab1(id1 char(9),id2 varchar(9)); Table created. SQL> insert into tab1 values('1 3 ','1 3 '); 1 row created. SQL> select length(substr(id1,2,1)),length(substr(id1,5,1)),length(substr(id2,2,1)) from tab1; LENGTH(SUBSTR(ID1,2,1)) LENGTH(SUBSTR(ID1,5,1)) LENGTH(SUBSTR(ID2,2,1))
----------------------- ----------------------- -----------------------
1 1 1
LIKE与等式查询比较的更多相关文章
- 【转】Oracle索引的类型
数据库的应用类型分为 OLTP(OnLine Transaction Processing ,联机事务处理):OLTP是传统关系型数据库的主要应用,其主要面向基本的.日常的事务处理,例如银行交易. O ...
- mysql高性能索引策略
转载说明:http://www.nyankosama.com/2014/12/19/high-performance-index/ 1. 引言 随着互联网时代地到来,各种各样的基于互联网的应用和服务进 ...
- 【翻译】从头开始写一个时间序列数据库-Writing a Time Series Database from Scratch
本文来自: https://fabxc.org/tsdb/, 如翻译有误,请纠正. 我是从事监控工作的.特别是Prometheus, 一个包含自定义的时间序列库以及集成Kuberntes的监控系统. ...
- 【翻译】ScyllaDB数据建模的最佳实践
文章翻译自Scylla官方文档:https://www.scylladb.com/2019/08/20/best-practices-for-data-modeling/ 转载请注明出处:https: ...
- SQL Server 数据库子查询基本语法
一.SQL子查询语句 1.单行子查询 select ename,deptno,sal from emp where deptno=(select deptno ...
- 类型转换bin()、chr()、ord() 、int()、float()、str()、repr()、bytes()、tuple(s )、 list(s ) 、unichr(x ) 、 ord(x ) 、 hex(x ) 、 type()数据类型查询
1.bin() 将整数x转换为二进制字符串,如果x不为Python中int类型,x必须包含方法__index__()并且返回值为integer: 参数x:整数或者包含__index__()方法切返回值 ...
- 高性能MySQL笔记 第6章 查询性能优化
6.1 为什么查询速度会慢 查询的生命周期大致可按照顺序来看:从客户端,到服务器,然后在服务器上进行解析,生成执行计划,执行,并返回结果给客户端.其中“执行”可以认为是整个生命周期中最重要的阶段. ...
- Elasticsearch查询
Elasticsearch支持两种类型的查询:基本查询和复合查询. 基本查询,如词条查询用于查询实际数据. 复合查询,如布尔查询,可以合并多个查询, 然而,这不是全部.除了这两种类型的查询,你还可以用 ...
- MS SQL SERVER索引优化相关查询
查找缺失索引 -- ============================================= -- Description: 查询当前数据库中缺失的索引,知道你进行优化的 ...
随机推荐
- windows10 程序和功能没有Hyper-V选项
1.在电脑桌面新建Hyper-V.cmd文件,将如下代码添加到文件中 pushd "%~dp0" dir /b %SystemRoot%\servicing\Packages\*H ...
- Linux编辑shell脚本快速启动jar包
1.上传jar包到服务器 2.创建并编辑start.sh文件 vi start.sh 将下面内容复制到文件中 ps -ef|grep xf-demo |grep -v grep |awk '{prin ...
- gitlab和jenkins做持续集成构建教程
背景介绍 上一个轮回,我花了三篇文章的时间着重向大家介绍了在条件有限的情况下,如果优雅地进行前端发版和迭代.庆七一,热烈庆祝香港回归,人民生活水平越来越好,昨天上午我自掏腰包买了台服务器,决定由冷兵器 ...
- mt19937 用法
老是忘记怎么用,自己写一个用作备忘录吧. 首先需要的头文件: #include <random> 或者是 #include <bits/stdc++.h> //万能头 yyds ...
- Neural Networks
神经网络能够使用torch.nn包构建神经网络. 现在你已经对autogard有了初步的了解,nn基于autograd来定义模型并进行微分.一个nn.Module包含层,和一个forward(inpu ...
- shell判断参数值是否在数组内的方法
比如定义数组: arr=("one" "tow" "thr" "three" "four") 1. ...
- Apache Hudi数据跳过技术加速查询高达50倍
介绍 在 Hudi 0.10 中,我们引入了对高级数据布局优化技术的支持,例如 Z-order和希尔伯特空间填充曲线(作为新的聚类算法),即使在经常使用过滤器查询大表的复杂场景中,也可以在多个列而非单 ...
- vivado没用上的寄存器变量
vivado中定义了但没用上的寄存器变量,在综合时会被移除,即没有综合出来.(如下cnt,虽然在y的过程块中用了cnt作为判断条件,但实际上cnt用了跟没用效果一样,所以综合时cnt_reg就被放弃了 ...
- 清北学堂 2020 国庆J2考前综合强化 Day7
目录 1. 题目 T1 魔力石 题目描述 Sol T2 和 题目描述 Sol T3 数对 题目描述 Sol T4 海豹王国 题目描述 Sol 考场策略 1. 题目 T1 魔力石 题目描述 题目描述 小 ...
- 运行 vue 项目时报错
INFO Starting development server... ERROR Error: C - D:\T32890\Desktop\my-project\node_modules\@vue\ ...