rand by order如何检索掉某些ID为空的数据条? 我有一个表,ID项自增,但是中间某些ID段曾经被删除过,所以总计29W条记录,最大的ID号是35W
现在用下面的语句随机,当ID号为某个数据库里已被删除的条目时,老是默认 ID = 22857 的记录(注:ID=14794 - ID=22856 已被删除)
SQL code
SELECT * FROM `my_table` AS r1
JOIN (SELECT (FLOOR(RAND() * (SELECT MAX(id) FROM `my_table`))) AS id) AS r2
WHERE r1.id >= r2.id
ORDER BY r1.id ASC
LIMIT 1
加了 WHERE id!='NULL' AND id!='' 依旧没有效果,求助如何修改可以跳过不存在的ID?谢谢。
SQL code
SELECT * FROM `my_table` AS r1
JOIN (SELECT (FLOOR(RAND() * (SELECT MAX(id) FROM `my_table` WHERE id!='NULL' AND id!=''))) AS id) AS r2 WHERE r1.id >= r2.id
ORDER BY r1.id ASC
LIMIT 1