0

I have here a laravel query:

DB::table('notifications')
    ->leftJoin('domains', 'domains.domain', '=', 'notifications.data->via_domain')
    ->select('notifications.data->via_domain as domain_name')
    ->groupBy('notifications.data->via_domain')
    ->get();

This shows domain names that exists between the tables 'notifications' and 'domains'. However, I want to count notifications.notifiable_id field in 'notifications' by:

DB::table('notifications')
    ->leftJoin('domains', 'domains.domain', '=', 'notifications.data->via_domain')
    ->selectRaw('count(notifications.notifiable_id), notifications.data->via_domain')
    ->get();

However it shows error in SQL syntax.

(You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

via_domain from notifications left join domains on
domains.domain = not at line 1 (SQL: select
count(notifications.notifiable_id), notifications.data->via_domain
from notifications left join domains on domains.domain =
notifications.data->'$."via_domain"')')

Thanks for the help, minna-san.

5
  • is data->via_domain actually within the table or is this a variable? Can you post the two tables you're joining Commented Oct 24, 2017 at 6:56
  • Yes, it is a json field, and it already shows data. Commented Oct 24, 2017 at 6:58
  • DOMAINS TABLE id, int(10) unsigned, NO, PRI, , auto_increment domain, varchar(255), NO, UNI, , description, varchar(255), YES, , , country_id, varchar(255), YES, , , settings, json, NO, , , captor_id, int(11), YES, , , account_id, int(11), NO, , , owner_email, varchar(255), YES, , , owner_phone, varchar(255), YES, , , created_at, timestamp, YES, , , updated_at, timestamp, YES, , , Commented Oct 24, 2017 at 7:02
  • NOTIFICATIONS TABLE Commented Oct 24, 2017 at 7:03
  • 'updated_at', 'timestamp', 'YES', '', NULL, '' 'type', 'varchar(255)', 'NO', '', NULL, '' 'read_at', 'timestamp', 'YES', '', NULL, '' 'notifiable_type', 'varchar(255)', 'NO', '', NULL, '' 'notifiable_id', 'int(10) unsigned', 'NO', 'MUL', NULL, '' 'id', 'char(36)', 'NO', 'PRI', NULL, '' 'data', 'text', 'NO', '', NULL, '' 'created_at', 'timestamp', 'YES', '', NULL, '' Commented Oct 24, 2017 at 7:03

2 Answers 2

0

I already got it!

Here's my code:

DB::table('notifications')->leftJoin('domains', 'domains.domain', '=', 'notifications.data->via_domain')
                    ->select('notifications.data->via_domain as domainName', DB::raw('COUNT(notifications.notifiable_id) AS leadsCount'))
                    ->groupBy('notifications.data->via_domain')
                    ->get();

This results to an array showing the domain name and its leadsCount.

Thanks everyone.

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

Comments

0

for example, assume we have data as like below

enter image description here

Couponview::whereBetween('created_at', [$start_date, $end_date])
                ->select('store_id', DB::raw('COUNT(store_id) as store_count'))
                ->groupBy('store_id')
                ->orderBy(DB::raw('COUNT(store_id)'), 'DESC')
                ->take(8)
                ->get();

then We got OutPut Like below example image enter image description here

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.