Update SQL table with random value from other table

后端 未结 6 1816
挽巷
挽巷 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:42

    Check this out.

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

提交回复
热议问题