Laravel: only one statement is used to identify the number of data entries corresponding to different conditions in a table: {code ...} it is concluded that the number of sex is 1 and age is 2, the number of sex is 1 and age is 5, and the number of sex is 1 and age is 10: high efficiency I may not be very clear about what I need... laravel: uses only one statement to identify the number of data entries corresponding to different conditions in a table.
See the following table:
id name age sex 1 a 1 02 b 10 1 3 c 2 1
It is concluded that the number of sex is 1 and age is 2, the number of sex is 1 and age is 5, and the number of sex is 1 and age is 10.
Requirement: high efficiency
Maybe I am not very clear about it. I need to know how many of them are 2 years old and 5 years old when sex is 1, which of the three answers is 10 years old?
Note: it is best to use a statement in the laravel framework. it is best not to use an original statement. The data volume ranges from several hundred million to several millions.
Help me ~
Reply content:
Laravel: uses only one statement to identify the number of data entries corresponding to different conditions in a table.
See the following table:
id name age sex 1 a 1 02 b 10 1 3 c 2 1
It is concluded that the number of sex is 1 and age is 2, the number of sex is 1 and age is 5, and the number of sex is 1 and age is 10.
Requirement: high efficiency
Maybe I am not very clear about it. I need to know how many of them are 2 years old and 5 years old when sex is 1, which of the three answers is 10 years old?
Note: it is best to use a statement in the laravel framework. it is best not to use an original statement. The data volume ranges from several hundred million to several millions.
Help me ~
Try this sentence
Table::select(DB::raw('age,count(id) count'))->where('sex',1)->groupBy('age')->get()->toArray();
SQL is not very understandable, but using Query Builder instead of Eloquent ORM can improve efficiency. Although this ORM is very powerful, it is also the slowest place in Laravel.
The database aggregation function is required by the subject.
DB::table('yourtable')->where('sex', 1)->where('age', $age)->count();
I don't know if it's the logic that the landlord wants.