-4

I am working on complicated problem, but I will simplify it to this problem.

I have two tables

A [ID, column1, column2, column3]
B [ID, column1, column2, column3, column4]
C [ID, column1, column2,column3] 

I am creating another third table using this query.

UPDATE C 
  set column1=t.firstTab, column2=t.secondTab, column3=t.thirdTab 
  from (select A.column1 as firstTab, B.column2 as secTab, 
              (A.column1 + B.column2) thirdTab 
        from A, B limit 1; ) as t ; 

I got: UPDATE 0

when I run this query:

select A.column1 as firstTab, B.column2 as secTab, (A.column1 + B.column2) thirdTab 
            from A, B limit 1; 

I got results.

3
  • Please provide sample data and desired results. Also, fix the description ("I have two tables" followed by three tables and "I am creating a third table" followed by an update rather than a create table statement). Feb 6, 2014 at 15:14
  • Just to confirm, you want to update all rows of C with just the values from the first row of the projection t?
    – StuartLC
    Feb 6, 2014 at 15:17
  • @StuartLC : Yes, exactly. This is what I want to do. Feb 6, 2014 at 15:18

1 Answer 1

2

As per the comments, this will update all rows of C from the first row (unspecified order) of the projection:

UPDATE C
  set column1=t.firstTab, column2=t.secTab, column3=t.thirdTab 
  FROM 
    (select A.column1 as firstTab, B.column2 as secTab, 
            (A.column1 + B.column2) thirdTab 
     from A, B limit 1) as t;

SqlFiddle here

If you just want to update ONE row of C with the first row of the projection, and if C.ID is a primary key:

UPDATE C 
  set column1=t.firstTab, column2=t.secondTab, column3=t.thirdTab 
  from
  (select A.column1 as firstTab, B.column2 as secondTab, 
              (A.column1 + B.column2) thirdTab 
        from A, B limit 1) as t 
  WHERE C.ID IN (SELECT ID from C LIMIT 1);

However, if you have no primary key on C, then you can use the CTID system column:

UPDATE C 
  set column1=t.firstTab, column2=t.secondTab, column3=t.thirdTab 
  from
  (select A.column1 as firstTab, B.column2 as secondTab, 
              (A.column1 + B.column2) thirdTab 
        from A, B limit 1) as t 
  WHERE CTID IN (SELECT CTID from C LIMIT 1);

Updated Fiddle here

4
  • I am not seeing difference between your query and mine. Feb 6, 2014 at 15:23
  • 2 minor things - remove the semicolon after the limit, and the projected column name is secTab. The fiddle works. Perhaps your real query is more involved than the example given and doesn't demonstrate the issue?
    – StuartLC
    Feb 6, 2014 at 15:30
  • But why we are getting duplications ? Feb 6, 2014 at 15:40
  • ? Do you just want to update the first row of C with the first row of the projection? Typically you would have some kind of join condition between A, B and C.
    – StuartLC
    Feb 6, 2014 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.