【问题标题】:SQL INSERT from where clause to another where clauseSQL INSERT 从 where 子句到另一个 where 子句
【发布时间】:2016-11-11 15:15:52
【问题描述】:

如何将带有 where 子句的表中的内容复制到同一个表到另一个 where 子句?

所以选择查询将是: SELECT * FROM items WHERE countryNr=5;

我现在想将上述查询中的所有数据插入到 countryNr=1 中。

所以 countryNr=5 和 CountryNr=1 最终应该是相同的数据。

【问题讨论】:

标签: mysql sql insert


【解决方案1】:

您所描述的更像是 UPDATE 而不是 INSERT... 在这种情况下,您的查询可能是这样的:

update item
set (field1, field2) = (select field1, field2 from item where countrNR = 5)
where countrNR = 1 

【讨论】:

  • 我认为应该可以。我稍后会试试这个。我以前试过这个,但它并没有像我想象的那样真正起作用。插入 ts_items(BtnNr、ScreenNr、DepartNr、FunctionNr、TypeNr、ItemNr、BtnText、BonText、数量、价格、MWStSatzNr、已启用、可见、TextColor、LastUpdate、VirtualKeyNr)选择 BtnNr、ScreenNr、DepartNr、FunctionNr、TypeNr、ItemNr、BtnText , BonText, 数量, 价格, MWStSatzNr, Enabled, Visible, TextColor, LastUpdate, VirtualKeyNr FROM ts_items WHERE FilialNr=5
【解决方案2】:

我不确定我是否理解这个问题,但类似于...mySQL docs for insert into select

Insert into items Select Field1, Field2, Field3, Field4..., 1 as countryNR 
from items where countrNR = 5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多