删除,占总数的百分比
回答问题 假设我想删除 10% 的行,是否有执行此操作的查询? 就像是: DELETE FROM tbl WHERE conditions LIMIT (SELECT COUNT(*) FROM tbl WHERE conditions) * 0.1 Answers 我会简单地返回过滤行的总数,通过 php 计算并将该值用作我的 DELETE 查询中的限制。 $query = mysql_quer
·
回答问题
假设我想删除 10% 的行,是否有执行此操作的查询?
就像是:
DELETE FROM tbl WHERE conditions LIMIT (SELECT COUNT(*) FROM tbl WHERE conditions) * 0.1
Answers
我会简单地返回过滤行的总数,通过 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
更多推荐
所有评论(0)