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?
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