Random extraction of three consecutive records

Source: Internet
Author: User
Randomly draw 3 consecutive records?
As table:
Temid
1
2
3
4
5
6
7
8

If you take 3 records, the result can be:
1
2
3
Or
4
5
6
Or
7
8
1
Or
8
1
2
Wait a minute

Can't figure out how to write MySQL? Is there any good way to use PHP to take random random extraction ? php?mysql


------Solution--------------------
Use rand (1,8) to get a random integer, stored in a 3-length list, if the obtained random integer is the same as before, re-fetch, take 3 different numbers, with these 3 numbers for the ID to the database to fetch records
------Solution--------------------

References: Do
781 and 812 still count as continuous data? You're a ring-shaped list.

SELECT * FROM test as T1 joins (select round (rand () * (select Max (temid) from ' Test ') as ID) as T2
where T1. Temid >= T2. Temid
ORDER by T1. Temid ASC Limi ...


There's a clerical error.

SELECT * FROM test as T1 joins (select round (rand () * (select Max (temid) from ' Test ') as Temid) as T2
where T1.temid >= T2.temid
ORDER BY t1.temid ASC limit 3;
  • 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.