mysql 加入问题
mysql> select * from new_table;
+-------------+
| idnew_table |
+-------------+
| 1 |
| 2 |
+-------------+
mysql> select * from second_table;
+----------------+--------+
| idsecond_table | second |
+----------------+--------+
| 100 | 1 |
| 150 | 1 |
| 200 | 2 |
+----------------+--------+
mysql> select * from third;
+---------+-------+
| idthird | third |
+---------+-------+
| 500 | 1 |
| 600 | 2 |
+---------+-------+
我需要将它连接到一个,就像这样
+-----+-----+--------+------+------------+
| tid | sid | secsec | thid | thirdthird |
+-----+-----+--------+------+------------+
| 1 | 100 | 1 | 500 | 1 |
| 1 | 150 | 1 | null | null |
| 2 | 200 | 2 | 600 | 2 |
+-----+-----+--------+------+------------+
我正在尝试这个查询
select
t.idnew_table as tid,
sec.idsecond_table as sid ,
sec.second as secsec,
th.idthird as thid,
th.third as thirdthird
from
new_table t
join second_table sec on sec.second = t.idnew_table
join third th on th.third = t.idnew_table
,但它重复第三个表中的行,
+-----+-----+--------+------+------------+
| tid | sid | secsec | thid | thirdthird |
+-----+-----+--------+------+------------+
| 1 | 100 | 1 | 500 | 1 |
| 1 | 150 | 1 | 500 | 1 |
| 2 | 200 | 2 | 600 | 2 |
+-----+-----+--------+------+------------+
所以我需要你的建议
mysql> select * from new_table;
+-------------+
| idnew_table |
+-------------+
| 1 |
| 2 |
+-------------+
mysql> select * from second_table;
+----------------+--------+
| idsecond_table | second |
+----------------+--------+
| 100 | 1 |
| 150 | 1 |
| 200 | 2 |
+----------------+--------+
mysql> select * from third;
+---------+-------+
| idthird | third |
+---------+-------+
| 500 | 1 |
| 600 | 2 |
+---------+-------+
and i need to join it to one, like this
+-----+-----+--------+------+------------+
| tid | sid | secsec | thid | thirdthird |
+-----+-----+--------+------+------------+
| 1 | 100 | 1 | 500 | 1 |
| 1 | 150 | 1 | null | null |
| 2 | 200 | 2 | 600 | 2 |
+-----+-----+--------+------+------------+
I'm trying this query
select
t.idnew_table as tid,
sec.idsecond_table as sid ,
sec.second as secsec,
th.idthird as thid,
th.third as thirdthird
from
new_table t
join second_table sec on sec.second = t.idnew_table
join third th on th.third = t.idnew_table
but it repeats rows from third table like this
+-----+-----+--------+------+------------+
| tid | sid | secsec | thid | thirdthird |
+-----+-----+--------+------+------------+
| 1 | 100 | 1 | 500 | 1 |
| 1 | 150 | 1 | 500 | 1 |
| 2 | 200 | 2 | 600 | 2 |
+-----+-----+--------+------+------------+
so i need your advice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的评论和提供的数据,我猜您需要第二个和第三个表的
FULL OUTER JOIN
,基于行号和ID(second_table.second
和< code>third_table.third),然后(通常)INNER JOIN
到第一个表。不幸的是,表中不存在行号,并且 MYSQL 不提供窗口函数。因此这是可能的,但有这样可怕的事情:
From your comments and the data you provided, I guess that you need a
FULL OUTER JOIN
of the second and third table, based in rownumbers and ids (second_table.second
andthird_table.third
) and then a (usual)INNER JOIN
to the first table.Unforunately no row numbers exist in the tables and MYSQL doesn't provide windowing functions. Therefore it's possible but with something horrible like this: