简体   繁体   English

我想更新mysql表column1,除非数据存在,然后移至下一个column2

[英]I want to update mysql table column1 unless data exists then move to next column2

I want to update my MySQL table. 我想更新我的MySQL表。 What I want to do is update column1 unless data exists in column1 then move it onto column2 and update that one instead. 我想要做的就是更新column1除非存在数据column1然后将其移动到column2和更新一个来代替。 The code that I currently have is constantly updating the first column. 我当前拥有的代码正在不断更新第一列。

UPDATE users
SET    active = 1,
       time_started = '$_POST[in_time]'
WHERE  user_id = '$_POST[user_id]

Could anyone help on this? 有人可以帮忙吗?

Something like: 就像是:

update users set active=1, time_in_1=if(time_started is null,time_in_1,?), time_started=if(time_started is null,?,time_started) while ...

In effect, when time_started is null, the above does: 实际上,当time_started为null时,上述操作会执行以下操作:

update users set time_in_1=time_in_1, time_started=?

(leaving time_in_1 unchanged; ? is a placeholder for your new value). (保留time_in_1不变;?是您的新值的占位符)。 If time_started is not null, it does: 如果time_started不为null,则执行以下操作:

update users set time_in_1=?, time_started=time_started

(leaving time_started unchanged). (保持time_started不变)。 In either case, you need to provide your new value twice (assuming there is only one; maybe you want to update the second column to a different value than you would have updated the first column to?). 在这两种情况下,您都需要提供两次新值(假设只有一个;可能要将第二列更新为与将第一列更新为不同的值?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM