Home > Backend Development > PHP Tutorial > laravel: How to find out the number of data items corresponding to different conditions in a table using only one statement

laravel: How to find out the number of data items corresponding to different conditions in a table using only one statement

WBOY
Release: 2016-08-04 09:19:16
Original
929 people have browsed it

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>
Copy after login
Copy after login

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~

Reply content:

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>
Copy after login
Copy after login

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 sentence
Table::select(DB::raw('age,count(id) count'))->where('sex',1)->groupBy('age')-&gt ;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 DB
DB::table('yourtable')->where('sex', 1)->where('age', $age)-&gt ;count();
I don’t know if this is the original poster’s logic

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template