SQL - 使用多个表中的sql命令“UNION”创建视图 [英] SQL - Create view using sql command “UNION” from multiple tables
本文介绍了SQL - 使用多个表中的sql命令“UNION”创建视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 3 表 示例tb1,tb2,tb3并且每个表包含相同的数据用于不同的公司,所以我想获取所有 3 表数据 视图当 i 执行 union它还播放重复数据。 的原因是,每个列中没有相同的数据 重复的行。如下,任何人都可以帮助我。我想只需要一行来自
以下注:emp_no 是主键
ex : -
emp_no emp_name emp_addr emp_ph
101 abcd xloc 000000 - 来自 tbl1
101 abcd null 000000 - 来自 tbl2
解决方案
基本上,您不应该为不同的公司使用不同的表 - 您有一个表包含指向单独表的外键链接,该表只标识公司:
公司
<前lang =text> Id描述
1 Joes Wheels
2 Mikes Tires
3 Simons Hubs
DataTable:
Id CompanyId EmpNo EmpName ...
1 1 101 abcd
2 3 101 abcd
3 1 102 qwer
...这样,你可以识别真正的重复,并使用JOIN和WHERE而不是UNION仅提取每个公司所需的信息
这也使得添加第四个更加灵活公司下周如果你需要。
i have 3 tables for example tb1,tb2,tb3 and each table include same data for different different company, so i want to get all 3 table data in a view when i do "union" its sowing duplicate data also. the reason is, not having same data in each column for duplicate rows. like below, can any one please help me. i want only one row from below
Note: emp_no is primary key
ex:-
emp_no emp_name emp_addr emp_ph
101 abcd xloc 000000 -- from tbl1
101 abcd null 000000 -- from tbl2
解决方案
Basically, you shouldn't have different tables for different companies - you have have one table which contains a foreign key link to a separate table which just identifies the companies:
Companies
Id Description 1 Joes Wheels 2 Mikes Tyres 3 Simons Hubs
DataTable:
Id CompanyId EmpNo EmpName ... 1 1 101 abcd 2 3 101 abcd 3 1 102 qwer ...That way, you can identify "genuine" duplicates, and extract only the info you need for each company using JOIN and WHERE instead of UNION
This also makes it a lot more flexible to add a fourth company next week if you need to.
这篇关于SQL - 使用多个表中的sql命令“UNION”创建视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文