How to execute MYSQL query in laravel?

爱⌒轻易说出口 提交于 2019-12-11 11:22:49

问题


I have one MYSQL query with me I want to execute this query in 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');

回答1:


change the 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');



回答2:


You can just do a RAW query. like this:

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

the $result will be an array




回答3:


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

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



来源:https://stackoverflow.com/questions/36331289/how-to-execute-mysql-query-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!