且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将数据从一个表复制到另一个具有特定条件的表

更新时间:2023-11-27 22:43:34

您在条件 consignment.id >id1 下的 LEFT JOIN 几乎是在创建 catesian 产品.您可能想要的是仅插入源表中 id 比目标表中最高 id1 更高的行.您应该使用 SELECT MAX(id) 子查询:

Your LEFT JOIN with the condition consignment.id >id1 is almost creating a catesian product. What you probably want, is to insert only rows with a higher id from the source table than the highest id1 in the destination table. You should use a SELECT MAX(id) subquery instead:

SELECT [..]
FROM  `eamglo5_singaporelive`.`consignment` 
WHERE `eamglo5_singaporelive`.`consignment`.`processed`=1 
  and `eamglo5_singaporelive`.`consignment`.date_booked>'2018-07-17'
  and `eamglo5_singaporelive`.`consignment`.id > (
     SELECT MAX(id1) FROM eamglo5_billingsystem.`consignment`
  )