[英]SQL update from same table / multiple lists and items
I need to update a table that looks like this: 我需要更新一个看起来像这样的表:
No. Item Location Score Available Price some_more_text
1 Water London 0,00
1 Water Amsterdam 1 yes 1,11 alpha
1 Water Burges 1 yes 1,11 alpha
2 Honey London 0,00
2 Honey Amsterdam 5 yes 5,55 omega
2 Honey Burges 5 yes 5,55 omega
3 Spoon London 4 yes 3,33 gamma
3 Spoon Amsterdam 4 yes 3,33 gamma
3 Spoon Burges 4 yes 3,33 gamma
4 Books London 0,00
4 Books Amsterdam 1 no 2,55 alpha
4 Books Burges 1 no 2,55 alpha
5 Juice London 0,00
5 Juice Amsterdam 5 yes 5,55 beta
5 Juice Burges 5 yes 5,55 beta
...
In the end every item in Londen should have the same properties as the according item in Burges (or Amsterdam, doesn't matter). 最后,Londen中的每个项目都应与Burges中的相应项目具有相同的属性(或阿姆斯特丹,无关紧要)。 This can't be done manually as there are so many - but I cant find a way to somehow "batch" a SQL-command to update every item with the same no.
因为有这么多,这不能手动完成-但我找不到找到以某种方式“分批” SQL命令以更新具有相同编号的每个项目的方法。
One more problem: As it is a proprietary software I can't definitely say which language is used - but I assume it's Oracle. 另一个问题:由于它是专有软件,所以我不能肯定地说出使用哪种语言-但我认为它是Oracle。
You can join the table on itself in an update. 您可以将表本身加入更新中。
UPDATE table_X lon, table_X bur
SET lon.item=bur.item, lon.score=bur.score, etc
WHERE lon.No=bur.NO AND lon.location="London" AND bur.location="Burges";
That updates London with the data from Burges 从而使用Burges的数据更新了伦敦
In case it's actually SQL Server: 如果实际上是SQL Server:
update lon
set lon.item = bur.item, lon.score = bur.score, etc
from table_X lon
inner join table_X bur
on lon.No = bur.NO
where lon.location='London' AND bur.location='Burges';
If you're actually using Oracle you could use a merge
statement (which should work with MSSQL too with a slightly different syntax): 如果您实际上使用的是Oracle,则可以使用
merge
语句(该语句也应与MSSQL一起使用,语法稍有不同):
MERGE INTO table1
USING (
SELECT
t2.no t2_no,
t2.score t2_score,
t2.available t2_available,
t2.price t2_price,
t2.some_more_text t2_some_more_text
FROM table1 t1
JOIN table1 t2 ON t1.no = t2.no
WHERE t1.Location='London' AND t2.Location='Burges'
) ON (no = t2_no)
WHEN MATCHED THEN UPDATE
SET score = t2_score,
available = t2_available,
price = t2_price,
some_more_text = t2_some_more_text;
See the sample SQL Fiddle 参见示例SQL Fiddle
Reference documentation for merge (Oracle 11g). 合并的参考文档 (Oracle 11g)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.