在 Microsoft SQL Server 2008 上,我有一个包含产品的表:
ID |名称 |默认图像Id
还有一张带有图片的:
ID |产品编号 |字节
我想运行一个 Update 语句,该语句使用图像表中的随机 ID(通过 ProductId 列与产品相关)更新 Products 表中所有记录的 DefaultImageId。
有人可以帮忙吗?对于任何 SQL 冠军来说都应该很简单(这显然不是我)..
最佳答案
使用所选答案解决@philreed 的问题:
Is there a way to assign each row being updated with a different value randomly chosen from the source table?
UPDATE Products
SET DefaultImageId = t2.Id
FROM Products t1
CROSS APPLY (
SELECT TOP 1 Id
FROM Images
WHERE t1.Id = t1.Id
ORDER BY newid()
) t2
关于sql-server - 使用其他表中的随机值更新 SQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1289600/