如何在 Laravel 中执行 MYSQL 查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36331289/
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 13:30:18  来源:igfitidea点击:

How to execute MYSQL query in laravel?

phpmysqllaravellaravel-4laravel-5

提问by Makerz

I have one MYSQL query with me I want to execute this query in laravel.

我有一个 MYSQL 查询,我想在 laravel 中执行这个查询。

    select d1.update_id from ( select update_id, count(update_id)
 as ct from updates_tags where tag_id in 
(67,33,86,55) group by update_id) as d1 where d1.ct=4

Please guide me how do i Do it easily.

请指导我如何轻松做到。

I have one reference with me.... It is not working

我有一个参考....它不起作用

    $updateArray = DB::table('updates_tags')
->select('update_id', DB::raw('count(*) as update_id'))
->whereIn('tag_id',$jsontags)
->groupBy('update_id')
->having('update_id','=',count($jsontags))
->lists('update_id');

回答by Tomer Ofer

You can just do a RAW query. like this:

您可以只执行 RAW 查询。像这样:

$sqlQuery = "SELECT d1.update_id FROM...";
$result = DB::select(DB::raw($sqlQuery));

the $result will be an array

$result 将是一个数组

回答by oseintow

change the where('total','=',count($tags)) to having('total','=',count($tags))

改变 where('total','=',count($tags)) to having('total','=',count($tags))

$update = DB::table('tags')
->select('t_id', 'u_id', DB::raw('count(*) as total'))
->whereIn('t_id',$tags)
->groupBy('u_id')
->having('total','=',count($tags))
->lists('u_id');

回答by Juanjo Salvador

In classic PHP, we used to use mysqliconnector. In CodeIgniter, we use ActiveRecords (fully lovely). In Laravel, there is also ActiveRecords for SQL queries.

在经典的 PHP 中,我们曾经使用过mysqli连接器。在 CodeIgniter 中,我们使用 ActiveRecords(非常可爱)。在 Laravel 中,还有用于 SQL 查询的 ActiveRecords。

Checkout the official Laravel docs, query builder is your friend. https://laravel.com/docs/4.2/queries

查看 Laravel 官方文档,查询构建器是您的朋友。https://laravel.com/docs/4.2/queries