Laravel query using group by and where does not work
up vote
0
down vote
favorite
This is a query that I have in raw sql.
DB::select('SELECT bldgs.name as building , floors.name as floor, areas.name as area, locations.area_id ,count(reqs.location_id) as occupied FROM `reqs` '
. 'JOIN locations ON locations.id = location_id '
. 'JOIN areas ON areas.id = area_id '
. 'JOIN floors ON floors.id = areas.floor_id'
. ' JOIN bldgs ON bldgs.id = bldg_id '
. 'WHERE `status`=2 and (DATE_FORMAT(start_date,"%Y-%m")<= "'.$dateFrom.'" AND DATE_FORMAT(end_date,"%Y-%m")>="'.$dateTo.'") group by locations.area_id, areas.name, floors.name, bldgs.name' );
And this is one of many attempts to make it work in Laravel elequent instead of raw.
Req::select('bldgs.name as building',DB::raw('count(location_id) as count_occupied'))
->join('locations','locations.id','=','location_id')
->join('areas','areas.id','=','locations.area_id')
->join('floors','floors.id','=','areas.floor_id')
->join('bldgs as bl','bl.id','=','floors.bldg_id')
->where('reqs.status','=', '2')
->where('start_date','<=', $date)
->where('end_date','>=', $date)
->groupBy('bldgs.name')
I need to understand why the second way gives mysql error and refuses to run the query above. Is is a mistake in my code or is this normally not possible in using eloguent to group by like this except in raw mysql string?
This is the error I get.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bldgs.name' in 'field list' (SQL: select `bldgs`.`name` as `building`, count(location_id) as count_occupied from `reqs` inner join `locations` on `locations`.`id` = `location_id` inner join `areas` on `areas`.`id` = `locations`.`area_id` inner join `floors` on `floors`.`id` = `areas`.`floor_id` inner join `bldgs` as `bl` on `bl`.`id` = `floors`.`bldg_id` where `reqs`.`status` = 2 and `start_date` <= 2018-10 and `end_date` >= 2018-10 group by `bldgs`.`name`)
laravel eloquent
|
show 1 more comment
up vote
0
down vote
favorite
This is a query that I have in raw sql.
DB::select('SELECT bldgs.name as building , floors.name as floor, areas.name as area, locations.area_id ,count(reqs.location_id) as occupied FROM `reqs` '
. 'JOIN locations ON locations.id = location_id '
. 'JOIN areas ON areas.id = area_id '
. 'JOIN floors ON floors.id = areas.floor_id'
. ' JOIN bldgs ON bldgs.id = bldg_id '
. 'WHERE `status`=2 and (DATE_FORMAT(start_date,"%Y-%m")<= "'.$dateFrom.'" AND DATE_FORMAT(end_date,"%Y-%m")>="'.$dateTo.'") group by locations.area_id, areas.name, floors.name, bldgs.name' );
And this is one of many attempts to make it work in Laravel elequent instead of raw.
Req::select('bldgs.name as building',DB::raw('count(location_id) as count_occupied'))
->join('locations','locations.id','=','location_id')
->join('areas','areas.id','=','locations.area_id')
->join('floors','floors.id','=','areas.floor_id')
->join('bldgs as bl','bl.id','=','floors.bldg_id')
->where('reqs.status','=', '2')
->where('start_date','<=', $date)
->where('end_date','>=', $date)
->groupBy('bldgs.name')
I need to understand why the second way gives mysql error and refuses to run the query above. Is is a mistake in my code or is this normally not possible in using eloguent to group by like this except in raw mysql string?
This is the error I get.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bldgs.name' in 'field list' (SQL: select `bldgs`.`name` as `building`, count(location_id) as count_occupied from `reqs` inner join `locations` on `locations`.`id` = `location_id` inner join `areas` on `areas`.`id` = `locations`.`area_id` inner join `floors` on `floors`.`id` = `areas`.`floor_id` inner join `bldgs` as `bl` on `bl`.`id` = `floors`.`bldg_id` where `reqs`.`status` = 2 and `start_date` <= 2018-10 and `end_date` >= 2018-10 group by `bldgs`.`name`)
laravel eloquent
call->toSql()
on your second query. It converts it into sql query then you can compare
– Hirad Roshandel
Nov 9 at 19:01
If you eliminate the groupBy clause at the end, does the query work?
– eResourcesInc
Nov 9 at 19:06
Include the SQL error in your problem, otherwise we're just guessing why the query won't run.
– Devon
Nov 9 at 19:19
@Devon Just included the error and it contains the sql generated.
– user6437700
Nov 9 at 19:30
@eResourcesInc Yes it runs without the group by, but I need to mention that it is extremely slow and consumes a ton of memory. I have to allocate more in php.ini. And it is not the query I want after all
– user6437700
Nov 9 at 19:39
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This is a query that I have in raw sql.
DB::select('SELECT bldgs.name as building , floors.name as floor, areas.name as area, locations.area_id ,count(reqs.location_id) as occupied FROM `reqs` '
. 'JOIN locations ON locations.id = location_id '
. 'JOIN areas ON areas.id = area_id '
. 'JOIN floors ON floors.id = areas.floor_id'
. ' JOIN bldgs ON bldgs.id = bldg_id '
. 'WHERE `status`=2 and (DATE_FORMAT(start_date,"%Y-%m")<= "'.$dateFrom.'" AND DATE_FORMAT(end_date,"%Y-%m")>="'.$dateTo.'") group by locations.area_id, areas.name, floors.name, bldgs.name' );
And this is one of many attempts to make it work in Laravel elequent instead of raw.
Req::select('bldgs.name as building',DB::raw('count(location_id) as count_occupied'))
->join('locations','locations.id','=','location_id')
->join('areas','areas.id','=','locations.area_id')
->join('floors','floors.id','=','areas.floor_id')
->join('bldgs as bl','bl.id','=','floors.bldg_id')
->where('reqs.status','=', '2')
->where('start_date','<=', $date)
->where('end_date','>=', $date)
->groupBy('bldgs.name')
I need to understand why the second way gives mysql error and refuses to run the query above. Is is a mistake in my code or is this normally not possible in using eloguent to group by like this except in raw mysql string?
This is the error I get.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bldgs.name' in 'field list' (SQL: select `bldgs`.`name` as `building`, count(location_id) as count_occupied from `reqs` inner join `locations` on `locations`.`id` = `location_id` inner join `areas` on `areas`.`id` = `locations`.`area_id` inner join `floors` on `floors`.`id` = `areas`.`floor_id` inner join `bldgs` as `bl` on `bl`.`id` = `floors`.`bldg_id` where `reqs`.`status` = 2 and `start_date` <= 2018-10 and `end_date` >= 2018-10 group by `bldgs`.`name`)
laravel eloquent
This is a query that I have in raw sql.
DB::select('SELECT bldgs.name as building , floors.name as floor, areas.name as area, locations.area_id ,count(reqs.location_id) as occupied FROM `reqs` '
. 'JOIN locations ON locations.id = location_id '
. 'JOIN areas ON areas.id = area_id '
. 'JOIN floors ON floors.id = areas.floor_id'
. ' JOIN bldgs ON bldgs.id = bldg_id '
. 'WHERE `status`=2 and (DATE_FORMAT(start_date,"%Y-%m")<= "'.$dateFrom.'" AND DATE_FORMAT(end_date,"%Y-%m")>="'.$dateTo.'") group by locations.area_id, areas.name, floors.name, bldgs.name' );
And this is one of many attempts to make it work in Laravel elequent instead of raw.
Req::select('bldgs.name as building',DB::raw('count(location_id) as count_occupied'))
->join('locations','locations.id','=','location_id')
->join('areas','areas.id','=','locations.area_id')
->join('floors','floors.id','=','areas.floor_id')
->join('bldgs as bl','bl.id','=','floors.bldg_id')
->where('reqs.status','=', '2')
->where('start_date','<=', $date)
->where('end_date','>=', $date)
->groupBy('bldgs.name')
I need to understand why the second way gives mysql error and refuses to run the query above. Is is a mistake in my code or is this normally not possible in using eloguent to group by like this except in raw mysql string?
This is the error I get.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bldgs.name' in 'field list' (SQL: select `bldgs`.`name` as `building`, count(location_id) as count_occupied from `reqs` inner join `locations` on `locations`.`id` = `location_id` inner join `areas` on `areas`.`id` = `locations`.`area_id` inner join `floors` on `floors`.`id` = `areas`.`floor_id` inner join `bldgs` as `bl` on `bl`.`id` = `floors`.`bldg_id` where `reqs`.`status` = 2 and `start_date` <= 2018-10 and `end_date` >= 2018-10 group by `bldgs`.`name`)
laravel eloquent
laravel eloquent
edited Nov 9 at 19:29
asked Nov 9 at 18:59
user6437700
458
458
call->toSql()
on your second query. It converts it into sql query then you can compare
– Hirad Roshandel
Nov 9 at 19:01
If you eliminate the groupBy clause at the end, does the query work?
– eResourcesInc
Nov 9 at 19:06
Include the SQL error in your problem, otherwise we're just guessing why the query won't run.
– Devon
Nov 9 at 19:19
@Devon Just included the error and it contains the sql generated.
– user6437700
Nov 9 at 19:30
@eResourcesInc Yes it runs without the group by, but I need to mention that it is extremely slow and consumes a ton of memory. I have to allocate more in php.ini. And it is not the query I want after all
– user6437700
Nov 9 at 19:39
|
show 1 more comment
call->toSql()
on your second query. It converts it into sql query then you can compare
– Hirad Roshandel
Nov 9 at 19:01
If you eliminate the groupBy clause at the end, does the query work?
– eResourcesInc
Nov 9 at 19:06
Include the SQL error in your problem, otherwise we're just guessing why the query won't run.
– Devon
Nov 9 at 19:19
@Devon Just included the error and it contains the sql generated.
– user6437700
Nov 9 at 19:30
@eResourcesInc Yes it runs without the group by, but I need to mention that it is extremely slow and consumes a ton of memory. I have to allocate more in php.ini. And it is not the query I want after all
– user6437700
Nov 9 at 19:39
call
->toSql()
on your second query. It converts it into sql query then you can compare– Hirad Roshandel
Nov 9 at 19:01
call
->toSql()
on your second query. It converts it into sql query then you can compare– Hirad Roshandel
Nov 9 at 19:01
If you eliminate the groupBy clause at the end, does the query work?
– eResourcesInc
Nov 9 at 19:06
If you eliminate the groupBy clause at the end, does the query work?
– eResourcesInc
Nov 9 at 19:06
Include the SQL error in your problem, otherwise we're just guessing why the query won't run.
– Devon
Nov 9 at 19:19
Include the SQL error in your problem, otherwise we're just guessing why the query won't run.
– Devon
Nov 9 at 19:19
@Devon Just included the error and it contains the sql generated.
– user6437700
Nov 9 at 19:30
@Devon Just included the error and it contains the sql generated.
– user6437700
Nov 9 at 19:30
@eResourcesInc Yes it runs without the group by, but I need to mention that it is extremely slow and consumes a ton of memory. I have to allocate more in php.ini. And it is not the query I want after all
– user6437700
Nov 9 at 19:39
@eResourcesInc Yes it runs without the group by, but I need to mention that it is extremely slow and consumes a ton of memory. I have to allocate more in php.ini. And it is not the query I want after all
– user6437700
Nov 9 at 19:39
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
As the error states, you don't have a bldgs.name
column. You named the bldgs
table bl
when you joined it.
Rename your references from bldgs.name
to bl.name
.
Not working with or without the alias. I get the same error. The error seems to be near the select itself.
– user6437700
Nov 9 at 19:47
Then you must not have a bldgs.name column... That's the only reason for this error. Post the updated error if you updated the references.
– Devon
Nov 9 at 19:48
The same field works for the raw query. I have posted it above in my question.
– user6437700
Nov 9 at 19:49
Ok, post the updated error.. otherwise I have nothing to go on. It doesn't make any sense so you must have done something wrong.
– Devon
Nov 9 at 19:50
1
you still have as bl...
– Devon
Nov 9 at 20:02
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
As the error states, you don't have a bldgs.name
column. You named the bldgs
table bl
when you joined it.
Rename your references from bldgs.name
to bl.name
.
Not working with or without the alias. I get the same error. The error seems to be near the select itself.
– user6437700
Nov 9 at 19:47
Then you must not have a bldgs.name column... That's the only reason for this error. Post the updated error if you updated the references.
– Devon
Nov 9 at 19:48
The same field works for the raw query. I have posted it above in my question.
– user6437700
Nov 9 at 19:49
Ok, post the updated error.. otherwise I have nothing to go on. It doesn't make any sense so you must have done something wrong.
– Devon
Nov 9 at 19:50
1
you still have as bl...
– Devon
Nov 9 at 20:02
|
show 2 more comments
up vote
0
down vote
accepted
As the error states, you don't have a bldgs.name
column. You named the bldgs
table bl
when you joined it.
Rename your references from bldgs.name
to bl.name
.
Not working with or without the alias. I get the same error. The error seems to be near the select itself.
– user6437700
Nov 9 at 19:47
Then you must not have a bldgs.name column... That's the only reason for this error. Post the updated error if you updated the references.
– Devon
Nov 9 at 19:48
The same field works for the raw query. I have posted it above in my question.
– user6437700
Nov 9 at 19:49
Ok, post the updated error.. otherwise I have nothing to go on. It doesn't make any sense so you must have done something wrong.
– Devon
Nov 9 at 19:50
1
you still have as bl...
– Devon
Nov 9 at 20:02
|
show 2 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
As the error states, you don't have a bldgs.name
column. You named the bldgs
table bl
when you joined it.
Rename your references from bldgs.name
to bl.name
.
As the error states, you don't have a bldgs.name
column. You named the bldgs
table bl
when you joined it.
Rename your references from bldgs.name
to bl.name
.
answered Nov 9 at 19:42
Devon
21.9k42645
21.9k42645
Not working with or without the alias. I get the same error. The error seems to be near the select itself.
– user6437700
Nov 9 at 19:47
Then you must not have a bldgs.name column... That's the only reason for this error. Post the updated error if you updated the references.
– Devon
Nov 9 at 19:48
The same field works for the raw query. I have posted it above in my question.
– user6437700
Nov 9 at 19:49
Ok, post the updated error.. otherwise I have nothing to go on. It doesn't make any sense so you must have done something wrong.
– Devon
Nov 9 at 19:50
1
you still have as bl...
– Devon
Nov 9 at 20:02
|
show 2 more comments
Not working with or without the alias. I get the same error. The error seems to be near the select itself.
– user6437700
Nov 9 at 19:47
Then you must not have a bldgs.name column... That's the only reason for this error. Post the updated error if you updated the references.
– Devon
Nov 9 at 19:48
The same field works for the raw query. I have posted it above in my question.
– user6437700
Nov 9 at 19:49
Ok, post the updated error.. otherwise I have nothing to go on. It doesn't make any sense so you must have done something wrong.
– Devon
Nov 9 at 19:50
1
you still have as bl...
– Devon
Nov 9 at 20:02
Not working with or without the alias. I get the same error. The error seems to be near the select itself.
– user6437700
Nov 9 at 19:47
Not working with or without the alias. I get the same error. The error seems to be near the select itself.
– user6437700
Nov 9 at 19:47
Then you must not have a bldgs.name column... That's the only reason for this error. Post the updated error if you updated the references.
– Devon
Nov 9 at 19:48
Then you must not have a bldgs.name column... That's the only reason for this error. Post the updated error if you updated the references.
– Devon
Nov 9 at 19:48
The same field works for the raw query. I have posted it above in my question.
– user6437700
Nov 9 at 19:49
The same field works for the raw query. I have posted it above in my question.
– user6437700
Nov 9 at 19:49
Ok, post the updated error.. otherwise I have nothing to go on. It doesn't make any sense so you must have done something wrong.
– Devon
Nov 9 at 19:50
Ok, post the updated error.. otherwise I have nothing to go on. It doesn't make any sense so you must have done something wrong.
– Devon
Nov 9 at 19:50
1
1
you still have as bl...
– Devon
Nov 9 at 20:02
you still have as bl...
– Devon
Nov 9 at 20:02
|
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231814%2flaravel-query-using-group-by-and-where-does-not-work%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
call
->toSql()
on your second query. It converts it into sql query then you can compare– Hirad Roshandel
Nov 9 at 19:01
If you eliminate the groupBy clause at the end, does the query work?
– eResourcesInc
Nov 9 at 19:06
Include the SQL error in your problem, otherwise we're just guessing why the query won't run.
– Devon
Nov 9 at 19:19
@Devon Just included the error and it contains the sql generated.
– user6437700
Nov 9 at 19:30
@eResourcesInc Yes it runs without the group by, but I need to mention that it is extremely slow and consumes a ton of memory. I have to allocate more in php.ini. And it is not the query I want after all
– user6437700
Nov 9 at 19:39