An SQL query-php Tutorial

Source: Internet
Author: User
A problem related to an SQL query has a table groups, which contains many fields.
Id
User
Cost
Type
Addtime
Uptime
Note
.....

There are 5 types in type, namely 001-005,
The result is to query the total number of-in the last 6 months,
For example, you can query data from January 1, 31, and TYPE.
Select count (*) from groups where type = '001' and addtime <= '20180101' and addtime> = '20180101'
Select count (*) from groups where type = '002' and addtime <= '20180101' and addtime> = '20180101'
Select count (*) from groups where type = '003 'and addtime <= '20180101' and addtime> = '20180101'
Select count (*) from groups where type = '004 'and addtime <= '20180101' and addtime> = '20180101'
Select count (*) from groups where type = '005 'and addtime <= '20180101' and addtime> = '20180101'
You need to query 30 times for 6 months.
If many people log on to this system, it takes a lot of time to query such SQL statements. I have tried it. if all three people open this page, the response takes more than three seconds.
My idea is whether I can write the query results to a table. in this way, I directly query the data of the table during the query, and then I can set a time for the SQL data.
Automatically query results and update them to a table. In this way, it is faster to query data directly from a table than to query distinct using count. but how can this problem be achieved? what I use now is the most stupid
Solution: query them one by one.


Reply to discussion (solution)

Select count (*) from groups where type in ('001 ', '002', '003 ', '004', '005 ') and addtime> = '20180101' and addtime <= '20180101'

Select count (*) from groups where type in ('001 ', '002', '003 ', '004', '005 ') and addtime> = '20180101' and addtime <= '20180101'

Hello, thank you for your reply. the query result is a sum. I want a separate value.
For example, TYPE = 001. The total number in January is 20.
TYPE = 002 the total number of January is 500
TYPE = 003 the total number of January is 3987
TYPE = 004 the total number of January is 9898
TYPE = 005 the total number of January is 123

select count(*) from groups where addtime>='20130101' and addtime <= '20130630' group by addtime,type

Select type, left (addtime, 6) as addtime, count (*) as cnt from groups group by 1, 2

Even in does not need group by type, DATE_FORMAT (addtime,'m % ').

select count(*) from groups where addtime>='20130101' and addtime <= '20130630' group by MONTH(addtime),type

select count(*) from groups where addtime>='20130101' and addtime <= '20130630' group by substring(addtime,4,2),type

Method 2:
1. use SQL to write temporary tables
2. create an index related to the where condition;

Temporary tables are a good choice and will be released automatically after data processing is complete!

Use group by + 1
Study how to write
I cannot use the cache to reduce queries...

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.