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`)









share|improve this question























  • 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















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`)









share|improve this question























  • 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













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`)









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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













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.






share|improve this answer




















  • 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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.






share|improve this answer




















  • 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














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.






share|improve this answer




















  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo