多次加入" IN"条款

时间:2015-08-17 16:00:10

标签: php mysql

您好我正在尝试查询上游中继与下游客户设备之间的连接。

  

上游中继端口11/1 12/1 ---聚合器设备---客户端口1/1 1/2

                                  ---Customer ports 2/1 2/2

                                  ---Customer ports 3/1 3/2

                                  --etc

我想搜索客户设备并让它返回客户连接的端口列表。 客户需要连接到上游中继端口以及每个其他客户端口。 (网络的东西两侧) 输出表应该类似于:

  

EAST链接

     

Customer1 port 1/1 - > trunk 11/1

     

Customer1 port1 / 1 - > Customer2端口2/1

     

Customer1 port1 / 1 - > Customer3端口3/1

     

     

WEST LINKS

     

Customer1端口1/2 - >主干12/1

     

Customer1 port1 / 2 - > Customer2端口2/2

     

Customer1 port1 / 2 - >客户3端口3/2

     

我的连接表在源端具有上游端口,在目标端具有客户端口。 聚合器连接到核心设备,源端侧有核心连接,dest侧有AGG连接。

我可以查询每一位给我想要的东西,但无法找出将它们连接在一起的最佳方法。我是否应该尝试将它们加入到单个查询中? 有没有更好的办法?我还包括了示例数据sql。

我的疑问是:

//get trunk ports   
$q1 = "select system_name_dest, slot_dest, port_dest, side FROM     connections where system_name_dest IN (select distinct system_name_source     FROM connections where system_name_dest = '01-42C3:N:QLD:7NET:TOWNSVILLE')";
  

输出:

     

system_name_dest slot_dest port_dest side

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 12 01 E

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 24 01 W

//get all customer ports
$q2 = "select system_name_source, slot_source, port_source, side FROM connections where system_name_source IN (select distinct system_name_source  
  FROM connections where system_name_dest ='01-42C3:N:QLD:7NET:TOWNSVILLE')";
  

输出:

     

system_name_source slot_source port_source side

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 13 01 E

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 14 01 W

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 01 01 E

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 02 01 W

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 01 02 E

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 02 02 W

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 06 01 E

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 07 01 W

 //get A-side ports
$q3 = "select system_name_source, slot_source, port_source, side  
 FROM connections where system_name_dest='01-42C3:N:QLD:7NET:TOWNSVILLE'";
  

输出

     

system_name_source slot_source port_source side

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 01 01 E

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 02 01 W

我想我的决赛桌是这样的: 我有一个东部的源端口和相应的连接列表,还有一个带有相应连接的West列表。

  

源插槽端口目标插槽端口

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 01 01< - > 02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 12 01 E

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 01 01< - > 02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 13 01 E

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 01 01< - > 02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 01 02 E

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 01 01< - > 02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 06 01 E

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 02 01< - > 02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 24 01 W

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 02 01< - > 02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 14 01 W

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 02 01< - > 02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 02 02 W

     

02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 02 01< - > 02-46C1:P:QLD:TLS:TOWNSVIL_AGGR 07 01 W

如果有帮助,还包括数据库表的子集转储。

-- phpMyAdmin SQL Dump
-- version 4.2.8
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Aug 18, 2015 at 01:50 AM
-- Server version: 5.6.25-log
-- PHP Version: 5.6.10

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `dvn2`
--

-- --------------------------------------------------------

--
-- Table structure for table `connections`
--

