如何将两个具有相同字段的表的结果连接到一个字段中?

发布于 2024-10-04 18:56:39 字数 1334 浏览 4 评论 0原文

我有这样的表格:

   Table1                Table2
   name1 | link_id      name2  |  link_id
   text       1         text         2
   text       2         text         4

我想要结果:

name1     name2     link_id
text      text         1
text      text         2
text      text         4

我怎样才能做到这一点?

添加: 抱歉,我的英语不好。我有 device、device_model 和 device_type 表,其中包含重复字段 counter_set_id。我想从 counter_set 中选择包含 counter_set_id 的所有值的字段。我只需要从 counter_set_id 字段中获取值

现在我有这个查询:

SELECT  `dev`.`counter_set_id`, `mod`.`counter_set_id`, `type`.`counter_set_id`
FROM    `device` AS `dev`
LEFT JOIN   `device_model` AS `mod` ON `dev`.`device_model_id` = `mod`.`id`
LEFT JOIN   `device_type` AS `type` ON `mod`.`device_type_id` = `type`.`id`
WHERE   `dev`.`id` = 4;

这会返回 3 列,但我需要一列中的所有值

这是我认为的最终变体:

SELECT  `dev`.`counter_set_id`
FROM        `device` AS `dev` LEFT OUTER JOIN
        `device_model` AS `mod` ON `dev`.`device_model_id` = `mod`.`id`
WHERE   `dev`.`id` = 4 AND
        `dev`.`counter_set_id` IS NOT NULL
UNION
SELECT  `mod`.`counter_set_id`
FROM        `device_model` AS `mod` LEFT OUTER JOIN
        `device` AS `dev` ON `mod`.`id` = `dev`.`device_model_id`
WHERE   `mod`.`counter_set_id` IS NOT NULL;

I have tables like this:

   Table1                Table2
   name1 | link_id      name2  |  link_id
   text       1         text         2
   text       2         text         4

And I wanna have result:

name1     name2     link_id
text      text         1
text      text         2
text      text         4

How I can do this?

ADD:
Sry, my English in not good. I have device, device_model and device_type tables with duplicate field counter_set_id. I wanna select fields from counter_set with all values of counter_set_id. I need to fetch values only from counter_set_id fields

Now I have this query:

SELECT  `dev`.`counter_set_id`, `mod`.`counter_set_id`, `type`.`counter_set_id`
FROM    `device` AS `dev`
LEFT JOIN   `device_model` AS `mod` ON `dev`.`device_model_id` = `mod`.`id`
LEFT JOIN   `device_type` AS `type` ON `mod`.`device_type_id` = `type`.`id`
WHERE   `dev`.`id` = 4;

This returns 3 columns but I need all values in one column

This is final variant I think:

SELECT  `dev`.`counter_set_id`
FROM        `device` AS `dev` LEFT OUTER JOIN
        `device_model` AS `mod` ON `dev`.`device_model_id` = `mod`.`id`
WHERE   `dev`.`id` = 4 AND
        `dev`.`counter_set_id` IS NOT NULL
UNION
SELECT  `mod`.`counter_set_id`
FROM        `device_model` AS `mod` LEFT OUTER JOIN
        `device` AS `dev` ON `mod`.`id` = `dev`.`device_model_id`
WHERE   `mod`.`counter_set_id` IS NOT NULL;

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

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

发布评论

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

