繁体   English   中英

PHP / MySQL分组查询,如何将第二个查询与第一个分组?

[英]PHP / MySQL Grouped Query, how to group second query with first?

在第二次查询后(在while循环内)无法弄清楚如何在while循环中将选择分组。 下面的所有信息。

我的桌子

CREATE TABLE MTAs (
id int(10) NOT NULL AUTO_INCREMENT,
agcid int(3) NOT NULL,
ad int(1) NOT NULL,
fqdn varchar(50) NOT NULL,
`as` varchar(5) NOT NULL,
ps varchar(5) NOT NULL,
dn varchar(10) NOT NULL,
npa varchar(3) NOT NULL,
nxx varchar(3) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
)

样本数据

(1590305265, 2, 1, 'dlnirwtiv.xx.some.domain', 'ACT', 'UNA', '719587xxxx', '719', '587', '2018-03-22 17:15:01'),
(1590305266, 2, 1, 'kwror4203.xx.some.domain', 'ACT', 'UNA', '406290xxxx', '406', '290', '2018-03-22 17:15:01'),
(1590305267, 2, 1, 'o0ontf472.xx.some.domain', 'ACT', 'UNA', '406556xxxx', '406', '556', '2018-03-22 17:15:01'),
(1590305268, 2, 1, 'twron2507.xx.some.domain', 'ACT', 'UNA', '719992xxxx', '719', '992', '2018-03-22 17:15:01'),
(1590305269, 2, 1, 'qo3pkk149.xx.some.domain', 'ACT', 'UNA', '406299xxxx', '406', '299', '2018-03-22 17:15:01'),
(1590305270, 2, 1, 'wqqblbmij.xx.some.domain', 'ACT', 'UNA', '406309xxxx', '406', '309', '2018-03-22 17:15:01'),
(1590305271, 2, 1, 'c0woqk554.xx.some.domain', 'ACT', 'UNA', '719691xxxx', '719', '691', '2018-03-22 17:15:01'),
(1590305272, 2, 1, 'hsmt5b940.xx.some.domain', 'ACT', 'UNA', '970639xxxx', '970', '639', '2018-03-22 17:15:01'),
(1590305273, 2, 1, 'yfbl3rl8e.xx.some.domain', 'ACT', 'UNA', '406656xxxx', '406', '656', '2018-03-22 17:15:01'),
(1590305274, 2, 1, 'w5mkoc117.xx.some.domain', 'ACT', 'UNA', '406361xxxx', '406', '361', '2018-03-22 17:15:01'),
(1590305275, 2, 1, 'ulp5frrgj.xx.some.domain', 'ACT', 'UNA', '406563xxxx', '406', '563', '2018-03-22 17:15:01'),

我有这个查询

$sql = "SELECT AGCs.name AS agcname, npa, nxx, COUNT( npa ) AS ooscount
FROM MTAs
INNER JOIN AGCs ON AGCs.id = MTAs.agcid
WHERE agcid =  '".$_GET['agcid']."'
GROUP BY npa, nxx
ORDER BY  `ooscount` DESC";

然后在while循环中,我对数据执行另一个查询,以将NPA / NXX与费率中心/城市相关联。

SELECT rcabbr, s.l_state
FROM endoff
INNER JOIN state s ON s.s_state = state
WHERE npa =  '".$npa."'
AND nxx =  '".$nxx."' LIMIT 1";

该组计入特定的NPA / NXX

这给了我这样的输出。

AGC OOS Count   Location    Rate Center NPA NXX
agc04   246 WAUSAU  WISCONSIN   715 298
agc04   170 MERRILL WISCONSIN   715 536
agc04   119 WAUSAU  WISCONSIN   715 842
agc04   116 WAUSAU  WISCONSIN   715 675
agc04   103 WAUSAU  WISCONSIN   715 845
agc04   85  WAUSAU  WISCONSIN   715 848

我现在需要做的是将所有类似的城市/城市归为一个容器,如下所示:

AGC OOS Count   Location    Rate Center NPA NXX
agc04   672 WAUSAU  WISCONSIN   715 298
                                715 842
                                715 675
                                715 845
                                715 848
agc04   170 MERRILL WISCONSIN   715 536

由于表结构,我什至不知道如何完成此操作。 对状态/城市/费率中心的第二个查询来自LERG数据库(电话数据)。 我猜想我需要在PHP中执行此操作。

- -编辑 - -

我已经取得了一些进展。 我将查询更改为此:

