ygrek / sqlgg

SQL Guided (code) Generator
https://ygrek.org/p/sqlgg/
GNU General Public License v2.0
62 stars 20 forks source link

UPDATE with JOIN is not supported #71

Closed cyberhuman closed 1 month ago

cyberhuman commented 5 years ago

From https://dev.mysql.com/doc/refman/8.0/en/update.html:

You can also perform UPDATE operations covering multiple tables. However, you cannot use ORDER BY or LIMIT with a multiple-table UPDATE. The table_references clause lists the tables involved in the join. Its syntax is described in Section 13.2.10.2, “JOIN Syntax”. Here is an example:

UPDATE items,month SET items.price=month.price
WHERE items.id=month.id;

The preceding example shows an inner join that uses the comma operator, but multiple-table UPDATE statements can use any type of join permitted in SELECT statements, such as LEFT JOIN.

Reproducer:

CREATE TABLE t1 (
  `t2_id` INTEGER UNSIGNED,
  `value` TEXT
);

CREATE TABLE t2 (
  `id` INTEGER PRIMARY KEY,
  `value` TEXT
);

UPDATE t1 SET t1.`value` = 0;

UPDATE t2 SET `value` = `id`;

UPDATE t1, t2 SET t1.`value` = t2.`value` WHERE t1.t2_id = t2.id;

UPDATE t1 a1, t2 a2 SET a1.`value` = a2.`value` WHERE a1.t2_id = a2.id;

-- does not work:
UPDATE t1 JOIN t2 ON t2.id = t1.t2_id SET t1.`value` = t2.`value`;

-- does not work:
UPDATE t1 a1 JOIN t2 a2 ON a2.id = a1.t2_id SET a1.`value` = a2.`value`;
==> UPDATE t1 JOIN t2 ON t2.id = t1.t2_id SET t1.`value` = t2.`value`
Position 1:14 Tokens: JOIN t2 ON t2.id = t1.t2_id SET t1.`
Error: Sqlgg.Sql_parser.MenhirBasics.Error
==> UPDATE t1 a1 JOIN t2 a2 ON a2.id = a1.t2_id SET a1.`value` = a2.`value`
Position 1:17 Tokens: JOIN t2 a2 ON a2.id = a1.t2_id SET a
Error: Sqlgg.Sql_parser.MenhirBasics.Error
Errors encountered, no code generated
jongleb commented 1 month ago

https://github.com/ygrek/sqlgg/commit/6002d49734f41a8f461b4b7304998c8e3e44a323