4
PHP系列 | TP6使用表达式设置数据 Db::raw('end_time') - Tinywan
source link: https://www.cnblogs.com/tinywan/p/16178669.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
TP6使用表达式设置数据 Db::raw('end_time')
模型或者Db无数据
使用以下语句查询记录
$liveList = Db::table('sys_live') ->field('id,begin_time,end_time,title') ->where('end_time','<>',0) ->where('begin_time','>','end_time') ->count();
查询结果为:0
通过fetchSql打印SQL语句
$liveList = Db::table('sys_live') ->field('id,begin_time,end_time,title') ->where('end_time','<>',0) ->where('begin_time','>','end_time') ->fetchSql() ->count();
打印SQL如下
SELECT COUNT(*) AS think_count FROM `sys_live` WHERE `end_time` <> 0 AND `begin_time` > end_time
注意:细心的朋友会发现开始时间是:`begin_time`(有双引号),而结束时间是:end_time(无双引号)
直接通过MySQL命令行,执行以上SQL语句是没问题的
mysql> SELECT COUNT(*) AS think_count FROM `sys_live` WHERE `end_time` <> 0 AND `begin_time` > end_time; +-------------+ | think_count | +-------------+ | 100 | +-------------+ 1 row in set (0.14 sec)
解决办法
查询条件使用表达式设置数据 Db::raw('end_time'),最后改写为以下查询
$liveList = Db::table('sys_live') ->field('id,begin_time,end_time,title') ->where('end_time','<>',0) ->where('begin_time','>',Db::raw('end_time')) ->count();
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK