Laravel 4.2执行查询而不使用特征查询构建器

编程入门 行业动态 更新时间:2024-10-27 19:21:06
本文介绍了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 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"));

laravel/docs/4.2/queries#raw-expressions

更多推荐

Laravel 4.2执行查询而不使用特征查询构建器

本文发布于:2023-05-31 22:07:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/400274.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   特征   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!