6

I want to join 2 tables and update value of firts table on specified value of 2nd table. I failed using others solutions.

UPDATE customers
SET cutoffstop = cutoffstop + '432000'
FROM customers as c
JOIN bluemedia as b ON c.id = b.customerid
WHERE b.orderid = '217201807'
3
  • 1
    Quote from the manual "Note that the target table must not appear in the from_list, unless you intend a self-join" - in other words: do not repeat the targe table in the FROM clause Commented Mar 28, 2018 at 9:16
  • You don't need to repeat the cusomers table in the FROM clause: UPDATE customers c SET cutoffstop = cutoffstop + '432000' FROM bluemedia as b ON c.id = b.customerid WHERE b.orderid = '217201807' Commented Mar 28, 2018 at 9:17
  • 1
    If cutoffstop is a numerical field: SET cutoffstop = cutoffstop + 432000 (you cannot add a string to a numerical field) Commented Mar 28, 2018 at 9:27

3 Answers 3

23

General Update Syntax:

[ WITH [ RECURSIVE ] with_query [, ...] ]
UPDATE [ ONLY ] table [ [ AS ] alias ]
    SET { column = { expression | DEFAULT } |
          ( column [, ...] ) = ( { expression | DEFAULT } [, ...] ) } [, ...]
    [ FROM from_list ]
    [ WHERE condition | WHERE CURRENT OF cursor_name ]
    [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]

Solution for you problem:

UPDATE customers AS c
SET cutoffstop = cutoffstop + 432000
FROM bluemedia as b
WHERE c.id = b.customerid
AND b.orderid = '217201807'

For more information on UPDATE syntax follow the below link:

https://www.postgresql.org/docs/current/static/sql-update.html

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

7 Comments

It gave me an error ERROR: syntax error at or near "AS" LINE 1: SELECT COUNT(*) AS total FROM (UPDATE customers AS cust ^
Sorry I have assigned the wrong alias for customers. Now Try Again.
It keeps throwing me an error ERROR: syntax error at or near "AS" LINE 1: SELECT COUNT(*) AS total FROM (UPDATE customers AS c ^
@rafal1137 There is no COUNT(*) in your original query.
can you give me full query including SELECT Count(*) AS part
|
0

You could use EXISTS

UPDATE customers c
SET c.cutoffstop = c.cutoffstop + '432000'
WHERE EXISTS (
        SELECT 1
        FROM bluemedia b
        WHERE c.id = b.customerid
            AND b.orderid = '217201807'
        );

Comments

0

Use this query for updation:

UPDATE customers
SET cutoffstop = cutoffstop + 432000
FROM bluemedia as b ON customers.id = b.customerid
WHERE b.orderid = '217201807'

3 Comments

ERROR: syntax error at or near "SET" LINE 2: SET cutoffstop = cutoffstop + '432000' ^
please let me know the data type of cutoffstop.. if cutoffstop is numeric or int then remove single code ....UPDATE customers SET cutoffstop = cutoffstop + 432000 FROM bluemedia as b ON customers.id = b.customerid WHERE b.orderid = '217201807'
@rafal1137: you can't add strings using +. But if those are numbers you need to get rid of the useless single quotes around the number '432000' is a varchar, 432000 is an integer

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.