0

how to query like

SELECT count(DISTINCT a.client_invoice_id) from client_invoice a LEFT JOIN document b on(a.client_invoice_id = b.relation_id_invoice) WHERE a.client_invoice_id IS NOT null and b.type_document = 'client_invoice'

in laravel version?

1
  • This is an inner join. If you like, you can prove this by running EXPLAIN EXTENDED.... followed by SHOW WARNINGS; Commented Mar 7, 2017 at 8:07

2 Answers 2

2
$result = DB::table('client_invoice as a')
->leftjoin('document as b', 'a.client_invoice_id', '=', 'b.relation_id_invoice')
->whereNotNull('a.client_invoice_id')
->where('b.type_document', '=', 'client_invoice')
->distinct()
->count();
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks bro,but how query count(distinct) from those query ?
i have eror --SQLSTATE[42S22]: Column not found: 1054 Unknown column 'a.client_invoice' in 'where clause' (SQL: select distinct * from client_invoice as a left join document as b on a.client_invoice_id = b.relation_id_document where a.client_invoice is not null and b.type_document = client_invoice)-- did you know,bro ?
Check my update. add ->distinc() before ->count();
Sorry, replace a.client_invoice by a.client_invoice_id. Just add id at the end onwhereNotNull()
1

Laravel left join:

$users = DB::table('users')
            ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
            ->get();

Or you can use Raw Expression also like:

$users = DB::table('users')
                     ->select(DB::raw('count(*) as user_count, status'))
                     ->where('status', '<>', 1)
                     ->groupBy('status')
                     ->get();

Laravel Left Join

Laravel Raw Expression

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.