我可以在一个查询中更新/选择表吗?

时间:2023-01-13 23:44:04

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?

我需要在查看页面时选择数据并更新“视图”列是否有办法在一个查询中执行此操作,或者我是否必须使用不同的查询?

5 个解决方案

#1


2  

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.

如果您不想/不需要使用事务,则可以创建一个存储过程,该存储过程首先更新视图计数,然后选择值并将其返回给用户。

#2


2  

You would have to do this in two statements in one transaction

您必须在一个事务中的两个语句中执行此操作

Begin Tran

Update Pages Set Views = Views + 1 Where ID = @ID
Select Columns From Pages Where ID = @ID

Commit Tran

#3


1  

It would help if you listed the RDBMS you are using SQL Server has the OUTPUT statement

如果您列出使用SQL Server的RDBMS具有OUTPUT语句,将会有所帮助

Example

USE AdventureWorks;
GO
DECLARE @MyTestVar table (
    OldScrapReasonID int NOT NULL, 
    NewScrapReasonID int NOT NULL, 
    WorkOrderID int NOT NULL,
    ProductID int NOT NULL,
    ProductName nvarchar(50)NOT NULL);

UPDATE Production.WorkOrder
SET ScrapReasonID = 4
OUTPUT DELETED.ScrapReasonID,
       INSERTED.ScrapReasonID, 
       INSERTED.WorkOrderID,
       INSERTED.ProductID,
       p.Name
    INTO @MyTestVar
FROM Production.WorkOrder AS wo
    INNER JOIN Production.Product AS p 
    ON wo.ProductID = p.ProductID 
    AND wo.ScrapReasonID= 16
    AND p.ProductID = 733;
SELECT OldScrapReasonID, NewScrapReasonID, WorkOrderID, 
    ProductID, ProductName 
FROM @MyTestVar;
GO

#4


0  

PostgreSQL's UPDATE statement has the RETURNING clause that will return a result set like a SELECT statement:

PostgreSQL的UPDATE语句具有RETURNING子句,它将返回类似SELECT语句的结果集:

UPDATE mytable
 SET views = 5
 WHERE id = 16
 RETURNING id, views, othercolumn;

I'm pretty sure this is not standard though. I don't know if any other databases implement it.

我很确定这不是标准的。我不知道是否有其他数据库实现它。

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.

编辑:我刚注意到你的问题有“MySQL”标签。也许你应该在问题本身中提到它。这是一个很好的通用数据库问题 - 我想看看如何在其他数据库中做到这一点。

#5


0  

I used this trick with Java and SQL Server will also let you send two commands in a single PreparedStatement.

我在Java中使用了这个技巧,SQL Server也允许你在一个PreparedStatement中发送两个命令。

update tablex set y=z where a=b \r\n select a,b,y,z from tablex

This will need to be in a read committed transaction to work like you think it should though.

这将需要在读取已提交的事务中工作,就像您认为的那样。

#1


2  

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.

如果您不想/不需要使用事务,则可以创建一个存储过程,该存储过程首先更新视图计数,然后选择值并将其返回给用户。

#2


2  

You would have to do this in two statements in one transaction

您必须在一个事务中的两个语句中执行此操作

Begin Tran

Update Pages Set Views = Views + 1 Where ID = @ID
Select Columns From Pages Where ID = @ID

Commit Tran

#3


1  

It would help if you listed the RDBMS you are using SQL Server has the OUTPUT statement

如果您列出使用SQL Server的RDBMS具有OUTPUT语句,将会有所帮助

Example

USE AdventureWorks;
GO
DECLARE @MyTestVar table (
    OldScrapReasonID int NOT NULL, 
    NewScrapReasonID int NOT NULL, 
    WorkOrderID int NOT NULL,
    ProductID int NOT NULL,
    ProductName nvarchar(50)NOT NULL);

UPDATE Production.WorkOrder
SET ScrapReasonID = 4
OUTPUT DELETED.ScrapReasonID,
       INSERTED.ScrapReasonID, 
       INSERTED.WorkOrderID,
       INSERTED.ProductID,
       p.Name
    INTO @MyTestVar
FROM Production.WorkOrder AS wo
    INNER JOIN Production.Product AS p 
    ON wo.ProductID = p.ProductID 
    AND wo.ScrapReasonID= 16
    AND p.ProductID = 733;
SELECT OldScrapReasonID, NewScrapReasonID, WorkOrderID, 
    ProductID, ProductName 
FROM @MyTestVar;
GO

#4


0  

PostgreSQL's UPDATE statement has the RETURNING clause that will return a result set like a SELECT statement:

PostgreSQL的UPDATE语句具有RETURNING子句,它将返回类似SELECT语句的结果集:

UPDATE mytable
 SET views = 5
 WHERE id = 16
 RETURNING id, views, othercolumn;

I'm pretty sure this is not standard though. I don't know if any other databases implement it.

我很确定这不是标准的。我不知道是否有其他数据库实现它。

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.

编辑:我刚注意到你的问题有“MySQL”标签。也许你应该在问题本身中提到它。这是一个很好的通用数据库问题 - 我想看看如何在其他数据库中做到这一点。

#5


0  

I used this trick with Java and SQL Server will also let you send two commands in a single PreparedStatement.

我在Java中使用了这个技巧,SQL Server也允许你在一个PreparedStatement中发送两个命令。

update tablex set y=z where a=b \r\n select a,b,y,z from tablex

This will need to be in a read committed transaction to work like you think it should though.

这将需要在读取已提交的事务中工作,就像您认为的那样。