我有一个定义“连接”的表和另一个定义“设备”的表
我有一个“ source_system_name”和'destination_system_name'
连接表中的列。
并且"system_name"
与"equipment"
表中的这些匹配。
设备可以是各种类型:
1. Core
2. Aggregation
3. Customer.
我正在尝试定义一个查询,以返回设备表中源和目标字段的“类型”。
连接表如下:
id | Source Name | Source port | Destination Name | Destination Port | etc...
-----------------------------------------------------------------------------
4 Device_core1 1/1 Device_agg3 3/4
设备表如下:
id | Equip_name | Equip type | etc....
------------------------------------------
3 Device_core1 | Core
7 Device_agg3 | Aggregation
我试图使结果返回像:
id | Source Name |Source port|Source type|Destination Name|Destination Port|Destination type|
---------------------------------------------------------------------------------------------
1 Device_core1 1/1 Core Device_agg3 3/4 Aggregation
我要使用的查询是:
SELECT DISTINCT * FROM connections
LEFT JOIN equipment on connections.system_name_source=equipment.system_name
OR connections.system_name_dest=equipment.system_name;
当我这样做时,我得到了重复记录,其中,Source type
字段被覆盖,Destination type
反之亦然。是否可以使用查询查询equipment
表中的源数据和目标数据?即
id | Source Name |Source port|Source type|Destination Name|Destination Port|Destination type|
1 Device_core1 1/1 Aggregation Device_agg3 3/4 Aggregation
2 Device_core1 1/1 Core Device_agg3 3/4 Core
您将需要进行自我加入。尝试以下查询:
SELECT
connections.id,
connections.Source_Name,
connections.Source_port,
src.systype SourceType,
connections.Destination_Name,
connections.Destination_Port,
tgt.systype DestinationType
FROM connections
LEFT JOIN equipment src on connections.system_name_source=src.system_name
LEFT JOIN equipment tgt on connections.system_name_dest=tgt.system_name
在上面的查询中,我已经使用了别名equipment
表两倍src
和tgt
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句