简体   繁体   English

Laravel活动记录中的MySQL查询

[英]Mysql Query in Laravel active records

after a little bit search in stackoverflow I came up with this query which I wanted, and its like this: 在stackoverflow中进行了一点搜索之后,我想到了这个查询,它是这样的:

SELECT * FROM workers where TIMESTAMPDIFF(SECOND, last_activity_time, NOW()) >= (60/per_hour*60)

each record is consist of these fields: 每个记录都包含以下字段:

id   worker_name   last_activity_time   per_hour

so each worker has a per_hour field that will determined as actions number per hour. 因此,每个工作人员都有一个per_hour字段,该字段将确定为每小时的操作次数。

last activity is the last time worker was doing an action. 上一次活动是工人上一次执行操作的时间。 It will get records that are qualified to run at current time. 它将获得有资格在当前时间运行的记录。

so it will determine time interval with 60/per_hour in seconds and selects the records which time passed from their last_activity_time till now is more than this calculated interval. 因此它将确定以60/per_hour为单位的时间间隔(以秒为单位),并选择从其last_activity_time到现在为止经过的时间大于此计算间隔的记录。

this works okay, But I want to know two things. 可以,但是我想知道两件事。

1: is this query a good approach for this problem or its slow? 1:此查询是解决此问题的好方法还是速度慢?

2: how can I do this query in laravel 5.5 active records or query builder? 2:如何在laravel 5.5活动记录或查询生成器中执行此查询? and also it should return one record at a time. 并且它应该一次返回一个记录。

thanks in advance 提前致谢

this should work 这应该工作

\DB::table('workers')->whereRaw(...)->first();  

https://laravel.com/docs/5.6/queries https://laravel.com/docs/5.6/queries

1: is this query a good approach for this problem or its slow? 1:此查询是解决此问题的好方法还是速度慢?

It will depend on how many workers you have, you should try and see 这将取决于您有多少工人,您应该尝试看看

i think your query is fine because there were no joins and no subquerires just only condition is there. 我认为您的查询很好,因为没有联接,也没有子查询,仅存在条件。 You can fire raw queries on laravel to - 您可以在laravel上触发原始查询,以-

$workers = DB::select('SELECT * FROM workers where TIMESTAMPDIFF(SECOND, last_activity_time, NOW()) >= (60/per_hour*60)');
// or you can make use of query builder as follows
$workers = DB::table('workers')->whereRaw('TIMESTAMPDIFF(SECOND, last_activity_time, NOW()) >= (60/per_hour*60')->first();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM