DB::table('visitors')
->join('event_visitor', 'visitors.id', '=', 'event_visitor.visitor_id')->where('sex', 0)
->where('event_visitor.event_id', 1)
->count();
this is the query to get the count of men at the visitor’s table with an event id of 1
I want to get the record count of men women and kids in 10 events and formate it like this
$men = [100, 200, 300 ,400,500,600,700,800,900,1000];
$women = [100, 200, 300 ,400,500,600,700,800,900,1000];
$kids = [100, 200, 300 ,400,500,600,700,800,900,1000];
is there a way to do this in the database without getting into the n+1 problem?
0 = men
1 = women
2 = kids
2
Answers
the answer is going to be like this in laravel
Don’t know about laravel, but in MySQL you could write a query like
The numbers given after the
IN
keyword are ten sample event ids, for which you want to get a result. Note, that this counts visits not visitors. If you want to count distinct visitors, you cancount(DISTINCT visitors.id)
.