阅读完所有“INSERT INTO”帖子以及文档后,我仍然不确定我想要的是否可行。 我想将下面的UPDATE更改为INSERT INTO,因为我有很多它们(速度问题):
UPDATE city c
SET
c.g17h = '3196504',
c.g17q = '2593487',
c.g17k = '0',
c.g17w = '0',
c.g17s = '0'
WHERE
p17t = 30 AND p17l = '30';
或
UPDATE city c
SET
c.g1h = '0',
c.g1q = '0',
c.g1k = '0',
c.g1w = '0',
c.g1s = '0'
WHERE
p1t = 1
AND p1l = '1';
但是,我最好的解决方案不起作用:
INSERT INTO city (g17h, g17q, g17k, g17w, g17s)
SELECT
'3196504',
'2593487',
'0',
'0',
'0'
FROM valuestoretab
WHERE
p17t = 30
AND p17l = '30';
这当然是因为我的WHERE条件只能在第一个表(城市)中得到满足,而不能在第二个只满足值表的情况下得到满足,而城市表是一个数据集,其中每个id都有特定的值对于p17t,p17l等。 澄清:
The first table (city) looks like
cityid1, ownerid1, islandid1, p17t, p17l
cityid2, ownerid2, islandid2, p17t, p17l
对于p17t和p17l,每行具有不同的值。
所以,我的问题是: 1.是否可以为插入的表编写带有WHERE条件的INSERT-Query? 2.如果不是,我是否必须坚持我的更新或是否有其他(快速!)解决方案?
感谢社区!
litotes
答案 0 :(得分:0)
可以使用select语句快速回答插入
Insert INTO MyTable (Val1, Val2)
Select
SomeValue,
AnotherValue
From MyOtherTable
Where Date = Getdate()
另一方面,您还可以更新许多记录,如以下示例所示:
Update t1
Set
t1.Val1 = t2.SomeValue,
t1.Val2 = t2.AnotherValue
From MyTable t1
Inner join MyOtherTable t2 ON t1.PK = t2.FK
where t2.Date = getdate()
编辑:
当我阅读以下查询时,我认为p17t和p17l来自valuestoretab。
UPDATE city c
SET
c.g17h = '3196504',
c.g17q = '2593487',
c.g17k = '0',
c.g17w = '0',
c.g17s = '0'
WHERE
p17t = 30 AND p17l = '30';
==>将其改为:
UPDATE c
SET
c.g17h = '3196504',
c.g17q = '2593487',
c.g17k = '0',
c.g17w = '0',
c.g17s = '0'
FROM City c, ValueStoreTab v
WHERE
c.SomeCol = v.SomeCol -- Here, your relation must exist!
AND v.p17t = 30 AND v.p17l = '30';