这篇文章主要介绍“MySQL层比较函数的调用方式”,在日常操作中,相信很多人在MySQL层比较函数的调用方式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”MySQL层比较函数的调用方式”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
最近遇到一个日期比较的问题如下:
root@localhost:test:10:25:48>select from_unixtime(1596680255);
+---------------------------+
| from_unixtime(1596680255) |
+---------------------------+
| 2020-08-06 10:17:35 |
+---------------------------+
1 row in set (0.00 sec)
root@localhost:test:10:25:52>select from_unixtime(1596680255,'%Y-%m-%d') < '2020-08-1';
+----------------------------------------------------+
| from_unixtime(1596680255,'%Y-%m-%d') < '2020-08-1' |
+----------------------------------------------------+
| 1 |
+----------------------------------------------------+
1 row in set (2.70 sec)
root@localhost:test:10:25:59>select from_unixtime(1596680255) < '2020-08-1';
+-----------------------------------------+
| from_unixtime(1596680255) < '2020-08-1' |
+-----------------------------------------+
| 0 |
+-----------------------------------------+
1 row in set (2.36 sec)
按理来说from_unixtime(1596680255,’%Y-%m-%d’) < ‘2020-08-1’ 应该是false才对,但是返回为true,因此怀疑为字符串比较的方式
首先关闭gtid通过create table as 来验证一下字段类型如下:
create table testit951
as
select from_unixtime(1596678161,'%Y-%m-%d') as "tt"
create table testit952
as
select from_unixtime(1596678161) as "tt"
通过这种方式发现两种建表字段为varchar和datetime类型。通过源码验证可以看到如下:
Breakpoint 5, Arg_comparator::compare_string (this=0x7fffe40167c0) at /cdh/mysqldebug/percona-server-5.7.29-32/sql/item_cmpfunc.cc:1672
1672 if ((res1= (*a)->val_str(&value1)))
(gdb) c
Continuing.
Breakpoint 4, Arg_comparator::compare_datetime (this=0x7fffe40164c8) at /cdh/mysqldebug/percona-server-5.7.29-32/sql/item_cmpfunc.cc:1509
1509 THD *thd= current_thd;
(gdb) c
Continuing.
可以看到内部通过了string和datetime两种类型来比较。因此得到证明。最后留下比较函数的位置:
Item_cmpfunc.cc
到此,关于“MySQL层比较函数的调用方式”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:http://blog.itpub.net/7728585/viewspace-2709839/