假设我想删除10%的行,是否有查询要执行此操作?
类似的东西:
DELETE FROM tbl WHERE conditions LIMIT (SELECT COUNT(*) FROM tbl WHERE conditions) * 0.1
答案 0 :(得分:4)
如果你只需要大约 10%的行,没有特别的顺序,这应该可以解决问题:
DELETE FROM tbl WHERE RAND() <= 0.1
但是,由于产生随机数的开销,我不建议在非常大的数据集上使用它。
答案 1 :(得分:3)
我只返回已过滤行的总量,通过php计算并在DELETE查询中将该值用作限制。
$query = mysql_query("SELECT COUNT(*) FROM tbl WHERE conditions");
$int = reset(mysql_fetch_array($query));
$int = round($int * 0.1);
mysql_query("DELETE FROM tbl WHERE conditions LIMIT {$int}");
我不确定DELETE是否允许使用高级查询,例如:
DELETE FROM ( SELECT h2.id
FROM ( SELECT COUNT(*) AS total
FROM tbl
WHERE conditions) AS h
JOIN ( SELECT *, @rownum := @rownum + 1 AS rownum
FROM tbl, (SELECT @rownum := 0) AS vars
WHERE conditions) AS h2
ON '1'
WHERE rownum < total * 0.1) AS h3