本文介绍了Laravel 4.2执行查询而不使用特征查询构建器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个查询与子查询..我想写一个明确的SQL请求并执行它查询: 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));laravel/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 subi 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"));laravel/docs/4.2/queries#raw-expressions
更多推荐
Laravel 4.2执行查询而不使用特征查询构建器
发布评论