[英]PHP / MySQL Grouped Query, how to group second query with first?
Having trouble figuring out how to group a selection in a while loop after a second query (within while loop). 在第二次查询后(在while循环内)无法弄清楚如何在while循环中将选择分组。 All info below.
下面的所有信息。
My Table 我的桌子
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)
)
Sample Data 样本数据
(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'),
I have this query 我有这个查询
$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";
And in my while loop, I then perform another query on data to correlate the NPA/NXX to a rate center/city. 然后在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";
This groups counts to a particular NPA/NXX 该组计入特定的NPA / NXX
This gives me an output like this. 这给了我这样的输出。
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
What I need to do now, is group all similar cities/counts into a single container like so: 我现在需要做的是将所有类似的城市/城市归为一个容器,如下所示:
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
I'm at a loss for how I would even go about accomplishing this due to table structures. 由于表结构,我什至不知道如何完成此操作。 The second query for state/city/rate center is from a LERG database (telephony data).
对状态/城市/费率中心的第二个查询来自LERG数据库(电话数据)。 I'm guessing I'd need to do this within PHP.
我猜想我需要在PHP中执行此操作。
---EDIT--- - -编辑 - -
I've made some progress. 我已经取得了一些进展。 I changed my query to this:
我将查询更改为此:
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
This is almost what I want: 这几乎是我想要的:
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
Trying to get the other NXX's to display under my group. 试图让其他NXX显示在我的组下。 For reference, with original query, there was 7 BIGBEAR CY rows, so this is grouping properly now with the exception of listing the other NPA NXXs
作为参考,对于原始查询,有7个BIGBEAR CY行,因此,除了列出其他NPA NXX之外,此行现在已正确分组
Join all 4 of your tables to get the per-NPA/NXX information. 联接您的所有四个表以获取每个NPA / NXX的信息。 Then join this with a subquery that gets the totals per rate-center.
然后将其与子查询结合,以获取每个费率中心的总数。
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
When you're displaying the results, save the previous row's rcabbr
and l_state
in variables. 显示结果时,将上一行的
rcabbr
和l_state
在变量中。 If the current row is the same, leave all those columns blank. 如果当前行相同,则将所有这些列留空。
I figured out a way to resolve my issue. 我想出了一种解决问题的方法。 It was more of a PHP fix than MySQL, but figured I would share.
它比MySQL更像是一个PHP修复程序,但我想我会分享。
agc05olvemo ASHLAND OREGON 541201 55
541552
541708
541482
541488
I had to move the OOS count to the end in order to achieve this. 为了实现这一点,我不得不将OOS计数移到最后。 I'm still confused about this, but managed to make it work.
我对此仍然感到困惑,但是设法使它起作用。
Here is the query in use: 这是正在使用的查询:
$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";
Here is my crappy PHP to achieve the desired result. 这是我cr脚的PHP,可以达到预期的效果。 (don't criticize my aweful code, I'm not a developer by nature. :))
(不要批评我的代码,我不是天生的开发人员。:)
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.