gpt4 book ai didi

php - 如何在 Laravel 5.2 中删除重复的行

转载 作者:行者123 更新时间:2023-11-29 04:37:02 26 4
gpt4 key购买 nike

我尝试删除代码中的重复行:

$post = new posts();
$post->raw('DELETE b1 FROM baroot b1,baroot b2 WHERE b1.post_id = b2.post_id AND b1.id > b2.id');

我的模型代码是:

class posts extends Model
{
protected $table = 'baroot';

protected $fillable = [
'post_id', 'title', 'modify',
];
}

我尝试删除重复的:

$post = new posts();
$post->raw('DELETE b1 FROM posts b1,posts b2 WHERE b1.post_id = b2.post_id AND b1.id > b2.id');

但是没有删除任何重复的行,我知道 sql 查询是正确的,但不适用于我的 laravel 代码

最佳答案

来自“Running Raw SQL Queries”的文档

Running A Delete Statement

The delete method should be used to delete records from the database. Like update, the number of rows deleted will be returned:

$deleted = DB::delete('delete from users');

所以你必须执行

$deleted = DB::delete('DELETE b1 FROM baroot b1,baroot b2 WHERE b1.post_id = b2.post_id AND b1.id > b2.id');

关于php - 如何在 Laravel 5.2 中删除重复的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38692981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com