laravel: How to find out the number of data items corresponding to different conditions in a table using only one statement
As shown in the following table:
<code>id name age sex 1 a 1 0 2 b 10 1 3 c 2 1 </code>
Get: the number of sex is 1 and age is 2, the number of sex is 1 and age is 5, the number of sex is 1 and age is 10
Requirement: High efficiency
Maybe I didn’t express it very clearly, I need to get it The answer is how many are 2 years old, how many are 5 years old, and how many are 10 years old when sex is 1
Note: It is best to use Laravel framework statements, not original statements, the amount of data can range from hundreds of thousands to millions of items
Everyone, please help~
laravel: How to find out the number of data items corresponding to different conditions in a table using only one statement
As shown in the following table:
<code>id name age sex 1 a 1 0 2 b 10 1 3 c 2 1 </code>
Get: the number of sex is 1 and age is 2, the number of sex is 1 and age is 5, the number of sex is 1 and age is 10
Requirement: High efficiency
Maybe I didn’t express it very clearly, I need to get it The answer is how many are 2 years old, how many are 5 years old, and how many are 10 years old when sex is 1
Note: It is best to use laravel framework statements, preferably not original statements. The amount of data can range from hundreds of thousands to millions of items
Everyone, please help~
Try this sentenceTable::select(DB::raw('age,count(id) count'))->where('sex',1)->groupBy('age')-> ;get()->toArray();
I don’t know much about SQL, but using Query Builder instead of Eloquent ORM can improve efficiency. Although this ORM is very powerful, it is also the slowest part of Laravel.
If it is the request of the subject, use the aggregation function of DBDB::table('yourtable')->where('sex', 1)->where('age', $age)-> ;count();
I don’t know if this is the original poster’s logic