评论(7

假面具 2024-10-11 18:56:39

根据您提供的示例表和所需的输出,听起来您可能需要完整的外部连接。并非所有供应商都实现了这一点,但您可以使用 LEFT OUTER 连接和 EXCEPTION 连接的 UNION 来模拟它,并将表颠倒如下:

Select name1, name2, A.link_id
From table1 A Left Outer Join
     table2 B on A.link_id = B.link_id
Union
Select name1, name2, link_id
From table2 C Exception Join
     table1 D on C.link_id = D.link_id

那么您的输出将如下所示:

NAME1   NAME2    LINK_ID
=====   =====    =======
text    <NULL>         1
text    text           2
<NULL>  text           4

Based on the sample tables and desired output you provided, it sounds like you might want a FULL OUTER JOIN. Not all vendors implement this, but you can simulate it with a LEFT OUTER join and a UNION to an EXCEPTION join with the tables reversed like this:

Select name1, name2, A.link_id
From table1 A Left Outer Join
     table2 B on A.link_id = B.link_id
Union
Select name1, name2, link_id
From table2 C Exception Join
     table1 D on C.link_id = D.link_id

then your output would be like this:

NAME1   NAME2    LINK_ID
=====   =====    =======
text    <NULL>         1
text    text           2
<NULL>  text           4
宣告ˉ结束 2024-10-11 18:56:39

起初我认为加入会起作用,但现在我不确定......我正在考虑某种类型的联合。

老实说,在我看来,这是一个糟糕的设计。

select * from table1
union
select * from table2

At first i thought a join would work but now i'm not sure... Im thinking a union of some type.

in all honesty this is a bad design imo.

select * from table1
union
select * from table2
倦话 2024-10-11 18:56:39

我假设您是通过 link_id 字段加入的。正常的连接将导致返回四行(其中两行是相同的)。您真正要求的不是组合字段,而是仅获取不同的行。

只需使用 distinct 关键字即可:

select distinct t1.name1, t2.name2, t1.link_id
from Table1 as t1
inner join Table2 as t2
    on t1.link_id = t2.link_id

I'm assuming that you're joining on the link_id field. A normal join would result in four rows being returned (two of which are identical). What you're really asking for isn't combining fields, but getting only the distinct rows.

Just use the distinct keyword:

select distinct t1.name1, t2.name2, t1.link_id
from Table1 as t1
inner join Table2 as t2
    on t1.link_id = t2.link_id
携余温的黄昏 2024-10-11 18:56:39

name1 的 link_id 4 有什么值?
name2 的 link_id 1 有什么值?

尝试查找不同的 JOIN-Types ..

http://dev.mysql .com/doc/refman/5.0/en/join.html

也许我误解了手头的问题 - 抱歉。

干杯

What value does link_id 4 have for name1?
What value does link_id 1 have for name2?

Try looking up the different JOIN-Types ..

http://dev.mysql.com/doc/refman/5.0/en/join.html

Maybe I misunderstand the question at hand - sorry then.

cheers

属性 2024-10-11 18:56:39

如果三列具有相同的值,则

SELECT  `type`.`counter_set_id`
FROM ...

或者,更好的是,如果它们具有相同的值,您可以这样做:

SELECT  `dev`.`counter_set_id`
FROM    `device` AS `dev`
WHERE   `dev`.`id` = 4;

如果您想连接它们(将它们全部放入同一字段中),请使用以下命令:

SELECT  CONCAT(
   CAST(`dev`.`counter_set_id` AS CHAR),
   ',',
   CAST(`mod`.`counter_set_id` AS CHAR),
   ',',
   CAST(`type`.`counter_set_id` AS CHAR))
FROM ...

If the three columns have the same value, just

SELECT  `type`.`counter_set_id`
FROM ...

Or, better yet, if they have the same value, you can do:

SELECT  `dev`.`counter_set_id`
FROM    `device` AS `dev`
WHERE   `dev`.`id` = 4;

If you want to concatenate them (put them all into the same field), use this:

SELECT  CONCAT(
   CAST(`dev`.`counter_set_id` AS CHAR),
   ',',
   CAST(`mod`.`counter_set_id` AS CHAR),
   ',',
   CAST(`type`.`counter_set_id` AS CHAR))
FROM ...
骄兵必败 2024-10-11 18:56:39

如果您不想重复字段结果,请在查询末尾使用 GROUP BY link_id

如果您只想显示 link_id 字段,则:

SELECT DISTINCT ta.link_id
FROM tblA AS ta
INNER JOIN tblB AS tb
ON ta.link_id = tb.link_id

同时查找 CONCATCAST 和 mysql 手册上的其他有用函数
我希望这对你有帮助。

If you do not want to repeat a field result, use GROUP BY link_id in the end of your query

If you want to show only link_id field then:

SELECT DISTINCT ta.link_id
FROM tblA AS ta
INNER JOIN tblB AS tb
ON ta.link_id = tb.link_id

Also look for CONCAT , CAST and other usefull funcs on mysql manual
I hope this help you.

七七 2024-10-11 18:56:39

将所有 ID 选择到临时表中(不过,这适用于 MySQL - SQL Server 中的 CTE),并将其用作连接表。

Select all the IDs into a temp table (however that works in MySQL - a CTE in SQL Server), and use that as your joining table.

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