我正在寻找一种在两个(或更多)不同表中查找值时创建相对相关函数的方法。所以我有这样的表
table1:
id weight
1 0.1
2 0.15
3 0.12
6 0.21
table2:
id weight
3 0.09
6 0.2
8 0.1
11 0.13
我需要通过合并这两个表来获得相关函数。相同的行 ID 将获得 10 倍的相关性,而只有来自一个表的 ID 的行将获得“权重”相关性。
这是我需要得到的中间表(我的问题是如何制作这样的表):
id1 weight1 id2 weight2
1 0.1 null null
2 0.15 null null
3 0.12 3 0.09
6 0.21 6 0.2
null null 8 0.1
null null 11 0.13
使用这个表我可以计算任何我需要的相关性,但问题是从这两个创建这样的表。你能帮帮我吗?
我尝试了 LEFT JOIN、STRAIGHT_JOIN、LEFT OUTER JOIN,但它们产生了截然不同的结果。
编辑:如果重要的话,我目前设想的决赛 table 看起来像这样:
id relevance
1 0.1
2 0.15
3 2.1
6 4.1
8 0.1
11 0.13
最佳答案
您可以为此使用FULL OUTER JOIN
,例如:
SELECT t1.id AS id1, t1.weight AS weight1, t2.id AS id2, t2.weight AS weight2
FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id
UNION
SELECT t1.id AS id1, t1.weight AS weight1, t2.id AS id2, t2.weight AS weight2
FROM table1 t1 RIGHT JOIN table2 t2 ON t1.id = t2.id;
关于mysql - 左连接在 MySQL 中将为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54824110/