Can I join SELECT and UPDATE statement? If yes, how can I do that?
I have table which name is news.
I show news on my website. If someone read news I add 1 to hit. I do that with 2 queries:
mysql_query("SELECT * FROM news WHERE id=2 LIMIT 1");
mysql_query("UPDATE news SET hit=hit+1 WHERE id=2");
But I want to join them.
id | title | content |hit
---+-----------+-------------------+---
1 | sdfsdfdsf | dfsdffdsfds836173 |5
2 | sfsdfds | sdfsdsdfdsfdsfsd4 |9
3 | ssdfsdfds | sdfdsfs |3
Update: the OP wants to update and select the updated rows within one sql statement.
Write a stored procedure:
Usage:
Update
By the way, most mysql clients support multiple actions in a single statement.
A common use for this is (Pseude C# Code)
which will perform an Insert and Return the id of the newly created record.
You possibly could do