简体   繁体   English

在 SQL 中,如何将一整列从一个表复制到另一个没有任何相互列或值的表?

[英]how do i copy one whole column from a table to another without any mutual columns or values in SQL?

i have two tables:我有两张桌子:
Movies which contains movies names (one single column 'movie')包含电影名称的电影(单列“电影”)
Directors which contains directors names and IDs ('director' column and 'ID' column)包含董事姓名和 ID 的董事(“董事”列和“ID”列)
i just want to copy the whole 'director' column from the second table to the first table, no where conditions available as there are no mutual columns or values between the two tables, just copy/pasta thing, both tables are equal in rows and i just want them to aggregate together i tried many possible join/union/insert from select /update from select ways but none have worked (env is MS SQL server 2014)我只想将整个“director”列从第二个表复制到第一个表,没有可用的条件,因为两个表之间没有相互的列或值,只是复制/意大利面,两个表在行和我只是想让他们聚集在一起我尝试了很多可能的加入/联合/插入来自 select / 更新来自 select 方式但没有一个有效(env 是 MS SQL 服务器 2014)

If you want to assign arbitrary director's names to movies, you can use row_number() :如果你想给电影分配任意导演的名字,你可以使用row_number()

with toupdate as (
      select m.*, row_number() over (order by (select null)) as seqnum
      from movies m
     )
update toupdate
    set movie = d.director
    from (select d.*, row_number() over (order by (select null)) as seqnum
          from directors d
         ) d
    where m.seqnum = d.seqnum;

This seems like a very curious thing to do, but it is how I interpret your question.这似乎是一件非常奇怪的事情,但这是我解释你的问题的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM