Laravel: uses only one statement to identify the number of data entries corresponding to different conditions in a table.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.