-1

我在下面有两个结果集。

SELECT * FROM `mfw_host`
 WHERE host_id IN
(SELECT host_id FROM `latest_tech` 
WHERE `nav_node_id` = 213)



SELECT * FROM `mfw_navnode`
 WHERE host_id  IN
(SELECT host_id FROM mfw_host
WHERE host_id IN (SELECT host_id FROM `latest_tech` 
WHERE `nav_node_id` = 213))

我是将两个结果连接在一起......没有加入和联合(因为它们没有连接它们的键......并且每组结果的字段都不同)。

我怎样才能做到这一点?

更新:.. 答案更新见上文..

我想将上面两组的结果加在一起..它们每组返回 5 行,并且每组检索到不同的列号

尝试了一种解决方案:

SELECT
    a.*
  , b.*
FROM
  ( SELECT * FROM `mfw_host`
 WHERE host_id IN
(SELECT host_id FROM `latest_tech` 
WHERE `nav_node_id` = 213)
  ) AS a
  CROSS JOIN
  ( SELECT * FROM `mfw_navnode`
 WHERE host_id  IN
(SELECT host_id FROM mfw_host
WHERE host_id IN (SELECT host_id FROM `latest_tech` 
WHERE `nav_node_id` = 213))
  ) AS b

此交叉连接返回太多行

4

1 回答 1

2

您可以为其他查询中不存在的字段获取一些默认值。让我们假设在 mfw_host 你有字段:id、主机名、ip;在 mfw_navnode 你有 id、name、create_date。那么您的查询可能是

SELECT 0 AS type, id, host_name, ip, '' AS node_name, 
    NULL AS create_date FROM mfw_host
WHERE host_id IN (
    SELECT host_id FROM `latest_tech` WHERE `nav_node_id` = 213)
UNION
SELECT 1 AS type, id, '' AS host_name, NULL AS ip, node_name, create_date 
    FROM `mfw_navnode` WHERE `parent_id`=213

类型是为了让你知道从哪个表是哪一行。但这不是必需的。

于 2012-05-17T07:06:27.230 回答