【发布时间】:2011-05-17 07:56:15
【问题描述】:
我有这样的表格:
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
我该怎么做?
添加: 对不起,我的英语不好。我有带有重复字段 counter_set_id 的 device、device_model 和 device_type 表。我想从 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;
【问题讨论】:
-
对于 link_id = 4,name1 肯定为空吗?它没有对应的值。