Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.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 更新Postgres中表的第一行_Sql_Postgresql - Fatal编程技术网

Sql 更新Postgres中表的第一行

Sql 更新Postgres中表的第一行,sql,postgresql,Sql,Postgresql,我有两张桌子: 包含以下列的Corresp:idCorresp、textCorresp、dateCorresp 具有以下列的Trans:idTransf、textTransf、dateTransf、idCorresp 这是一个数据示例: 科雷普斯: 因此,在运行我的查询后,我希望在Trans中获得此结果 我还尝试: update Trans trans3 set textTransf= textCorresp from ( select Corresp .textCorresp ,

我有两张桌子:

包含以下列的Corresp:idCorresp、textCorresp、dateCorresp 具有以下列的Trans:idTransf、textTransf、dateTransf、idCorresp

这是一个数据示例:

科雷普斯:

因此,在运行我的查询后,我希望在Trans中获得此结果

我还尝试:

update Trans trans3 set textTransf= textCorresp from ( select Corresp .textCorresp , min(trans.dateTransf) from Trans trans , Corresp corresp, Trans trans2 where corresp.idCorresp= trans.idCorresp and corresp.idCorresp= trans2.idCorresp group by corresp.textCorresp ,trans.dateTransf )as toto WHERE trans3.idTransf = toto.idTransf
这是总的想法

update trans
set this = that, etc
where idtransf = 
(select min(idtransf)
from trans
where idCorresp = the one you want)
编辑从这里开始

对于新的需求,这将适用于许多db引擎。希望postgresql就是其中之一

update trans
field = sqfield
from trans join 
(select SomeFieldFromTrans sqlfield, min(idtransf) mintrans
from trans join corresp on the proper fields
group by SomeFieldFromTrans ) sq ob idtransf = mintrans 

如果你试着这样做呢

update Trans set textTransf = (select textCorresp from Corresp cr join Trans tx
on cr.idCorresp = tx.idCorresp)
where idTransf = (
select idTransf 
from 
Trans t join Corresp c
on t.idCorresp = c.idCorresp limit 1
)

谢谢你的回答,我已经更新了我的问题,我不想在特定的Corresp和Transf中运行我的查询,而是针对这两个表中的所有数据在工作中,当有人在我满足要求后更改要求时,他们欠我一杯啤酒。首先,如何定义表trans的第一行?是最短日期的那一行吗?一条id最小的线?还有别的吗? update Trans trans3 set textTransf= textCorresp from ( select Corresp .textCorresp , min(trans.dateTransf) from Trans trans , Corresp corresp, Trans trans2 where corresp.idCorresp= trans.idCorresp and corresp.idCorresp= trans2.idCorresp group by corresp.textCorresp ,trans.dateTransf )as toto WHERE trans3.idTransf = toto.idTransf
update trans
set this = that, etc
where idtransf = 
(select min(idtransf)
from trans
where idCorresp = the one you want)
update trans
field = sqfield
from trans join 
(select SomeFieldFromTrans sqlfield, min(idtransf) mintrans
from trans join corresp on the proper fields
group by SomeFieldFromTrans ) sq ob idtransf = mintrans 
update Trans set textTransf = (select textCorresp from Corresp cr join Trans tx
on cr.idCorresp = tx.idCorresp)
where idTransf = (
select idTransf 
from 
Trans t join Corresp c
on t.idCorresp = c.idCorresp limit 1
)