Evening,
I’m currently running this query:
SELECT `id`, `type`, `controller` FROM `urls` WHERE `url` = :url;
UPDATE `urls` SET `views` = `views` + 1 WHERE `url` = :url;
I run this when a page is loaded and it increments the views column by 1. However, I’m interested to know if there is a way (maybe using something like a trigger) that the view column could be incremented automatically.
I can’t find an exact duplicate but Syntax for "RETURNING" clause in Mysql PDO and Mysql returning clause equivalent give you the answer you need.
MySQL does not have an equivalent of Oracle and PostgreSQLs
returning intoclause. You’ll have to use two separate statements.If you’re expecting to do a lot of updates it might (conditional, it might not) be better to keep the number of views in a separate table, especially as you’re not returning the view count in your query. Something like the following:
and then if you need the number of views:
or use MySQLs
insert ... selectsyntax.