简体   繁体   中英

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:

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.

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.

this works okay, But I want to know two things.

1: is this query a good approach for this problem or its slow?

2: how can I do this query in laravel 5.5 active records or query builder? 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

1: is this query a good approach for this problem or its slow?

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 -

$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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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