Laravel 4.2执行查询而不使用特征查询构建器 [英] Laravel 4.2 execute query without using elequent query builder
本文介绍了Laravel 4.2执行查询而不使用特征查询构建器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
查询:
select count(*)as count from
(
SELECT DISTINCT t.article_id FROM
`articles`作为左连接`tags`作为t
on a .`id` = t.`article_id`
其中a.`flag` = 1
)as sub
我尝试执行请求而不构建查询:
DB :: connection() - > getPdo() - > exec($ sql);
但总是返回0!
解决方案
您可以使用 DB :: raw 的子查询。
方法DB :: raw()(不要忘记使用Illuminate\Support\Facades\DB)可以选择任何你想要的,并基本上编写原始SQL语句。
DB :: select(DB :: raw(select count(*)as count from
(
SELECT DISTINCT t。 article_id FROM
`articles`作为左连接`tags`作为t
on.`id` = t.`article_id`
其中a.`flag` = 1
)as));
https://laravel.com/docs/4.2/queries#raw-expressions
I have a query with sub-query .. and i want to write an explicite SQL request and execute it Query :
select count(*) as count from
(
SELECT DISTINCT t.article_id FROM
`articles` as a left join `tags` as t
on a.`id` = t.`article_id`
where a.`flag` = 1
) as sub
i tried this to execute the request without building the query :
DB::connection()->getPdo()->exec( $sql );
But it always return 0 !
解决方案
You can use sub query with DB::raw. A method DB::raw() (don’t forget to use Illuminate\Support\Facades\DB) allows you to select whatever you want and basically write raw SQL statements.
DB::select(DB::raw("select count(*) as count from
(
SELECT DISTINCT t.article_id FROM
`articles` as a left join `tags` as t
on a.`id` = t.`article_id`
where a.`flag` = 1
) as sub"));
https://laravel.com/docs/4.2/queries#raw-expressions
这篇关于Laravel 4.2执行查询而不使用特征查询构建器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文