MySQL obtains the continuous range. mysql obtains the range.

Source: Internet
Author: User

MySQL obtains the continuous range. mysql obtains the range.

1. Raw Data

2. Convert running_number into data and add an ordered number to the column.

SELECT d. running_number + 0 running_number, @ a: = @ a + 1 rn FROM device_data d, (SELECT @ a: = 0) a where d. device_id = 13 order by d. running_number;

3. The difference between running_number and ordered data. If the difference is the same as diff, running_number indicates that it is continuous.

SELECT running_number, rn, running_number-rn diffFROM (SELECT d. running_number + 0 running_number, @ a: = @ a + 1 rn FROM device_data d, (SELECT @ a: = 0) a where d. device_id = 13) B;

4. grouping based on the difference and obtaining the minimum and maximum running_number of the correlation difference forms a continuous range.

Select min (c. running_number) min, max (c. running_number) maxfrom (SELECT running_number, rn, running_number-rn diff FROM (SELECT d. running_number + 0 running_number, @ a: = @ a + 1 rn FROM device_data d, (SELECT @ a: = 0) a where d. device_id = 13) B) c group by diff;

5. Refer to MySQL technology Insider: SQL programming 2.6.2 continuous range problem

Related Article

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.