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

标签 php mysql laravel laravel-5.1 laravel-5

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

$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/

相关文章:

php - 如何在 Laravel 中执行 MYSQL 查询?

php mysql 条件查询 AND OR

php - View 中 undefined variable taskid

laravel - laravel 中制作 Controller 时出错

Laravel 返回 "Object"而不是数组?

php - 如何使用 PHP 发送文本消息/SMS?

javascript - 禁用输入日期但检索值

mysql - 在 SQL 查询中分组

php - 如何隐藏数据库数据但将其显示为交互式可视化?

php - Laravel Gmail 不工作, "Username and Password not accepted. Learn more..."