在单个查询中同时选择和更新:更新位于不同的列

时间:2017-07-12 06:51:21

标签: mysql

以下是我的查询

select c.*,u.fullname,u.userphoto from chat_messages
   c left join us_signup u on u.id=c.fromid 
   where c.fromid in(?,?) and c.toid in(?,?) and c.jobid=? 
   order by c.received_date asc

chat_messages表中有一个名为“readi”的列我想要更新

我已经提到了:

How to UPDATE and SELECT at the same time in MySQL

Is there a way to SELECT and UPDATE rows at the same time?

等..但无法修改我的查询..

1 个答案:

答案 0 :(得分:1)

The answers you linked to are both for MS SQL Server (In the MySQL tagged question read the comments). In MySQL there is no OUTPUT clause or whatever. The only way I can think of is using user-defined variables.

For a single row:

SET @v := NULL;
UPDATE your_table
SET column_name = 'whatever'
WHERE another_column = 'foo'
AND @v := column_you_want_to_store
LIMIT 1; 
SELECT @v;

For multiple rows:

SET @v := '';
UPDATE your_table
SET column_name = 'whatever'
WHERE another_column = 'foo'
AND @v := (SELECT CONCAT_WS(', ', column_you_want_to_store, @v)); 
SELECT @v;
相关问题