我有两个表:samples_specific_storage sss和samples s,两个主键都有4列area_easting,area_northing,context_number,sample_number
表存储具有框号,例如框F52,我首先选择所有框= F52
SELECT sss.area_easting, sss.area_northing, sss.context_number,
sss.sample_number
FROM samples.samples_specific_storage sss
WHERE sss.container_name = 'F19'
我想使用此选项来更新样本表中的记录,这些记录具有相应的area_easting,area_northing,context_number,样本编号。我试图理解嵌套查询,因为我认为这就是我需要的:UPDATE SET FROM WHERE(SELECT ...) 我知道以下是错误的,但有些内容如下:
UPDATE samples.samples_specific_storage
SET current_location = 'testing'
WHERE (
SELECT s.area_easting,s.area_northing,s.context_number,s.sample_number FROM samples.samples s WHERE current_location = 'F19')
虚拟数据:
SSS
area_easting,area_northing,context_number,sample_numbers,box_number
99,45,1,1,F52
99,44,2,1,F52
99,44,2,2,F52
99,44,5,1,F52
......
样品
area_easting,area_northing,context_number,sample_numbers,shelf_number
99,45,1,1,shelf1
99,44,2,1,shelf1
99,44,2,2,shelf1
99,44,5,1,shelf1
......
所以我想将shelf1更新到shelf2,其中box_number = F52 [我知道表的结构很糟糕 - 我继承了它,它将被修复]
答案 0 :(得分:1)
我希望我的问题是正确的,但是......这样可以让你开始:
UPDATE samples t1
SET shelf_number='shelf2'
FROM (SELECT sss.area_easting, sss.area_northing, sss.context_number, sss.sample_number
FROM samples.samples_specific_storage sss
WHERE sss.container_name = 'F19') t2
WHERE t1.area_easting = t2.area_easting
AND t1.area_northing = t2.area_northing
AND t1.context_number = t2.context_number
AND t1.sample number = t2.sample number
我使用Postges的 UPDATE
... FROM
-Syntax,这不是标准SQL,但在这种情况下非常方便。