简体   繁体   English

更新 POSTGRESQL 中列的值

[英]UPDATE VALUES OF COLUMN IN POSTGRESQL

I have 4 tables:我有 4 张桌子:

1. matchsal 
ID salaray
1  1000
2  2000
3  3000
4  4000
5  5000

2. TABLE1
ID   C
1   NA
2   NA
3   NA
4   NA
5   NA

3. TABLE2
ID   lfs1   lfs2
1    2      3
2    3      1
3    3      1
4    3      1
5    2      3

4. TABLE3
ID_NIC    filternn
1         private
2         public
3         private
4         Private
5         public

what i want is to update table1 with the salary values from matchsal table with conditions in the others tables, i tried this query:我想要的是用匹配表中的工资值和其他表中的条件更新 table1,我尝试了这个查询:

update TABLE1 LFS
SET  C1= (Select SALARY from matchsal ss )
WHERE LFS."ID" IN
(   SELECT "ID" from
        TABLE2 lfs,
        TABLE3 NIC
    WHERE lfs."ID"=NIC."ID_NIC" 
    and lfs.lfs1 <> LFS.lfs2
    and filternn in ( 'Private'))

and i got this error: ERROR: more than one row returned by a subquery used as an expression SQL state: 21000我得到了这个错误:错误:一个子查询返回的不止一行用作表达式 SQL state: 21000

The subquery in the SET is causing the problem. SET中的子查询导致了问题。 It is possible that you want:您可能想要:

SET  C1 = (Select ss.SALARY from matchsal ss where ss.id = lfs.id)

But that is just a guess.但这只是一个猜测。

Try this:尝试这个:

update TABLE1 LFS
SET  C1= (Select ss.SALARY from matchsal ss where lfs.id=ss.id)
WHERE lfs.ID IN
(   SELECT ID from
        TABLE2 lfs,
        TABLE3 NIC
    WHERE lfs.ID=NIC.ID_NIC
    and lfs.lfs1 <> LFS.lfs2
    and filternn in ( 'Private'))
    

I found the answer ^^" It is simple thing我找到了答案^^”这很简单

update TABLE1 LFS
SET  C1= (Select SALARY from matchsal ss where ss.id=LFS."ID" limit 1)
WHERE LFS."ID" IN
(   SELECT "ID" from
        TABLE2 lfs,
        TABLE3 NIC
    WHERE lfs."ID"=NIC."ID_NIC" 
    and lfs.lfs1 <> LFS.lfs2
    and filternn in ( 'Private'))

I Just add a limit to the sub query我只是为子查询添加一个限制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM