当前位置:网站首页>String comparison size in MySQL (date string comparison problem)
String comparison size in MySQL (date string comparison problem)
2022-08-01 14:30:00 【Programmer over time】
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
边栏推荐
- 2022图片在线加水印源码
- 微服务原生案例搭建
- 2022-07-25 网工进阶(二十一)BGP-路由反射器、联盟、聚合
- Wovent Bio IPO: Annual revenue of 480 million pension fund is a shareholder
- 【论文笔记】MiniSeg: An Extremely Minimum Network for Efficient COVID-19 Segmentation
- 2022-07-29 网工进阶(二十二)BGP-其他特性(路由过滤、团体属性、认证、AS欺骗、对等体组、子路由器、路由最大接收数量)
- mysql的基本使用
- docker部署mysql并修改其占用内存大小
- ABC260 E - At Least One (Dual Pointer)
- SQL每日一练(牛客新题库)——第3天: 条件查询
猜你喜欢
随机推荐
redis主从同步方式(redis数据同步原理)
mysql查询两个字段值相同的记录
Koreographer Professional Edition丨一款Unity音游插件教程
ThreadLocal保存用户登录信息
性能优化——粒子优化笔记
只知道SQL数据库?又一国产数据库语言诞生了
tkinter-TinUI-xml实战(6)问卷
灵魂发问:MySQL是如何解决幻读的?
A Beginner's Guide to Performance Testing
MySQL中根据日期进行范围查询
HTB-Shocker
D - I Hate Non-integer Number(背包dp)
立新能源深交所上市:市值55亿 哈密国投与国有基金是股东
The role of the final keyword final and basic types, reference types
代理商替代义隆153 Aip4210
龙口联合化学通过注册:年营收5.5亿 李秀梅控制92.5%股权
有谁知道pg12.5版本的数据库驱动在哪里能找到么?
微服务原生案例搭建
SQL查询数据以及排序
Performance Optimization - Resource Optimization Notes









