当前位置:网站首页>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'
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
边栏推荐
猜你喜欢
随机推荐
台积电认清了形势,新的建厂计划没有美国,中国芯片也得到重视
经纬信息IPO过会:年营收3.5亿 叶肖华控制46.3%股权
HTB-Shocker
大神们,ODPS用的是MySQL吗?
E - Red and Blue Graph (Combinatorics)
Qt实战案例(55)——利用QDir删除选定文件目录下的空文件夹
Amperon IPO meeting: annual revenue of 500 million Tongchuang Weiye and China Mobile Innovation are shareholders
免费使用高性能的GPU和TPU—谷歌Colab使用教程
The default database main key, foreign key, and the only key index
the direction i'm looking for
MySQL中的存储过程(详细篇)
【无标题】
win10+Qt5.15.2 realizes low-power bluetooth control
PAT 1167 Cartesian Tree(30)
openEuler 社区12位开发者荣获年度开源贡献之星
可观测性就是对“监控”的包装?
1161. 最大层内元素和
微信UI在线聊天源码 聊天系统PHP采用 PHP 编写的聊天软件,简直就是一个完整的迷你版微信
重磅!国内首个开放式在线绘图平台Figdraw突破10万用户!发布《奖学金激励计划》:最高5000元!...
Pytorch —— 分布式模型训练