0

I have two tables. I want to update the emodnet_code column values of the table named 2018_01 based on the column emodnet_type of another table named shiptype_emodnet and using the matching of values of two other columns: column aisshiptype from 2018_01 table and column aisshiptype from shyptype_emodnet table. Query returned successfully but 0 rows affected:

UPDATE "2018_01"
SET emodnet_code = shiptype_emodnet.emodnet_type
FROM "shiptype_emodnet" 
WHERE '2018_01.aisshiptype' = 'shiptype_emodnet.aisshiptype';
5
  • 1
    And what is your question? Commented Jul 29, 2019 at 11:18
  • What does the corresponding SELECT return? Commented Jul 29, 2019 at 11:21
  • the query returned succesfully but with 0 rows affected and this is not possible. my question is: there is some error in the code? Commented Jul 29, 2019 at 11:21
  • Why is this not possible? If the SELECT returns 0 rows, there is no data that matches the WHERE condition. Check it. Commented Jul 29, 2019 at 11:22
  • Obviously it's not "impossible" as it apparently happened to you ;) Commented Jul 29, 2019 at 11:26

2 Answers 2

1

You are comparing string constants in your WHERE clause, not columns. So your where clause:

WHERE '2018_01.aisshiptype' = 'shiptype_emodnet.aisshiptype';

is always false, because the string literal '2018_01.aisshiptype' is never the same as the string literal 'shiptype_emodnet.aisshiptype'. So your where condition is essentially the same as:

where false

Identifiers need to be quoted with double quotes ("). Single quotes (') are only for string literals.

UPDATE "2018_01"
  SET emodnet_code = shiptype_emodnet.emodnet_type
FROM "shiptype_emodnet" 
WHERE "2018_01".aisshiptype = shiptype_emodnet.aisshiptype;

And you only need the double quotes for columns or tables that use names that are illegal in SQL or were created using double quotes and mixed case.

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

Comments

0

Can you try:

UPDATE "2018_01" t
SET t.emodnet_code = (SELECT shiptype_emodnet.emodnet_type 
                    FROM shiptype_emodnet 
                    WHERE t.aisshiptype = shiptype_emodnet.aisshiptype
                    Limit 1);

You should add limit 1 for update each row

Comments

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.