Update SQL table with random value from other table

后端 未结 6 1833
挽巷
挽巷 2020-12-31 06:56

On Microsoft SQL Server 2008, I have a table with Products:

Id | Name | DefaultImageId

And one with Images:

Id | ProductId |

6条回答
  •  醉酒成梦
    2020-12-31 07:44

    Another possible solution

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

提交回复
热议问题