1

I want some help with a WHERE clause $start date is set further up in the code, and the query works when running against SQL from within Laravel. I get an error saying that $startdate isn't set. I am not sure that I am even doing WHERE correctly.

$query = DB::query()
    ->select('CC AS CountOfVisits', DB::raw('count(CC) AS Users'))
    ->fromSub
    (function (\Illuminate\Database\Query\Builder $query) {
            $query->select('user_id', DB::raw('count(user_id) AS CC '))
                ->from('mytable')
                ->where('created_at', '>=', $startdate)
                ->groupBy('user_id');

        }, 'CC')
    ->groupBy('CC');

$result = DB::connection('mysql2')->select($query->toSql());
3
  • I think you need to show more code to see where $startdate is declared. Commented Oct 17, 2019 at 23:45
  • If it was me, I'd start with the sql. Commented Oct 18, 2019 at 0:01
  • Thanks the SQL works SELECT CC AS CountOfVisits, Count(CC) AS Users FROM ( SELECT user_id, count(user_id) AS CC FROM mytable WHERE created_at <= '2019-10-29' AND created_at >= '2019-10-01' GROUP BY user_id ) AS CC GROUP BY CC; the issue is getting the variable into the function. I tried function (\Illuminate\Database\Query\Builder $query) use($startdate) { but that didn't work either Commented Oct 18, 2019 at 2:21

2 Answers 2

1

I'm putting this here for others who find this page. It should be noted that fromSub() can also take a Builder object instead of a closure.

$builder = DB::table('mytable')->where('created_at', '>=', $start_date)
   ->select('user_id', DB::raw('count(user_id) AS CC '))
   ->groupBy('user_id');

$query = DB::query()
    ->select('CC AS CountOfVisits', DB::raw('count(CC) AS Users'))
    ->fromSub($builder, 'temp')
    ->groupBy('CC');

In my case, I already had a Builder object that I wanted to use in a sub select.

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

Comments

0

That's because your fromSub is an anonymous function, also known as a closures. It does not know about variables that you define outside of the closure. You can read more about them in the official PHP docs

Rewrite your query to:

$result = DB::connection('mysql2')
    ->query()
    ->select('CC AS CountOfVisits', DB::raw('count(CC) AS Users'))
    ->fromSub(function($query) use ($startdate) {
        $query->select('user_id', DB::raw('count(user_id) AS CC '))
            ->from('mytable')
            ->where('created_at','>=',$startdate)
            ->groupBy('user_id');
        }, 'temp')
    ->groupBy('CC')
    ->get();

13 Comments

Thanks for the input, i tried this and it returns SQLSTATE[HY000]: General error: 2031
well that’s a different error from your original post. if you need help with your query you’ll have to give us more info and paste more code as you only pasted partial code. also please post the full error that you are getting not just the code
Error: Illuminate \ Database \ QueryException (HY000) SQLSTATE[HY000]: General error: 2031 (SQL: select `CC` as `CountOfVisits`, count(CC) AS Users from (select `user_id`, count(user_id) AS CC from `mytable` where `created_at` >= ? group by `user_id`) as `CC` group by `CC`)
$startdate = $request->startdate; $query = DB::query() ->select('CC AS CountOfVisits', DB::raw('count(CC) AS Users')) ->fromSub ( function (\Illuminate\Database\Query\Builder $query) use ($startdate) { $query->select('user_id', DB::raw('count(user_id) AS CC ')) ->from('establishment_checkins') ->where('created_at','>=',$startdate) ->groupBy('user_id'); }, 'CC') ->groupBy('CC');
$startdate = '2019-10-17'
|

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.