MySQL to Laravel Query
select a.members, count() as teams from(SELECT count() as members FROM team_member_detail group by team_id) as a group by a.members
this query i have as converted to Laravel query:
$data = DB::query()->select(DB::raw('a.members, count() as teams'))->fromSub(function ($query) {
$query->select(DB::raw('count() as members'))->from('team_member_detail')
->groupBy('team_id');
}, 'a')->get();
Still i am getting error ?
group By of the sub query which is also result of group By
@eyantra you query looks overcomplicated. show your table and and expected result
i have team member details table, which contains
id | team_id | name ....
1 | 1001 |
2 | 1001 |
3 | 1002 |
4 | 1002 |
5 | 1002 |
Now select count(*) number_of_members from team_detail group by team_id gives me how many members in the team.
and
select a.members, count() as teams from(SELECT count() as members FROM team_detail group by team_id) as a group by a.members
gives me the team number with number of members.
members | teams
2 | 1
3 | 1
@eyantra
try this
DB::table("team_member_detail ) as a")
->select("a.members", "count () as teams from(select count() as members")
->groupBy("team_id")
->get();
Unknown column 'a.members'
@eyantra I still do not understand your idea, so I can suggest to wrap it entirely in query
dd(DB::select('your entire query'));
Yes, this i have already tried using DB::raw, not working.
Anyway i resolved this issue using laravel collection: countBy
Thank you !!
@eyantra not DB::raw, DB::select
dd(DB::select('select a.members, count() as teams from(SELECT count() as members FROM team_detail group by team_id) as a group by a.members'));
This gives, Syntax error or access violation
Please or to participate in this conversation.