Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 更新未添加正确的值_Sql Server - Fatal编程技术网

Sql server 更新未添加正确的值

Sql server 更新未添加正确的值,sql-server,Sql Server,我的select语句返回我正确加入的表中的ID 393,从92开始,上升到484,它不是PK字段,因此当我总共需要更新550行时,一些ID可以被多次使用,当我运行更新时,它只为所有550行将数字92添加到表中 update movements set [location] = locaID.id FROM ( SELECT lo.id FROM [Harvest_Transactions] ht left join [Harvest_Master_Shift] hms on

我的select语句返回我正确加入的表中的ID 393,从92开始,上升到484,它不是PK字段,因此当我总共需要更新550行时,一些ID可以被多次使用,当我运行更新时,它只为所有550行将数字92添加到表中

update movements set [location] = locaID.id FROM (
SELECT
      lo.id
  FROM [Harvest_Transactions] ht
  left join [Harvest_Master_Shift] hms on ht.Harvest_Master_id = hms.Harvest_Master_id
  left join [Greenhouse_Troughs] gt on ht.Trough = gt.Greenhouse_Trough_id 
  left join [batches] b on b.name = hms.Batch_id
  left join [phases] p on p.batch = b.id and p.[type] = 3
  left join #loc lo on lo.name = gt.Trough_No and lo.area = gt.SQM_per_Trough and lo.Bay_id = gt.Bay_id
  where ht.Number_of_Plants_Harvested != 0 and (hms.CreatedOn <= '2020-02-05 09:33:00.000' OR hms.CreatedOn is null )
 )locaID where product = 14

我遗漏了什么,所以它会用正确的值进行更新?

我对您的查询的第一印象是,只有移动表中product=14的行会用子查询中的单个值进行更新

如果要基于另一个派生表更新值,则需要某种方式将行链接在一起,即要更新的表与保存其他数据的表之间的连接请参见

动作和动作之间的共同点是什么

SELECT
  lo.id
 FROM [Harvest_Transactions] ht
 left join [Harvest_Master_Shift] hms on ht.Harvest_Master_id = hms.Harvest_Master_id
 left join [Greenhouse_Troughs] gt on ht.Trough = gt.Greenhouse_Trough_id 
 left join [batches] b on b.name = hms.Batch_id
 left join [phases] p on p.batch = b.id and p.[type] = 3
 left join #loc lo on lo.name = gt.Trough_No and lo.area = gt.SQM_per_Trough and 
 lo.Bay_id = gt.Bay_id
 where ht.Number_of_Plants_Harvested != 0 and (hms.CreatedOn <= '2020-02-05 
 09:33:00.000' OR hms.CreatedOn is null )
我认为粗略的语法应该是

UPDATE movements
set [location] = locaID.id 
FROM movements
JOIN (
SELECT
  lo.id
 FROM [Harvest_Transactions] ht
 left join [Harvest_Master_Shift] hms on ht.Harvest_Master_id = hms.Harvest_Master_id
 left join [Greenhouse_Troughs] gt on ht.Trough = gt.Greenhouse_Trough_id 
 left join [batches] b on b.name = hms.Batch_id
 left join [phases] p on p.batch = b.id and p.[type] = 3
 left join #loc lo on lo.name = gt.Trough_No and lo.area = gt.SQM_per_Trough and 
 lo.Bay_id = gt.Bay_id
 where ht.Number_of_Plants_Harvested != 0 and (hms.CreatedOn <= '2020-02-05 
 09:33:00.000' OR hms.CreatedOn is null )
) locaID
ON movements.<some column> = locaID.<some column>

您正在使用SQL Server吗?或者Postgres?我正在SQL Server中运行更新脚本,但正在将数据推送到Postgres,将SQL切换到Postgres值得将其添加到问题中以澄清。您缺少SELECT just after=运算符和beforelocaID@Mohamad除非我想将其转换为另一个子查询,否则select在此不起作用。我已调整了查询,取代了92填充点,现在为空。你调整了什么?您是否实现了移动和派生表之间的连接?