dpf56454 2013-10-21 19:11
浏览 15
已采纳

比较2个MySQL表并将新行移动到第二个表

This is my current scenario:

I have two tables (TB1 & TB2) that are exactly the same exept for one column. I have a CRON that copies everything from a remote SQL database and refreshes everything on TB1 dropping the current rows and inserting the new ones, so TB1 is my source table.

On TB2 I have the same exact structure as TB1 but the last row changes from true to false, the default is false. See the sample tables below:


TB1
+---------------+---------------+---------------+---------------+
| id            | col1          | col2          |col3           |
+---------------+---------------+---------------+---------------+
| 1             | value1        | val-A         |false          |
| 2             | value2        | val-B         |false          |
| 3             | value1        | val-C         |false          |
| 4             | value3        | val-D         |false          |
| 5             | value4        | val-E         |false          |
+---------------+---------------+---------------+---------------+



TB2
+---------------+---------------+---------------+---------------+
| id            | col1          | col2          |col3           |
+---------------+---------------+---------------+---------------+
| 1             | value1        | val-A         |true           |
| 2             | value2        | val-B         |false          |
| 3             | value1        | val-C         |true           |
| 4             | value3        | val-D         |false          |
+---------------+---------------+---------------+---------------+

As you can see the TB1 has an extra row id=5. The combination of both col1 and col2 is unique and that's what I'm trying to detect, so any new combination of both TB1.col1 AND TB1.col2 should be copied over to TB2 while keeping TB2.col3.

I tried the following and it doesn't really work in my case because it only shows all different values not the combined difference:

SELECT TB1.col1, TB1.col2 FROM (
SELECT TB1.col1, TB1.col2 FROM TB1
UNION ALL
SELECT TB2.col1, TB2.col2 FROM TB2
) TB1
GROUP BY TB1.col1
HAVING count(*) = 1
ORDER BY TB1.col1

I know I'm missing the INSERT part but any help on both or at least the SELECT will be really appreciated.

Thanx!

  • 写回答

3条回答 默认 最新

  • duanrong0738 2013-10-21 19:29
    关注

    Don't think Minus is available in my sql but an outer join should work

    http://sqlfiddle.com/#!2/e73e4/1/0

    Insert into tb2 (
    Select tb1.ID, tb1.Col1, tb1.Col2, tb1.Col3 from tb1 
    LEFT JOIN tb2 on Tb1.col1=tb2.col1 and Tb1.col2=tb2.col2
    Where tb2.col1 is null);
    

    But I'm likely missing something you were after...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?