Maison > développement back-end > tutoriel php > mysql随机获取优化语句

mysql随机获取优化语句

WBOY
Libérer: 2016-06-23 14:01:43
original
820 Les gens l'ont consulté

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

$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" ;
Copier après la connexion


回复讨论(解决方案)

错误信息是什么?

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

23代码如下
$result=mysql_query($str, $con);  //送出查?	$rows=mysql_num_rows($result); //23行
Copier après la connexion

因为是你的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
Copier après la connexion

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 
Copier après la connexion

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

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

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

理解了,3Q。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal