Mysql random get optimization statement

Source: Internet
Author: User
The mysql random retrieval optimization statement finds a random fetch statement on the Internet, which combines its own needs to run an error,
I haven't found any errors for a long time. please advise.
$str="select * from dizhi where shijiancuo>'$times' GROUP BY user HAVING COUNT(user) > 4 AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM dizhi)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id ASC LIMIT 1" ;


Reply to discussion (solution)

What is the error message?

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\www\AppServ\www\tuijianshuchu.php on line 23

23 The code is as follows:
$ Result = mysql_query ($ str, $ con); // send a query? $ Rows = mysql_num_rows ($ result); // 23 rows

Because an error occurs in your SQL statement, you can print the $ str statement to see what it is.

$ Str posted on the first floor

$ Result = mysql_query ($ str, $ con) or die (mysql_error ());

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM dizhi)) AS id) AS t2 ' at line 1

Select * from
(Select * from dizhi where shijiancuo> '$ times' group by user having count (user)> 4) AS t1
JOIN
(Select round (RAND () * (select max (id) FROM dizhi) AS id) AS t2
WHERE t1.id> = t2.id
Order by t1.id asc limit 1

JOIN (select round (RAND () * (select max (id) FROM dizhi) AS id from dizhi) AS t2

The 8th floor is unavailable. the 7th floor is OK. thank you.
I have never understood the meaning of this sentence

 (SELECT ROUND(RAND() * (SELECT MAX(id) FROM dizhi)) AS id) AS t2 

According to the Internet, the ROUND function is used to ROUND a value field to a specified decimal place, so I don't quite understand what the result of this statement is.

RAND generates a random number less than 1
Select max (id) FROM dizhi to obtain the largest id value in dizhi.
ROUND rounded to an integer

The query command indicates:
Any id value in the table

Understood, 3Q.

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.