SELECT a.name AS agcname, m.npa, m.nxx, COUNT( m.npa ) AS ooscount, 
e.rcabbr, s.l_state FROM k.MTAs m
INNER JOIN k.AGCs a ON a.id = m.agcid
INNER JOIN endoffice.endoff e on e.npa = m.npa and e.nxx = m.nxx
INNER JOIN endoffice.state s ON s.s_state = e.state       
      WHERE agcid =  '3'
      GROUP BY e.rcabbr,m.npa
      ORDER BY  ooscount DESC

这几乎是我想要的:

agc03   909 585 1047    BIGBEAR CY  CALIFORNIA
agc03   775 686 892     RENO        NEVADA
agc03   909 878 359     BIGBEAR LK  CALIFORNIA
agc03   775 298 252     CRYSTALBAY  NEVADA
agc03   909 527 195     UPLAND      CALIFORNIA

试图让其他NXX显示在我的组下。 作为参考,对于原始查询,有7个BIGBEAR CY行,因此,除了列出其他NPA NXX之外,此行现在已正确分组

联接您的所有四个表以获取每个NPA / NXX的信息。 然后将其与子查询结合,以获取每个费率中心的总数。

SELECT a.name, x.total_ooscount, e.rcabbr, s.l_state, m.npa, m.nxx
FROM MTAs AS m
JOIN AGCs AS a ON a.id = m.agcid
JOIN endoff AS e ON e.npa = m.npa AND e.nxx = m.nxx
JOIN state AS s ON s.s_state = e.state
JOIN (
    SELECT e.rcabbr, s.l_state, COUNT(*) AS total_ooscount
    FROM MTAs AS m
    JOIN endoff AS e ON e.npa = m.npa AND e.nxx = m.nxx
    JOIN state AS s ON s.s_state = e.state
    WHERE m.agcid = {$_GET['agcid']}
    GROUP BY e.rcabbr, s.l_state
) AS x ON x.rcabbr = e.rcabbr AND x.l_state = s.l_state
WHERE m.agcid = {$_GET['agcid']}
ORDER BY s.l_state, e.rcabbr

显示结果时,将上一行的rcabbrl_state在变量中。 如果当前行相同,则将所有这些列留空。

我想出了一种解决问题的方法。 它比MySQL更像是一个PHP修复程序,但我想我会分享。

agc05olvemo ASHLAND OREGON  541201  55
                            541552
                            541708
                            541482
                            541488

为了实现这一点,我不得不将OOS计数移到最后。 我对此仍然感到困惑,但是设法使它起作用。

这是正在使用的查询:

$sql = "SELECT a.name AS agcname, m.npa, m.nxx, COUNT( m.npa ) AS ooscount,e.rcabbr,s.l_state
      FROM MTAs m
      INNER JOIN AGCs a ON a.id = m.agcid
      LEFT JOIN endoffice.endoff e ON e.npa = m.npa
      INNER JOIN endoffice.state s ON s.s_state = e.state
      AND e.nxx = m.nxx
      WHERE agcid =  '".$agcid."'
      GROUP BY npa, nxx
      ORDER BY rcabbr ASC";

这是我cr脚的PHP,可以达到预期的效果。 (不要批评我的代码,我不是天生的开发人员。:)

while($row = $result->fetch_assoc()) {
  $agcrow = "";
  $agcname = $row['agcname'];
  $ooscount = $row['ooscount'];
  $npa = $row['npa'];
  $nxx = $row['nxx'];
  $rcabbr = $row['rcabbr'];
  $l_state = $row['l_state'];
  $npanxx = "".$npa."".$nxx."";

  if (isset($rc) && $rc != $rcabbr) {
      $agcrow .= "                       </div><div class=\"divTableCell\">".$rcoos."</div>\n                     </div>\n";
      unset($rcoos);
      unset($rc);
      unset($st);

  }    

  if (!isset($rc)) {
      $rc = $rcabbr;
      $st = $l_state;
      $agcrow .= "\n                     <div class=\"divTableRow\">     
                   <div class=\"divTableCell\">" . $agcname. "</div>      
                   <div class=\"divTableCell\">".$rc."</div>      
                   <div class=\"divTableCell\">".$l_state."</div>
                   <div class=\"divTableCell\"><A href=\"".$_SERVER['PHP_SELF']."?agcid=".$agcid."&npa=".$npa."&nxx=".$nxx."\">".$npanxx."</a><BR>\n";
  } else {
      $agcrow .= "                                                 <A href=\"".$_SERVER['PHP_SELF']."?agcid=".$agcid."&npa=".$npa."&nxx=".$nxx."\">".$npanxx."</a><BR>\n";   
  }
  $rcoos = $rcoos + $ooscount;

  echo $agcrow;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM