当前位置:网站首页>String comparison size in MySQL (date string comparison problem)
String comparison size in MySQL (date string comparison problem)
2022-08-02 03:34:00 【asdfadafd】
String comparison size in MySQL (date string comparison problem)
In the database, when comparing the size of strings with numbers and non-numbers, If the lengths of the two strings are equal, then the two strings will comparison at the same position.Character , if the character is a number, it will be compared directly. If the character is not a number, it will be converted to ascii code for comparison.Compare.
As mentioned above, problems arise when strings are used to compare time-formatted data for time-dimension comparisons.When querying as the following sql
select * FROM work_day_content as wdc WHERE wdc.work_day >= '2019-09-30 00:00:00' AND wdc.work_day<= '2019-10-06 00:00:00'![[External link image transfer failed, the origin site may have anti-leech mechanism, it is recommended to save the imageDirect upload(img-FdmqBpj3-1641372898325)(C:UsersASUSAppDataRoamingTypora ypora-user-imagesimage-20220105163155854.png)]](/img/2d/8a728a5fbd8cf62352a288e80f1a0d.png)
Figure 1-1
As shown in Figure 1-1, no conditions were found
When using the date function to convert a string to a date
select * FROM work_day_content as wdc where date(wdc.work_day) >= date('2019-09-30 00:00:00' ) AND date(wdc.work_day)<= date('2019-09-30 00:00:00' )
Figure 1-2
As shown in Figure 1-2, the qualified data was found
Analyze why the first sql statement did not query the content.Since the field is stored in the form of a string, the comparison of the size is performed in the form of a string at this time.
The size of the string comparison is from left to right.When matching strings of different lengths.There is no size difference after the shorter strings are compared.longer strings are larger.As shown in the following sql
select '2019-09-30' < '2019-09-30 00:00:00'![[External link image transfer failed, the origin site may have an anti-leech mechanism, it is recommended to save the imageDirect upload (img-Ls9jHyI4-1641372840373)(C:UsersASUSAppDataRoamingTypora ypora-user-imagesimage-20220105164214829.png)]](/img/7f/9390859c164071120a9f8d4d74cafb.png)
It is found that the **string '2019-09-30 00:00:00'** is larger at this time.Therefore, the previous sql did not find the data dated 2019-09-30
Therefore.When using strings to represent time, you need to use the time function for comparison.
The time functions provided by sql are: date(), str_to_date()
In addition, in mysql, when adding, subtracting, multiplying and dividing between string types, the part of the number that starts with a number will be intercepted for operation. If there is no number in front of the string, the only value that can be intercepted is0, then the result of addition and subtraction is 0, and the result of multiplication and division is NULL.
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢
随机推荐
STL entry basics map and set containers
磷脂-聚乙二醇-叠氮,DSPE-PEG-Azide,DSPE-PEG-N3,MW:5000
(Repost) HashCode Summary (1)
@ApiModel 和 @ApiModelProperty
Freeswitch操作基本配置
跨域问题解决
化学试剂磷脂-聚乙二醇-羟基,DSPE-PEG-OH,DSPE-PEG-Hydroxyl,MW:5000
debian 10 nat 与路由转发
2022年比若依更香的开源项目
黑马案例--实现 clock 时钟的web服务器
Day34 LeetCode
云服务器安装部署Nacos2.0.4版本
基于libmodbus库实现modbus TCP/RTU通信
LeetCode:1161. 最大层内元素和【BFS层序遍历】
MySQL常见的索引
线性代数学习笔记2-2:向量空间、子空间、最大无关组、基、秩与空间维数
MySQL删除表数据 MySQL清空表命令 3种方法
OD-Model [4]: SSD
mysql中json类型字段用法
LeetCode:746. 使用最小花费爬楼梯【动态规划】









