doudi5524 2018-08-28 08:39
浏览 162
已采纳

Laravel活动记录中的Mysql查询

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

  • 写回答

2条回答 默认 最新

  • duan2477 2018-08-28 09:04
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程