MySQL:返回更新的行

Squ*_*art 14 mysql return

我试图在扭曲的python中组合这两个查询:

SELECT * FROM table WHERE group_id = 1013 and time > 100;
Run Code Online (Sandbox Code Playgroud)

和:

UPDATE table SET time = 0 WHERE group_id = 1013 and time > 100
Run Code Online (Sandbox Code Playgroud)

进入单个查询.有可能这样做吗?

我尝试将SELECT放在子查询中,但我不认为整个查询会返回我想要的内容.

有没有办法做到这一点?(更好,没有子查询)或者我只需坚持两个查询?

谢谢,

小智 10

这确实是迟到了,但我也遇到了同样的问题,我发现最有帮助的解决方案如下:

SET @uids := null;
UPDATE footable
   SET foo = 'bar'
 WHERE fooid > 5
   AND ( SELECT @uids := CONCAT_WS(',', fooid, @uids) );
SELECT @uids;
Run Code Online (Sandbox Code Playgroud)

来自https://gist.github.com/PieterScheffers/189cad9510d304118c33135965e9cddb


AW1*_*101 8

显然mysql确实有一些可能有用的东西,特别是如果你只更新一行.

此示例来自:http://lists.mysql.com/mysql/219882

UPDATE mytable SET
mycolumn = @mycolumn := mycolumn + 1
WHERE mykey = 'dante';

SELECT @mycolumn;
Run Code Online (Sandbox Code Playgroud)

我从来没有试过这个,但请告诉我你是怎么做的.


jue*_*n d 4

您不能直接组合这些查询。但是您可以编写一个执行这两个查询的存储过程。例子:

delimiter |
create procedure upd_select(IN group INT, IN time INT)
begin
    UPDATE table SET time = 0 WHERE group_id = @group and time > @time;
    SELECT * FROM table WHERE group_id = @group and time > @time;
end;
|
delimiter ;
Run Code Online (Sandbox Code Playgroud)

  • 更新后,Squall Leohart 想要返回的行的时间 = 0,因此条件为 time > @time 的 SELECT 将无法按预期工作。 (5认同)