Update statements with joins don't seem to work
See original GitHub issueHello! Just switched from Prisma and loving the library so far. Just ran into an issue where I’m hoping there’s a workaround or I’m doing something wrong.
Basically, I have an existing MySQL statement that reads:
update OverallMV O join OverallMV O2 on O.Rank = O2.Rank and O.UserID <> O2.UserID set O.Tied = true
So I recreated it in Kysely as:
await trx.updateTable("OverallMV as O").innerJoin(
"OverallMV as O2",
(join) =>
join
.onRef("O.Rank", "=", "O2.Rank")
.onRef("O.UserID", "<>", "O2.UserID"),
).set({ Tied: 1 }).execute();
However, this fails with a ER_PARSE_ERROR
since it puts the set before the join:
update `OverallMV` as `O` set `Tied` = 1 inner join `OverallMV` as `O2` on `O`.`Rank` = `O2`.`Rank` and `O`.`UserID` <> `O2`.`UserID`
Any ideas here?
Side note, I’m wondering if this use case was never tested as I would also expect to choose which table to update in the set. So more like .set({ "O.Tied": 1 })
, which does not currently work but .set({ Tied: 1 })
does.
Issue Analytics
- State:
- Created a year ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
MySQL - Inner join not working with update statement
UPDATE `Student` INNER JOIN `Year` on Student.GradYear = Year.GradYear and Year.UserID = 1 SET Student.Half = 0;. ^this should work.
Read more >5 Useful Tips for Using SQL Server UPDATE Query with JOIN
Using Update Command with Join Safely (5 Tips) · View the Records First with the SELECT Statement · Do a Trial Run Using...
Read more >SQL Update Join not updating all records - MSDN
Using SQL Server 2008 R2. I have a select join query that will return 2202 records: SELECT * FROM bAPVM V INNER JOIN...
Read more >SQL UPDATE from SELECT, JOIN or MERGE
In this article learn how to update data in a SQL Server table from another table using a JOIN, the MERGE statement or...
Read more >Using A SQL JOIN In A SQL UPDATE Statement (Thanks John ...
I have known for a long time that you could update a SQL View in Microsoft SQL Server (back when I used to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That wouldn’t work with the types. Correct me if I’m wrong, but on MySQL you can update all joined tables? So we would need to append to
UT
ofUpdateQueryBuilder<DB, UT, TB, O>
in “beforefrom
joins” and toTB
in “afterfrom
joins”. We’d need to add a new type argument just to indicate iffrom
has been called.I think what @igalklebanov suggested regarding
WITH
is this: