php - laravel中有没有有效的方法从数据库中选择记录并设置它们user_id?

标签 php mysql sql laravel

我必须制作使用数据库 100k+ 记录的 Laravel 应用程序。我的表格包含以下列:numerdatesegmentuser_id

我正在选择我需要的记录:

$datetime = new \DateTime();
$datetime->modify('+6 months');

$seg = "biz";

$select = DB::table('clients')
            ->where('user_id', $id)
            ->where('date','<',$datetime)
            ->where('segment',$seg)
            ->get();

我得到了一些记录,现在,是否有为 select 中的 100 条记录设置 user_id

主要规则是我无法对数据库中的日期进行排序。

最佳答案

在选择要更新的给定数量的行后,您可以批量更新行:

$datetime = new \DateTime();
$datetime->modify('+6 months');

$seg = "biz";

$new_id = 743; // <-------- example of value to be updated

$updated_rows = DB::table('clients')
                  ->where('user_id', $id)
                  ->where('date','<', $datetime)
                  ->where('segment', $seg)
                  ->take(100)                       // <---- # records to be updated
                  ->update(['user_id' => $new_id]); // <---- new value(s) to update

检查the documentation与本主题相关(它面向 Eloquent,但也适用于 Laravel Query Builder)。

PS:$updated_rows是已更新的行数。

关于php - laravel中有没有有效的方法从数据库中选择记录并设置它们user_id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56010632/

相关文章:

php - 提交按钮不适用于 fancybox

MySQL 排序 : NULL to the end & use index? 不可能?

php - Yii 中的 AR 添加重复行

php - 检查数字是否以 0 或 00 开头

mysql - 如何使用 Sequelize 在我的时区存储正确的日期时间

php - 选择故事及其评论和回复,并在一个查询中检查当前用户是否喜欢

SQL Server - 如何在我的表上查找依赖表?

sql - azure 上托管的 .net 应用程序停止工作并发生异常 [Win32Exception (0x80004005) : Access is denied] [SqlException (0x80131904):

sql - 是否可以在选择查询中使用同一个表两次?

php - 优化此 SQL 查询