在 Laravel 中运行 SQL 删除查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45401442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:24:19  来源:igfitidea点击:

Running a SQL delete query in Laravel

phpmysqllaravel

提问by D. 777KLM

I have an SQL query:

我有一个 SQL 查询:

DELETE n1 
  FROM satellites n1
     , satellites n2 
 WHERE n1.id < n2.id 
   AND n1.norad_cat_id = n2.norad_cat_id

What this query does is delete rows that have the same norad_cat_idand only leave one with the highest id. I don't know if my SQL query is correct, but I will have to see.

这个查询的作用是删除具有相同的行,norad_cat_id只留下最高的行id。我不知道我的 SQL 查询是否正确,但我必须看看。

I am a bit stuck when it comes to running raw SQL queries in Laravel. From this documentation (https://laravel.com/docs/5.4/database#running-queries) you can see that you have a few options to run the query:

在 Laravel 中运行原始 SQL 查询时,我有点卡住了。从本文档 ( https://laravel.com/docs/5.4/database#running-queries) 中,您可以看到有几个选项可以运行查询:

DB::update('SQL QUERY HERE');

DB::update('SQL QUERY HERE');

DB::delete('SQL QUERY HERE');

DB::delete('SQL QUERY HERE');

DB::statement('SQL QUERY HERE');

DB::statement('SQL QUERY HERE');

DB::select( DB::raw('SQL QUERY HERE'));

DB::select( DB::raw('SQL QUERY HERE'));

In my case I am trying to delete duplicate rows while only leaving the one with the highest id. What Laravel DBstatement do I run to achieve the results I want or does it matter at all?

就我而言,我试图删除重复的行,而只留下id. DB我运行什么 Laravel语句来实现我想要的结果或者它根本不重要?

EDIT:SQL query for @MasudMiah

编辑:@MasudMiah 的 SQL 查询

delete satellites from satellites inner join ( select max(id) as lastId, norad_cat_id from satellites group by norad_cat_id having count(*) > 1) duplic on duplic.norad_cat_id = satellites.norad_cat_id where satellites.norad_cat_id < duplic.lastId;

delete satellites from satellites inner join ( select max(id) as lastId, norad_cat_id from satellites group by norad_cat_id having count(*) > 1) duplic on duplic.norad_cat_id = satellites.norad_cat_id where satellites.norad_cat_id < duplic.lastId;

回答by CoursesWeb

If you want to run directly your DELETE SQL query, you can use:

如果要直接运行 DELETE SQL 查询,可以使用:

$nrd = DB::delete('SQL QUERY HERE');

It returns the number of affected /deleted rows. See this page:

它返回受影响/删除的行数。请参阅此页面:

http://coursesweb.net/laravel/working-mysql-database#anc_rsq

http://coursesweb.net/laravel/working-mysql-database#anc_rsq

回答by Masud Miah

I am afraid your query is not right though. but let me show you some :

恐怕您的查询是不正确的。但让我给你看一些:

  DELETE FROM table1 WHERE user_id='$your_provided_value';
    DELETE FROM table2 WHERE user_id='$your_provided_value';

Now using query builder for laravel :

现在为 laravel 使用查询构建器:

DB::table('table_name')
->where('id',$your_provided_value)
->delete();

One thing I would like to mention to set multiple conditions like id = 1 AND gender = 'male' you need to something like that

我想提到的一件事是设置多个条件,例如 id = 1 AND性别 = 'male' 你需要类似的东西

DB::table('table_name')->where('id',1)
->where('gendar','male')
->delete();

Now by eloquent :

现在通过雄辩:

User:where('id', 1)->delete();

here User is your model for the users table. Hope you are getting some basics. by visiting below link you get the idea of using eloquent. https://scotch.io/tutorials/a-guide-to-using-eloquent-orm-in-laravel

这里 User 是您的用户表模型。希望你能掌握一些基础知识。通过访问以下链接,您将了解使用 eloquent 的想法。 https://scotch.io/tutorials/a-guide-to-using-eloquent-orm-in-laravel