

Say that you have a table shipment that you want to augment with information from table vehicle, and both tables have a column vehicle_id so you can use NATURAL JOIN. and the WHERE clause checks that rows from X are present in (X as X_joined join Y).Īs a result this works with natural joins, which is very nice.You don't have to repeat the join condition.

This answer is different from the rest because I'm leaving this answer up to save others from going down this path. It's a pity that the runtime is so bad, because the syntax was very elegant. If it does, then only one of the join rows will be used to update the target row, but which one will be used is not readily predictable.ĮDIT: do not use, execution time increases quadratically In other words, a target row shouldn't join to more than one row from the other table(s). When using FROM you should ensure that the join produces at most one output row for each row to be modified. The moral of the story always check if the join produce a unique result. While using a descending order in the CTE the column is updated to the value of c (lowest value) id|att| With the ascendig order the column is updated to the value of d (highest value) id|att| Order by 2 /* Based on this order different update is performed */ Using the order by in the CTE we get a different results. We use a CTE where the id = 1 id giving two possible values for the update. Some RDMS raise an exception is this case, but PostgreSQL apparently performs the update with a non deterministic outcome. the result of the join produces more values that can be used in the update. Some care should be taken if the join is performed on a non-unique column. It might be straightforward for some but I got stuck on this problem wondering what's going on so hopefully, it will help others. Instead, you must use all the tables in the FROM clause like this: UPDATE join_a_b JOIN b - Not valid since there is no ON clause JOIN b on b.id = join_a_b.b_id - Not valid since join_a_b is used here This means that basically, the following queries are not valid: UPDATE join_a_b Postgres wants a ON clause after the JOIN so you cannot only use where clauses.you cannot use the table you want to update to JOIN another one.To add something quite important to all the great answers above, when you want to update a join-table, you may have 2 problems: And there is no more need to JOIN table with itself (as it was in subquery). SET documents_taken_at = b.certificate_issued_at - we can reference joined table hereĪ.abiturient_id = b.id AND - JOIN ON clauseĪ.documents_taken_at::date < b.certificate_issued_at - Subquery WHEREĪs you can see, original subquery JOIN's ON clause have become one of WHERE conditions, which is conjucted by AND with others, which have been moved from subquery with no changes. SET a.documents_taken_at = b.certificate_issued_at īecomes PostgreSQL-like in such a way UPDATE applications a

WHERE ap.documents_taken_at::date < ab.certificate_issued_at So, we will increase application submit date to fit certificate issue date. Task: correct info, where abiturients (students about to leave secondary school) have submitted applications to university earlier, than they got school certificates (yes, they got certificates earlier, than they were issued (by certificate date specified). Let me explain a little more by my example.
