连接两个表并保存到Third-sql [英] Join two tables and save into third-sql

查看:103
本文介绍了连接两个表并保存到Third-sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加入两个表.

+-------------------+--------+
| wordA(primarykey) | countA |
+-------------------+--------+
| abc               |     25 |
| abcd              |     29 |
| abcde             |     45 |
+-------------------+--------+


TableB

+-------------------+--------+
| wordB(primarykey) | countB |
+-------------------+--------+
| ab                |     10 |
| abc               |     40 |
| abcde             |     90 |
| abcdef            |     55 |
+-------------------+--------+


所需的输出:

TableC


Desired output:

TableC

+--------+--------+--------+
|  word  | countA | countB |
+--------+--------+--------+
| ab     |      0 |     10 |
| abc    |     25 |    40  |
| abcd   |     29 |      0 |
| abcde  |     45 |     90 |
| abcdef |      0 |     55 |
+--------+--------+--------+


我想在TableC中插入所需输出的值.请提供一些代码.我尝试了此操作,但是出现的问题是我无法合并wordA和wordB.


I want to insert values of the desired output in TableC. Please provide some code. I tried this but the problem which I am getting is that I am not able to merge wordA and wordB.

推荐答案

尝试一下

仅针对MYSQL编辑

SQL小提琴DEMO

Insert into TableC(word , countA , countB)
Select IFNULL(TableA.wordA , TableB.wordB) as word ,
IFNULL(TableA.countA , 0) as countA , IFNULL(TableB.countB , 0) as countB 
from TableA LEFT join TableB on TableA.wordA = TableB.wordB
Union
Select IFNULL(TableA.wordA , TableB.wordB) as word ,
IFNULL(TableA.countA , 0) as countA , IFNULL(TableB.countB , 0)
from TableA RIGHT join TableB on TableA.wordA = TableB.wordB;

这篇关于连接两个表并保存到Third-sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