代码之家  ›  专栏  ›  技术社区  ›  confusedMind

SQL查询问题(从一个表到另一个表随机添加值)

  •  1
  • confusedMind  · 技术社区  · 7 年前

    我有两张桌子 Accounts Proxies .

    我有近100个代理 代理人 表现在我想随机分配代理到 账户 表。我正在使用此查询:

    update Accounts set proxy= (select top 1 proxy from Proxies order by newid())
    

    但是它用相同的代理更新所有的行,从理论上看,我相信它应该为每一行随机获得一个新的代理。

    3 回复  |  直到 7 年前
        1
  •  3
  •   Livius    7 年前

    update A 
    set proxy= (select top 1 proxy from Proxies P WHERE P.proxy-P.proxy=A.proxy-A.proxy order by newid()) 
    FROM Accounts A 
    

    http://sqlfiddle.com/#!18/c68ce/4/1

        2
  •  0
  •   Zaynul Abadin Tuhin    7 年前

    where

    update Accounts set proxy= (select top 1 proxy from Proxies order by newid())
    where Accounts.col =something
    
        3
  •  0
  •   Dumi    7 年前

    UPDATE t1
    SET t1.proxy= t2.proxy
    FROM Accounts t1
    CROSS APPLY (
       SELECT TOP 1 p.proxy
       FROM Proxies p
       WHERE p.proxy= t1.proxy
       ORDER BY newid()
    ) t2