Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel将循环查询转换为单个准备好的查询_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php Laravel将循环查询转换为单个准备好的查询

Php Laravel将循环查询转换为单个准备好的查询,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,需要一些帮助将下面的Laravel Eloquent for loop查询转换为一个MySQL查询,该查询一次访问数据库。基本上,这会根据$cardQueryList中有多少项来命中数据库n次 for($i=0; $i<count($cardQueryList); $i++) { $userCard = Collection::firstOrNew( ['username' => $cardQueryList[$i]['username'], 'card_uid' =

需要一些帮助将下面的Laravel Eloquent for loop查询转换为一个MySQL查询,该查询一次访问数据库。基本上,这会根据$cardQueryList中有多少项来命中数据库n次

for($i=0; $i<count($cardQueryList); $i++) {
        $userCard = Collection::firstOrNew( ['username' => $cardQueryList[$i]['username'], 'card_uid' => $cardQueryList[$i]['card_uid']] );
        $userCard->have_quantity = $cardQueryList[$i]['have_quantity'];
        $userCard->save();
}
问题是我同时使用“username”和“card_uid”作为唯一键来查找要更新的对应行。另一个问题是,这要求我为所有字段指定所有值,否则字段将丢失或替换为默认值


如何让原始MySQL查询只命中数据库一次,而不是像Laravel雄辩的代码中那样命中n次?或者有没有一种方法可以只使用雄辩(这将是最好的,但我不认为有方法)来点击数据库一次?

编辑根据您的评论,您可以使用以下方法使用
DB
进行大规模更新:

DB::table((new Collection)->getTable())->update([
    [
        'field' => 'value',
        'field2' => 'value 2',
    ],
    [
        'field' => 'value',
        'field2' => 'value 3',
    ],
])
->where('some_id', $id);

您真的在寻找一个原始SQL吗,或者您可以简单地按照以下思路做一些事情:

foreach ($cardQueryList as $cardQuery) {
    $userCard = Collection::where('username', $cardQuery['username'])
        ->where('card_uid', $cardQuery['card_uid'])
        ->firstOrNew();

    $userCard->have_quantity = $cardQuery['have_quantity'];
    $userCard->save();
}

编辑根据您的评论,您可以使用以下方法使用
DB
进行批量更新:

DB::table((new Collection)->getTable())->update([
    [
        'field' => 'value',
        'field2' => 'value 2',
    ],
    [
        'field' => 'value',
        'field2' => 'value 3',
    ],
])
->where('some_id', $id);

您真的在寻找一个原始SQL吗,或者您可以简单地按照以下思路做一些事情:

foreach ($cardQueryList as $cardQuery) {
    $userCard = Collection::where('username', $cardQuery['username'])
        ->where('card_uid', $cardQuery['card_uid'])
        ->firstOrNew();

    $userCard->have_quantity = $cardQuery['have_quantity'];
    $userCard->save();
}

我就是这样解决的

$last = count($cardQueryList)-1;

$sql = 'INSERT INTO users_collection (username, card_uid, have_quantity) VALUES ';
for($i=0; $i<$last; $i++) {
   $sql .= '("'. $cardQueryList[$i]['username'] .'", "'. $cardQueryList[$i]['card_uid'] .'", '. $cardQueryList[$i]['have_quantity'] .'), ';
}
$sql .= '("'. $cardQueryList[$last]['username'] .'", "'. $cardQueryList[$last]['card_uid'] .'", '. $cardQueryList[$last]['have_quantity'] .') ';
$sql .= 'ON DUPLICATE KEY UPDATE ';
for($i=0; $i<$last; $i++) {
    $sql .= 'have_quantity = '. $cardQueryList[$i]['have_quantity'] .', ';
}
$sql .= 'have_quantity = '. $cardQueryList[$last]['have_quantity'];

DB::statement($sql);
$last=count($cardQueryList)-1;
$sql='插入用户集合(用户名、卡片uid、数量)值';

对于($i=0;$i我就是这样解决的

$last = count($cardQueryList)-1;

$sql = 'INSERT INTO users_collection (username, card_uid, have_quantity) VALUES ';
for($i=0; $i<$last; $i++) {
   $sql .= '("'. $cardQueryList[$i]['username'] .'", "'. $cardQueryList[$i]['card_uid'] .'", '. $cardQueryList[$i]['have_quantity'] .'), ';
}
$sql .= '("'. $cardQueryList[$last]['username'] .'", "'. $cardQueryList[$last]['card_uid'] .'", '. $cardQueryList[$last]['have_quantity'] .') ';
$sql .= 'ON DUPLICATE KEY UPDATE ';
for($i=0; $i<$last; $i++) {
    $sql .= 'have_quantity = '. $cardQueryList[$i]['have_quantity'] .', ';
}
$sql .= 'have_quantity = '. $cardQueryList[$last]['have_quantity'];

DB::statement($sql);
$last=count($cardQueryList)-1;
$sql='插入用户集合(用户名、卡片uid、数量)值';

对于($i=0;$Ith)这并不能解决根据cardQueryList的项数循环n次的基本问题。是的,
foreach
将迭代
$cardQueryList
数组中的项数,将
$cardQuery
作为其中的每个项。它与for循环相同,但更干净rence这里是我如何使用
where
作为条件来查询您想要的记录,并在该记录不存在时创建它。不,它不能解决我要问的问题。每次这一行发生时,$userCard->save();它命中数据库。您的代码命中数据库的次数与我的完全相同。问题是我如何将单个查询发送到数据库一次并进行更新(或插入)在$cardQueryList中包含所有对应项的数据库。啊,有趣的是,如果该行不存在,会插入吗?遗憾的是,不会,您将不得不沿着替换到路径。不过,我认为没有必要。记住,替换到不会更新,而是使用更新的数据创建新记录。我会选择后一个路径。如果您非常关心性能,那么ORM是您最不希望使用的。这并不能解决根据cardQueryList的项数循环n次的基本问题。是的,它确实存在,
foreach
将迭代
$cardQueryList
数组中的项数,给出
$cardQuery
>作为其中的每个项。它与for循环相同,但更干净。这里的区别是我如何使用
where
作为条件来查询您想要的记录,并在记录不存在时创建它。不,它不能解决我要问的问题。每次出现这一行,$userCard->save();它命中数据库。您的代码命中数据库的次数与我的完全相同。问题是我如何将单个查询发送到数据库一次并进行更新(或插入)在$cardQueryList中包含所有对应项的数据库。啊,有趣的是,如果该行不存在,会插入吗?遗憾的是,不会,您将不得不沿着替换到路径。不过,我认为没有必要。记住,替换到不会更新,而是使用更新的数据创建新记录。我会选择后一个路径.如果你非常关心性能,那么ORM是你最不想使用的。