gpt4 book ai didi

MySQL:如何更新随机选择的 50% 的行?

转载 作者:IT老高 更新时间:2023-10-29 00:05:31 25 4
gpt4 key购买 nike

我想更新表中随机选择的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com