两个表,更新特定的列和/或如果不存在则插入新记录

时间:2018-09-10 05:23:56

标签: php mysql

我有两个表: NewData RecentData

      NewData

id | Order_Number | Company | SerialNumber | ShipDate
-----------------------------------------------------
 1 | 2323         | Apple   |  SN2324      | 2018-09-12 00:00:00
 2 | 53444        | Telsa   |  SN9098      | 2018-09-12 00:00:00
 3 | 42321        | Stack   |  SN9999      | 2018-09-12 00:00:00



      RecentData

id | Order_Number | Company | SerialNumber | ShipDate
-----------------------------------------
 1 | 2323         | Apple   |              | 2018-05-05 00:00:00
 2 | 53444        | Telsa   | oldserial    | 2018-07-20 00:00:00

NewData 表上更改 ShipDate 后,我只需要 update ShipDate 和<最近数据表上的em> SerialNumber 列。另外,如果在NewData表上确实存在Order_Number,而在LatestData表上却存在DOESNT,则将整行添加到LatestData表中。

我尝试了以下操作:

UPDATE RecentData
LEFT JOIN NewData on NewData.Order_Number = RecentData.Order_Number
SET RecentData.Order_Number = NewData.Order_Number;

但这会删除Order_Number列中的订单号。

请帮助!

0 个答案:

没有答案