Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8453073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:40:26+00:00 2026-06-10T11:40:26+00:00

I have two tables junk=# select * from t; name | intval ———-+——– bar2

  • 0

I have two tables

junk=# select * from t;                                                                                                                                
   name   | intval 
----------+--------
 bar2  |      2
 bar3  |      3
 bar4  |      4
(3 rows)

and

junk=# select * from temp;                                                                                                                             
 id |    name    | intval 
----+------------+--------
  1 | foo   |      0
  2 | foo2  |      2
  3 | foo3  |      3
  4 | foo4  |      4
  5 | foo5  |      5
(5 rows)

Now, I want to use the values from table t to update the values in table temp. Basically, I want to replace the name column in second, third and fourth values in temp by bar2, bar3 and bar4.

I created the table t using the COPY statement. I am doing batch updates and I am trying to optimize that.

So, I get this error. I think this is pretty basic one.

junk=# UPDATE temp FROM t SET name=t.name FROM t WHERE intval=t.intval;
ERROR:  syntax error at or near "FROM"
LINE 1: UPDATE temp FROM t SET name=t.name FROM t WHERE intval=t.int...
                    ^
junk=# 

Fow now, this works.

UPDATE test SET name=t.name FROM t WHERE test.intval=t.intval
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:40:28+00:00Added an answer on June 10, 2026 at 11:40 am

    Get rid of your first FROM t clause.

    FROM must come after SET, not before and it can only affect the WHERE clause. SET must be done with subqueries.

    your completed code is:

    UPDATE temp SET name=(SELECT t.name FROM t WHERE temp.intval = t.inval);
    

    PostgreSQL has some ways to optimize this so it’s not like you are just doing a huge nested loop join (and looking up one row over and over from the heap based on the join criteria).

    Edit: Adding plan to show we are not, in fact, running through a sequential scan of the second table for each row on the first one.

    Here is an example that updates 172 rows in one table using a group-by from another:

    mtech_test=# explain analyze
    update ap
    set amount = (select sum(amount) from acc_trans ac where ac.trans_id = ap.id) + 1;
                                                                     QUERY PLAN 
    
    --------------------------------------------------------------------------------
    ---------------------------------------------------------------------
    Update on ap  (cost=0.00..3857.06 rows=229 width=231) (actual time=39.074..39.0
       74 rows=0 loops=1)
     ->  Seq Scan on ap  (cost=0.00..3857.06 rows=229 width=231) (actual time=0.050..28.444 rows=172 loops=1)
         SubPlan 1
           ->  Aggregate  (cost=16.80..16.81 rows=1 width=5) (actual time=0.109..0.110 rows=1 loops=172)
                 ->  Bitmap Heap Scan on acc_trans ac  (cost=4.28..16.79 rows=4 width=5) (actual time=0.075..0.102 rows=4 loops=172)
                       Recheck Cond: (trans_id = ap.id)
                       ->  Bitmap Index Scan on acc_trans_trans_id_key  (cost=0.00..4.28 rows=4 width=0) (actual time=0.006..0.006 rows=4 loops=172)
                             Index Cond: (trans_id = ap.id)
    Trigger for constraint ap_entity_id_fkey: time=69.532 calls=172
    Trigger ap_audit_trail: time=391.722 calls=172
    Trigger ap_track_global_sequence: time=1.954 calls=172
    Trigger check_department: time=111.301 calls=172
    Total runtime: 612.001 ms
    (13 rows)
    

    `

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: A, B. I need to select all data from B.
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables user table user_id | name | 1 | peter |
I have two tables. Table Emp id name 1 Ajay 2 Amol 3 Sanjay
I have two tables. One is temp table and another is main table. In
I have two tables: Events (ID, Name, Time, Recorder) Video (ID, StartTime, EndTime, Recorder,
I have two tables and want to compare rows on sqlite like this table1
I have two tables: COUNTRY ------------- id name NEIGHBOUR ------------- id id_country1 id_country2 id_country1
I have two tables, I want to transfer all data from the first table
I have two tables with a foreign key constraint how can I delete rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.