我定義「連接」一張桌子和另一個定義「設備」加入兩列,一列
我有一個「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
你應該手動指定字段,然後你就可以設置適當的表和別名 –