I handle articles on a multi-language site, with the possibility for the users to update the content. I use a MySQL database.
Table articles
: id
Table article_contents
: id
article_id
(FK) content
lang
version
If the new content is still a draft, version
is null
. As soon as the draft is validated by a moderator, it becomes a definitive version and takes the next available version
for the given article_id
and the given lang
.
UPDATE article_contents
SET version=(
SELECT MAX(version)
FROM article_contents
WHERE article_id=SAME_AS_THE_ONE_IN_THE_MAIN_QUERY
AND lang=SAME_AS_THE_ONE_IN_THE_MAIN_QUERY
) + 1
WHERE id=:id;
By what can I replace SAME_AS_THE_ONE_IN_THE_MAIN_QUERY
, or is there a cleaner way to do it?
3
Answers
If you are fine with using variables, try smt like this (T-SQL):
Depending on the operations you are performing,
this might not be the best solution performance wise.
For that you might wanna try CTE
You want to update the version of the current validated article with the next versions in ascending order?
I think you don’t need to match on id and language, why would you since you have null on version on that moment. Instead just get the highest version overall and add 1.
I got it wrong, you want the versions for one specific article. Thank you jarlh for pointing it out.
Use a correlated subquery, i.e. let it reference the update table:
Demo: https://dbfiddle.uk/IkH8jGmy