0

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 !

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you try with DB::raw('your sql query here')

Comments

0

Using DB::select should solve your problem.

DB::select('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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.