如何将两个具有相同字段的表的结果连接到一个字段中?
我有这样的表格:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据您提供的示例表和所需的输出,听起来您可能需要完整的外部连接。并非所有供应商都实现了这一点,但您可以使用 LEFT OUTER 连接和 EXCEPTION 连接的 UNION 来模拟它,并将表颠倒如下:
那么您的输出将如下所示:
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:
then your output would be like this:
起初我认为加入会起作用,但现在我不确定......我正在考虑某种类型的联合。
老实说,在我看来,这是一个糟糕的设计。
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.
我假设您是通过
link_id
字段加入的。正常的连接将导致返回四行(其中两行是相同的)。您真正要求的不是组合字段,而是仅获取不同的行。只需使用
distinct
关键字即可: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: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
如果三列具有相同的值,则
或者,更好的是,如果它们具有相同的值,您可以这样做:
如果您想连接它们(将它们全部放入同一字段中),请使用以下命令:
If the three columns have the same value, just
Or, better yet, if they have the same value, you can do:
If you want to concatenate them (put them all into the same field), use this:
如果您不想重复字段结果,请在查询末尾使用
GROUP BY link_id
如果您只想显示
link_id
字段,则:同时查找
CONCAT
、CAST
和 mysql 手册上的其他有用函数我希望这对你有帮助。
If you do not want to repeat a field result, use
GROUP BY link_id
in the end of your queryIf you want to show only
link_id
field then:Also look for
CONCAT
,CAST
and other usefull funcs on mysql manualI hope this help you.
将所有 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.