- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我想更新表中随机选择的 50% 的行。有什么办法吗?
编辑:只是为了澄清它应该始终更新 50% 的记录,但是必须随机选择这 50% 的行(例如不仅是前 50%)。换句话说,平均而言,每隔一条记录都应该更新。
最佳答案
应该像这样工作:
UPDATE table SET x = y WHERE RAND() < 0.5
是的,经过测试,有效。但当然,平均只有 50% 的行,而不是恰好 50%。
如 SQL 92 规范中所写,WHERE
必须为每个元组执行子句,因此 rand()
必须重新评估以产生预期结果(而不是选择所有行或根本不选择行)。
规范摘录(强调我的):
General Rules
1) The
<search condition>
is applied to each row of T. The result of the<where clause>
is a table of those rows of T for which the result of the<search condition>
is true.2) Each
<subquery>
in the<search condition>
is effectively executed for each row of T and the results used in the application of the<search condition>
to the given row of T. If any executed<subquery>
contains an outer reference to a column of T, then the reference is to the value of that column in the given row of T.
关于MySQL:如何更新随机选择的 50% 的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11087059/
我是一名优秀的程序员,十分优秀!