Home > Backend Development > PHP Tutorial > mysql随机获取优化语句

mysql随机获取优化语句

WBOY
Release: 2016-06-23 14:01:43
Original
823 people have browsed it

在网上找了一个随机取数语句,结合了自己的需求,运行出错,
看了半天没发现出错的地方,求指点

$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" ;
Copy after login


回复讨论(解决方案)

错误信息是什么?

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

23代码如下
$result=mysql_query($str, $con);  //送出查?	$rows=mysql_num_rows($result); //23行
Copy after login

因为是你的sql语句出错了 你把$str语句打印下看是什么东东

$str在1楼贴了

$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
Copy after login

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

8楼的不行,7楼的OK,多谢了
一直没搞明白这句的意思

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

网上说ROUND 函数用于把数值字段舍入为指定的小数位数,所以不太理解这语句的结果是什么

RAND 产生一个小于 1 的随机数
SELECT MAX(id) FROM dizhi 取得表 dizhi 中最大的 id 值
ROUND 四舍五入化为整数

这个查询指令的意思是:
任取表中的一个 id 值

理解了,3Q。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template