在MYSQL中联接两个不相关的表 [英] Joining two unrelated tables in MYSQL
本文介绍了在MYSQL中联接两个不相关的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我有两个桌子:
表1
sku | 产品名称 | 颜色 |
1a | product1 | 黑色 |
1b | product2 | red |
表2
sku | 产品名称 | 尺寸 |
2a | product3 | 10x10x30 |
2b | product4 | 20x25x50 |
这些表属于不同的产品类别类型. sku 是每个表的主键.我需要一个主表,其中包含每个类别类型的所有产品.我需要的主表如下:
主表
sku | 产品名称 | 颜色 | 尺寸 |
1a | product1 | 黑色 | |
1b | product2 | 红色 | |
2a | product3 | 10x10x30 | |
2b | product4 | 20x25x50 |
如何加入Table1和Table2以获得主表?
谢谢.
Hello,
I have two tables:
Table 1
sku | product_name | color |
1a | product1 | black |
1b | product2 | red |
Table 2
sku | product_name | dimensions |
2a | product3 | 10x10x30 |
2b | product4 | 20x25x50 |
These tables belong to different product category types. sku is a primary key for each table. I need a master table that contains all products from every category type. The master table that I need is like:
Master Table
sku | product_name | color | dimensions |
1a | product1 | black | |
1b | product2 | red | |
2a | product3 | 10x10x30 | |
2b | product4 | 20x25x50 |
How can I join Table1 and Table2 to get Master Table?
Thank you.
推荐答案
使用联合查询.
例如:
Use a union query.
eg:
select sku, product_name, color, '' as dimensions from [table 1]
union all
select sku, product_name, '' as color, dimensions from [table 2]
这篇关于在MYSQL中联接两个不相关的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文