三向全外连接/表合并
我想在三个表上执行FULL OUTER JOIN
,合并公共行。
SELECT * FROM Users
id Username Fullname
== ======== =====================
7 iboyd Ian Boyd
8 nicholle Michelle Karnac
10 jamie Jimmy Chew
3 row(s) affected
SELECT * FROM GrobUsers
id Username Fullname
== ======== =====================
7 iboyd Ian Steven Boyd
8 nicholle Michelle Baker
9 chris Kris Kallme
3 row(s) affected
SELECT * FROM FrobUsers
id Username Fullname
== ======== =====================
7 ian Ian
9 chris Kris K.
10 jamie Jimmy Chew
3 row(s) affected
我想根据 id
列合并表格。
这就提出了我该如何做的问题 当对方希望冲突得到解决时 列值不同。算法 可用于解决用户名和全名之间冲突的方法是:
if (id 相等) then 选择一个;我不在乎
我尝试了以下方法:
SELECT
COALESCE(Users.id, GrobUsers.id, FrobUsers.id) AS id,
COALESCE(Users.Username, GrobUsers.Username, FrobUsers.Username) AS Username,
COALESCE(Users.FullName, GrobUsers.FullName, FrobUsers.FullName) AS Fullname
FROM Users
FULL OUTER JOIN GrobUsers ON GrobUsers.id = Users.id
FULL OUTER JOIN FrobUsers ON FrobUsers.id = .....something......
i want to perform a FULL OUTER JOIN
, merging common rows, on three tables.
SELECT * FROM Users
id Username Fullname
== ======== =====================
7 iboyd Ian Boyd
8 nicholle Michelle Karnac
10 jamie Jimmy Chew
3 row(s) affected
SELECT * FROM GrobUsers
id Username Fullname
== ======== =====================
7 iboyd Ian Steven Boyd
8 nicholle Michelle Baker
9 chris Kris Kallme
3 row(s) affected
SELECT * FROM FrobUsers
id Username Fullname
== ======== =====================
7 ian Ian
9 chris Kris K.
10 jamie Jimmy Chew
3 row(s) affected
i want to merge the tables based on the id
column.
This brings up the issue of how do i
want conflicts resolved when the other
column values differ. The algorithm
that may be applied to resolve conflicts between Usernames and FullName is:if (id's are equal) then pick one; i don't care
i've tried something along the lines of:
SELECT
COALESCE(Users.id, GrobUsers.id, FrobUsers.id) AS id,
COALESCE(Users.Username, GrobUsers.Username, FrobUsers.Username) AS Username,
COALESCE(Users.FullName, GrobUsers.FullName, FrobUsers.FullName) AS Fullname
FROM Users
FULL OUTER JOIN GrobUsers ON GrobUsers.id = Users.id
FULL OUTER JOIN FrobUsers ON FrobUsers.id = .....something......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个典型的技巧——使用没有意义的聚合函数。
嗯...但是它可能会从一个表中选择用户名并从另一表中选择全名。如果您仍然不在乎,请使用它,否则......也许
A typical trick -- use an aggregation function that doesn't make sense.
Hmmm... but then it may select user name from one table and full name from another table. If you still don't care, use that, otherwise... maybe
在您给出的示例中,您根本不需要联接。我希望这是一个真实的例子,而不是一个人为的例子。这里你想要做的非常简单,你根本不需要连接,也不需要 row_number。你可以这样做:
它会给你这个:
这是我使用的测试用例:
In the example you are giving you do not need joins at all. I hope, that this is a real example and not a contrived one. What you want to do is very simple here, you don't need joins at all and you don't need row_number. You can do it like this:
And it will give you this:
Here is the test case I used: