使用另一个表中的随机值更新SQL表

时间:2009-08-17 18:28:49

标签: sql-server

在Microsoft SQL Server 2008上,我有一个包含Products的表:

Id |名称| DefaultImageId

还有一张图片:

Id | ProductId |字节

我想运行一个Update语句,它会更新Products表中所有记录的DefaultImageId,其中Images表格中的随机ID通过ProductId列与Product相关。

任何人都可以帮忙吗?对于任何SQL Champ来说都应该很简单(显然不是我)..

5 个答案:

答案 0 :(得分:8)

您可以通过NEWID进行订购,获取更新每一行的随机数。

UPDATE
    Products
SET
    DefaultImageId =
    (
        SELECT TOP 1
            Id
        FROM
            Images
        WHERE
            Images.ProductId = Products.Id
        ORDER BY
            NEWID()
    )

这已被标记下来并添加了评论,表明它无法解决问题。我认为混淆来自人们尚未意识到原始问题请求为每个产品选择随机图像,因此带有产品ID的where子句。已提供包含以下数据集的完整脚本。它为每种产品增加了五种产品和三种图像。然后随机设置每个产品的默认图像ID。

CREATE TABLE Products(Id INT, Name NVARCHAR(100), DefaultImageId INT NULL)

CREATE TABLE Images (Id INT, ProductId INT, Bytes VARBINARY(100))

INSERT INTO Products (Id, NAME, DefaultImageId) VALUES(1, 'A', NULL)
INSERT INTO Products (Id, NAME, DefaultImageId) VALUES(2, 'B', NULL)
INSERT INTO Products (Id, NAME, DefaultImageId) VALUES(3, 'C', NULL)
INSERT INTO Products (Id, NAME, DefaultImageId) VALUES(4, 'D', NULL)
INSERT INTO Products (Id, NAME, DefaultImageId) VALUES(5, 'E', NULL)

INSERT INTO Images (Id, ProductId, Bytes) VALUES(1, 1, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(2, 1, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(3, 1, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(4, 2, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(5, 2, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(6, 2, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(7, 3, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(8, 3, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(9, 3, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(10, 4, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(11, 4, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(12, 4, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(13, 5, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(14, 5, NULL)
INSERT INTO Images (Id, ProductId, Bytes) VALUES(15, 5, NULL)

UPDATE
    Products
SET
    DefaultImageId =
    (
        SELECT TOP 1
            Id
        FROM
            Images
        WHERE
            Images.ProductId = Products.Id
        ORDER BY
            NEWID()
    )

SELECT * FROM Products

答案 1 :(得分:3)

另一种可能的解决方案

UPDATE
    Products
SET
    DefaultImageId =
    (
        SELECT TOP 1
            Id
        FROM
            Images
        ORDER BY
            NEWID(), Products.Id
    )

答案 2 :(得分:1)

检查一下。

Update Products
Set DefaultImageId = (
SELECT top 1 Id 
From Images 
Where 1=1
and Products.Id = Images.ProductId
ORDER BY NEWID()
)

答案 3 :(得分:1)

试试这个(在AdventureWorks上):

它使用产品表中的新随机名称更新人员表的所有行。

begin tran
--show initial state:
select top 25 * from person.person order by BusinessEntityID

update person.person
set 
FirstName = otherTable.Name
from 
(select BusinessEntityID, row_number() over (order by newid()) as row_num from person.person) p2
,(select ProductId, Name, row_number() over (order by newid()) as row_num from production.product) as otherTable
where 
person.person.BusinessEntityID=p2.BusinessEntityID 
and (p2.row_num%500)=(otherTable.row_num%500) -- deal with tables with different rowcount

--check results:
select top 25 * from person.person order by BusinessEntityID

rollback tran

或者在SQL Fiddle上尝试类似的查询: http://sqlfiddle.com/#!3/8b719/1

答案 4 :(得分:-1)

declare @carg_id as int
DECLARE cursor_cargo CURSOR FOR 
    SELECT carg_id FROM cargo

OPEN cursor_cargo

FETCH NEXT FROM cursor_cargo
INTO @carg_id
WHILE  @@FETCH_STATUS =0
BEGIN

    update cargo set ciud_id = (
    select top 1 ciud_id from ciudad 
    inner join Pais on Pais.pais_id = ciudad.pais_id
    where ciudad.pais_id is not null and empr_id is  null and pais_status =1 order by  NEWID()) WHERE CARG_ID = @CARG_ID

    FETCH NEXT FROM cursor_cargo
        INTO @carg_id
END
CLOSE cursor_cargo
DEALLOCATE cursor_cargo