日期:2014-05-16  浏览次数:20796 次

MYSQL随机加条件
大家新年好。

mysql 数据库随机获取2条记录,没有红色代码时,无论怎么刷新,得到的记录数永远都是2条。
加了红色部分后,有时只能获取1条记录,代码哪里有问题?(注:ab值保存为是TEXT,储存格式不一定是数字。但是现在我想获取随机值时,大于5000)


SQL code
(SELECT * FROM `cms` AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM `cms`)) AS id) AS t2 WHERE t1.id >= t2.id [color=#FF0000]AND ab>5000[/color] ORDER BY t1.id ASC LIMIT 1) 
UNION 
(SELECT * FROM `cms` AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM `cms`)) AS id) AS t2 WHERE t1.id >= t2.id [color=#FF0000]AND ab>5000[/color] ORDER BY t1.id ASC LIMIT 1)
 LIMIT 2


------解决方案--------------------
用union all看看