0

I have 4 tables:

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:

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

4
  • What don't you understand? The error seems pretty clear. Commented Mar 8, 2021 at 16:39
  • I need to fix it, and i don't see the answer :" Commented Mar 8, 2021 at 16:41
  • @NoraS will the salary be 4000 for all the ID in table1? Commented Mar 8, 2021 at 16:58
  • No, there id should match with ids in matchsal table, so it should be 1000,3000,4000 Commented Mar 8, 2021 at 19:22

3 Answers 3

1

The subquery in the SET is causing the problem. 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.

Sign up to request clarification or add additional context in comments.

1 Comment

still face the same problem
1

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'))
    

Comments

0

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

1 Comment

Don't you have to match ID between matchsal and table1? Otherwise it will pick any salary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.