当前位置:网站首页>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'
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'
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
边栏推荐
猜你喜欢
随机推荐
UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu
Keil development environment installation tutorial
bgp机房的动态路由和静态路由的区别
Dynamic proxy tool class
磷脂-聚乙二醇-靶向新生血管靶向肽APRPG,DSPE-PEG-APRPG
np.isnan()
np.unique() function
OD-Model [4]: SSD
Cloud server installation and deployment of Nacos 2.0.4 version
Day34 LeetCode
Redis安装,基本命令,持久化方式,集群
2022.7.30 js notes Operators and flow controllers, loops
5.nodejs--cross domain, CORS, JSONP, Proxy
代码随想录笔记_哈希_383赎金信
TRICK second bullet
Knowledge Engineering Assignment 2: Introduction to Knowledge Engineering Related Fields
(Repost) HashCode Summary (1)
day11--shell脚本
LeetCode:第304场周赛【总结】
API 低代码开发:接口大师,一套开发、管理和提供接口的产品框架