【发布时间】:2020-11-20 06:49:30
【问题描述】:
我有一个使用 Laravel 查询生成器获取数据的 API。我从一个反应应用程序调用这个 API。我的数据库中每 5 分钟就有一个新行,我需要能够检索一天或一段时间(如一两周)的数据。 进行此调用会使我的反应应用程序非常慢,即使我只获取一天的数据。我需要能够选择每第 n 行以提高性能(以及我根据数据创建的图表的可读性......)。
而不是检索每条记录(每 5 分钟),例如,我希望能够每 12 检索 1 条记录(每 60 分钟)
时间(我的数据库中的log_time)以一种奇怪的方式存储:103500 是上午 10:35。想在我的查询中使用模数,但我不能让它工作......
$data = DB::connection('mysql')->table('my_table')
->select('value','log_date','log_time')
->where('id_user',$userId)
->where('log_time', mod 500 = 0) // This is the line I cannot get to work
->orderBy('log_date')
->orderBy('log_time')->get();
我为那条线尝试了不同的东西。有些是绝望的尝试......
->where('log_time', mod 500 = 0) // unexpected '500', expecting ')'
->where('log_time mod 500 = 0') // Unknown column 'log_time mod 500 = 0'
->where('log_time mod 500', 0) // Unknown column 'log_time mod 500'
->where('log_time', % 500 = 0) // unexpected '%', expecting ')'
->where('log_time % 500 = 0') // Unknown column 'log_time % 500 = 0'
->where('log_time % 500', 0) // Unknown column 'log_time % 500'
->where('log_time', mod 500, 0) // unexpected '500', expecting ')'
->where('log_time', 'mod 500', 0) // only return result for 'log_time' = 0 (midnight)
->where('log_time', % 500, 0) // unexpected '500', expecting ')'
->where('log_time', '% 500', 0) // only return result for 'log_time' = 0 (midnight)
->where('log_time', MOD(500,0)) // Call to undefined function MOD()
我也尝试了->whereRaw,但也没有用。
【问题讨论】:
-
这能回答你的问题吗? Return the nth record from MySQL query
-
这允许我检索第 n 条记录,而不是每条第 n 条记录。如果 n = 5,我只会检索第五个元素。我需要每 5 次检索 1 行(所以第 5、10、15、20、25 行...)。
标签: mysql reactjs laravel laravel-query-builder