将列日期设置为另一个表日期列加30天

时间:2014-01-08 20:57:51

标签: mysql sql sql-update

我有两张桌子:

newtable的

ID____PersonID___Criteria___TimeStamp
1_____456________1__________01/08/2014
2_____287________0__________01/08/2014
3_____359________0__________01/08/2014

代码PersonTable

ID______LastLogin
456_____11/08/2013
287_____10/25/2013
359_____09/15/2013

我正在尝试做的是将NewTable.TimeStamp更新为等于PersonTable.LoginDate PLUS 30天,其中NewTable.Criteria = 0

期望的结果

newtable的

ID____PersonID___Criteria___TimeStamp
1_____456________1__________01/08/2014
2_____287________0__________11/24/2013
3_____359________0__________10/15/2013

我尝试了什么,虽然这些似乎没有做任何事情:

UPDATE NewTable AS NT
    INNER JOIN PersonTable AS PT ON PT.ID = NT.PersonID AND NT.Criteria = 0
SET NT.TimeStamp = DATE_ADD(PT.LastLogin, INTERVAL 30 DAY);

- 我已经尝试了

UPDATE `NewTable` AS NT, PersonTable AS PT
SET NT.TimeStamp = DATE_ADD(PT.LastLogin, INTERVAL 30 DAY)
WHERE NT.PersonID = PT.ID
    AND NT.Criteria = 0;

2 个答案:

答案 0 :(得分:0)

UPDATE  NewTable AS NT INNER JOIN PersonTable AS PT 
ON PT.ID = NT.UserID 
SET NT.TimeStamp = DATE_ADD(PT.LastLogin, INTERVAL 30 DAY)
WHERE NT.Criteria = 0

答案 1 :(得分:0)

您必须在第一个查询中修改某些内容

UPDATE NewTable AS NT
INNER JOIN PersonTable AS PT 
ON PT.ID = NT.PersonID 
AND NT.Criteria = 0
SET NT.TimeStamp = DATE_ADD(PT.LastLogin, INTERVAL 30 DAY);

LIVE DEMO HERE