CREATE TABLE IF NOT EXISTS `connections` (
`id` smallint(5) NOT NULL,
  `system_name_source` char(32) COLLATE utf8_unicode_ci NOT NULL,
  `port_type` char(15) COLLATE utf8_unicode_ci NOT NULL,
  `slot_source` char(2) COLLATE utf8_unicode_ci NOT NULL,
  `port_source` char(2) COLLATE utf8_unicode_ci NOT NULL,
  `system_name_dest` char(32) COLLATE utf8_unicode_ci NOT NULL,
  `slot_dest` char(2) COLLATE utf8_unicode_ci NOT NULL,
  `port_dest` char(2) COLLATE utf8_unicode_ci NOT NULL,
  `cable` char(17) COLLATE utf8_unicode_ci NOT NULL,
  `side_name` char(32) COLLATE utf8_unicode_ci NOT NULL,
  `side` char(1) COLLATE utf8_unicode_ci NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=1007 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `connections`
--

INSERT INTO `connections` (`id`, `system_name_source`, `port_type`, `slot_source`, `port_source`, `system_name_dest`, `slot_dest`, `port_dest`, `cable`, `side_name`, `side`, `status`) VALUES
(15, '01-08C3:P:QLD:TLS:CHARLTTE_AGGR', '10G Trunk', '16', '01', '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', '12', '01', 'D 08C3 46C1 DD001', '01-47C5:P:QLD:TLS:WGABBA_AGGR', 'E', 1),
(202, '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', '10G Trunk', '13', '01', '01-11C4:P:QLD:TEL:WILLOWS_STDM', '11', '01', 'D 11C4 46C1 DD001', '', 'E', 1),
(203, '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', '10G Trunk', '14', '01', '01-11C4:P:QLD:TEL:WILLOWS_STDM', '12', '01', 'D 11C4 46C1 DD002', '', 'W', 1),
(204, '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', '1G Trunk', '01', '01', '01-42C3:N:QLD:7NET:TOWNSVILLE', '05', '01', 'D 42C3 46C1 DD001', '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', 'E', 1),
(205, '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', '1G Trunk', '02', '01', '01-42C3:N:QLD:7NET:TOWNSVILLE', '05', '02', 'D 42C3 46C1 DD002', '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', 'W', 1),
(218, '01-47C5:P:QLD:TLS:WGABBA_AGGR', '10G Trunk', '16', '01', '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', '24', '01', 'D 46C1 47C5 DD001', '01-08C3:P:QLD:TLS:CHARLTTE_AGGR', 'W', 1),
(626, '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', '1G Trunk', '01', '02', '01-13C4:P:QLD:SCA:TOWNSVILLE', '11', '01', 'D 13C4 46C1 DD001', '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', 'E', 0),
(627, '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', '1G Trunk', '02', '02', '01-13C4:P:QLD:SCA:TOWNSVILLE', '12', '01', 'D 13C4 46C1 DD002', '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', 'W', 0),
(825, '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', '1G Trunk', '06', '01', '01-24C5:P:QLD:BAUS:MT_STUART', '11', '01', 'D 24C5 46C1 DD001', '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', 'E', 0),
(826, '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', '1G Trunk', '07', '01', '01-24C5:P:QLD:BAUS:MT_STUART', '12', '01', 'D 24C5 46C1 DD002', '02-46C1:P:QLD:TLS:TOWNSVIL_AGGR', 'W', 0);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `connections`
--
ALTER TABLE `connections`
 ADD PRIMARY KEY (`id`), ADD KEY `system_name_source` (`system_name_source`), ADD KEY `system_name_dest` (`system_name_dest`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `connections`
--
ALTER TABLE `connections`
MODIFY `id` smallint(5) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=1007;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

亲切的问候  佰

1 个答案:

答案 0 :(得分:1)

这给出了与最终牌桌相同的一组结果 - 但不是以相同的顺序,如果重要的话,你可能需要使用它:

select c1.system_name_source, c1.slot_source, c1.port_source, '<-->', c2.system_name_source, c2.slot_dest, c2.port_dest, c1.side
FROM connections c1
JOIN connections c2 ON (c1.system_name_source=c2.system_name_dest) AND c1.side=c2.side AND c1.id!=c2.id
where c1.system_name_dest='01-42C3:N:QLD:7NET:TOWNSVILLE'
UNION
select c1.system_name_source, c1.slot_source, c1.port_source, '<-->', c2.system_name_source, c2.slot_source, c2.port_source, c1.side
FROM connections c1
JOIN connections c2 ON (c1.system_name_source=c2.system_name_source) AND c1.side=c2.side AND c1.id!=c2.id
where c1.system_name_dest='01-42C3:N:QLD:7NET:TOWNSVILLE'
ORDER BY side