MySQL 在一个查询中更新和选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4057244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:35:18  来源:igfitidea点击:

Update and select in one query

sqlmysql

提问by william

I found similar questions with correct answers. But they're a bit complicated for me. I just want a simple basic statement.

我发现了类似的问题,答案是正确的。但它们对我来说有点复杂。我只想要一个简单的基本陈述。

I have:

我有:

string sql = "UPDATE tblPopUp 
                 SET PopUp = 'False' 
               WHERE DisplayNo = 1"

...and:

...和:

string sql1 = "SELECT Period  
                 FROM tblPopUp 
                WHERE DisplayNo = 1"

How can I combine them?

我怎样才能把它们结合起来?

回答by Subhash

UPDATE tblPopUp  
SET PopUp = 'False', Period = Period  
OUTPUT DELETED.Period
WHERE DisplayNo = 1

For more information about OUTPUT clause please check this post.

有关 OUTPUT 子句的更多信息,请查看这篇文章

回答by OMG Ponies

You can't.

你不能。

There's no convention in a SQL UPDATE statement for returning data. And vice versa -- a SELECT statement doesn't write information to a table.

SQL UPDATE 语句中没有返回数据的约定。反之亦然——SELECT 语句不会将信息写入表。

If you've found questions/answers that you feel are similar to what you want, please provide links.

如果您发现问题/答案与您想要的相似,请提供链接。

回答by Lafras Henning

The correct way to do this (now for MySQL 5+), would be with a stored procedure.

执行此操作的正确方法(现在适用于 MySQL 5+)是使用存储过程。

回答by Marek Bernád

Old Q, but still in usage, for psql solution, try this:

老Q,还在用,psql的解决办法,试试这个:

UPDATE table SET column = value
WHERE condition
RETURNING column;

回答by mahesh

Try This

尝试这个

UPDATE tblPopUp 
             SET PopUp = 'False' 
           WHERE DisplayNo = '1'
(
SELECT Period  
             FROM tblPopUp 
            WHERE DisplayNo = '1'
)