我尝试使用以下代码删除代码中的重复行:
$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代码
答案 0 :(得分:4)
来自" Running Raw SQL Queries"
的文档运行删除声明
删除方法应该用于从数据库中删除记录。 与更新一样,将返回已删除的行数:
$ deleted = DB :: delete('从用户中删除');
所以你必须执行
$deleted = DB::delete('DELETE b1 FROM baroot b1,baroot b2 WHERE b1.post_id = b2.post_id AND b1.id > b2.id');