如何将2个临时表添加在一起

发布于 2024-09-10 05:53:50 字数 580 浏览 6 评论 0原文

如果我要创建临时表,该表有 2 列。 idscore。我想把它们加在一起。

我想要添加它们的方式是,如果它们各自包含相同的id,那么我不想重复id,而是将分数添加在一起。

如果我有 2 个名为 t1t2 的临时表,

并且 t1 had:

id 3 score 4
id 6 score 7

t2 had:

id 3 score 5
id 5 score 2

我最终会一个新的临时表包含:

id 3 score 9
id 5 score 2
id 6 score 7

我想这样做的原因是,我正在尝试构建产品搜索。我有一些我想使用的算法,1 使用全文,另一个不使用。我想使用这两种算法,所以我想创建一个基于算法 1 的临时表和一个基于算法 2 的临时表。然后将它们结合起来。

If I am creating temporary tables, that have 2 columns. id and score. I want to to add them together.

The way I want to add them is if they each contain the same id then I do not want to duplicate the id but instead add the scores together.

if I have 2 temp tables called t1 and t2

and t1 had:

id 3 score 4
id 6 score 7

and t2 had:

id 3 score 5
id 5 score 2

I would end up with a new temp table containing:

id 3 score 9
id 5 score 2
id 6 score 7

The reason I want to do this is, I am trying to build a product search. I have a few algorithms I want to use, 1 using fulltext another not. And I want to use both algorithms so I want to create a temporary table based on algorithm1 and a temp table based on algorithm2. Then combine them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

挽袖吟 2024-09-17 05:53:50

怎么样:

SELECT id, SUM(score) AS score FROM (
  SELECT id, score FROM t1
  UNION ALL
  SELECT id, score FROM t2
) t3
GROUP BY id

How about:

SELECT id, SUM(score) AS score FROM (
  SELECT id, score FROM t1
  UNION ALL
  SELECT id, score FROM t2
) t3
GROUP BY id
司马昭之心 2024-09-17 05:53:50

这未经测试,但您应该能够对两个表执行并集,然后对结果执行选择,对字段进行分组并添加分数

SELECT id,SUM(score) FROM
(
    SELECT id,score FROM t1
    UNION ALL
    SELECT id,score FROM t2
) joined
GROUP BY id

This is untested but you should be able to perform a union on the two tables and then perform a select on the results, grouping the fields and adding the scores

SELECT id,SUM(score) FROM
(
    SELECT id,score FROM t1
    UNION ALL
    SELECT id,score FROM t2
) joined
GROUP BY id
嘦怹 2024-09-17 05:53:50

对 ID 执行完全外部联接。将值合并为 0 后,选择 ID 和两个“分数”列的总和。

Perform a full outer join on the ID. Select on the ID and the sum of the two "score" columns after coalescing the values to 0.

ぃ双果 2024-09-17 05:53:50
SELECT id, SUM(score) FROM
(
    SELECT id, score FROM #t1
    UNION ALL
    SELECT id, score FROM #t2
) AS Temp
GROUP BY id
SELECT id, SUM(score) FROM
(
    SELECT id, score FROM #t1
    UNION ALL
    SELECT id, score FROM #t2
) AS Temp
GROUP BY id
梦回旧景 2024-09-17 05:53:50
select id, sum(score)
from (
 select * from table 1
 union all
 select * from table2
) tables
group by id
select id, sum(score)
from (
 select * from table 1
 union all
 select * from table2
) tables
group by id
铜锣湾横着走 2024-09-17 05:53:50

您需要创建这两个表的并集,然后您可以轻松地将结果分组。

SELECT id, sum(score) FROM 

(
 SELECT id, score FROM t1
 UNION 
 SELECT id, score FROM t2
) as tmp
GROUP BY id;

You need to create an union of those two tables then You can easily group the results.

SELECT id, sum(score) FROM 

(
 SELECT id, score FROM t1
 UNION 
 SELECT id, score FROM t2
) as tmp
GROUP BY id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文