<p>I need to select data when a page is viewed and update the 'views' column is there a way to do this in one query, or do I have to use to distinct queries?</p>

[{'answer_id': 43651, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 2, 'selected': False, 'text': '<p>You would have to do this in two statements in one transaction</p>\n\n<pre><code>Begin Tran\n\nUpdate Pages Set Views = Views + 1 Where ID = @ID\nSelect Columns From Pages Where ID = @ID\n\nCommit Tran\n</code></pre>\n'}, {'answer_id': 43664, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 3, 'selected': True, 'text': '<p>If you do not want/need to use a transaction, you could create a stored procedure that first updates the view count and then selects the values and return them to the user.</p>\n'}, {'answer_id': 43677, 'author': 'Neall', 'author_id': 619, 'author_profile': 'https://Stackoverflow.com/users/619', 'pm_score': 0, 'selected': False, 'text': '<p>PostgreSQL\'s <a href="http://www.postgresql.org/docs/8.3/static/sql-update.html" rel="nofollow noreferrer">UPDATE</a> statement has the RETURNING clause that will return a result set like a SELECT statement:</p>\n\n<pre><code>UPDATE mytable\n SET views = 5\n WHERE id = 16\n RETURNING id, views, othercolumn;\n</code></pre>\n\n<p>I\'m pretty sure this is not standard though. I don\'t know if any other databases implement it.</p>\n\n<p>Edit: I just noticed that your question has the "MySQL" tag. Maybe you should mention it in the question itself. It\'s a good generic database question though - I would like to see how to do it in other databases.</p>\n'}, {'answer_id': 44104, 'author': 'SQLMenace', 'author_id': 740, 'author_profile': 'https://Stackoverflow.com/users/740', 'pm_score': 1, 'selected': False, 'text': '<p>It would help if you listed the RDBMS you are using\nSQL Server has the OUTPUT statement</p>\n\n<p>Example</p>\n\n<pre><code>USE AdventureWorks;\nGO\nDECLARE @MyTestVar table (\n    OldScrapReasonID int NOT NULL, \n    NewScrapReasonID int NOT NULL, \n    WorkOrderID int NOT NULL,\n    ProductID int NOT NULL,\n    ProductName nvarchar(50)NOT NULL);\n\nUPDATE Production.WorkOrder\nSET ScrapReasonID = 4\nOUTPUT DELETED.ScrapReasonID,\n       INSERTED.ScrapReasonID, \n       INSERTED.WorkOrderID,\n       INSERTED.ProductID,\n       p.Name\n    INTO @MyTestVar\nFROM Production.WorkOrder AS wo\n    INNER JOIN Production.Product AS p \n    ON wo.ProductID = p.ProductID \n    AND wo.ScrapReasonID= 16\n    AND p.ProductID = 733;\nSELECT OldScrapReasonID, NewScrapReasonID, WorkOrderID, \n    ProductID, ProductName \nFROM @MyTestVar;\nGO\n</code></pre>\n'}, {'answer_id': 719992, 'author': 'Nathan Feger', 'author_id': 8563, 'author_profile': 'https://Stackoverflow.com/users/8563', 'pm_score': 0, 'selected': False, 'text': '<p>I used this trick with Java and SQL Server will also let you send two commands in a single PreparedStatement.</p>\n\n<pre><code>update tablex set y=z where a=b \\r\\n select a,b,y,z from tablex\n</code></pre>\n\n<p>This will need to be in a read committed transaction to work like you think it should though.</p>\n'}]
['https://Stackoverflow.com/questions/43644', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/115/']