0

49I need help understanding the approach to take with a problem. I have a data base that contains the fields id, LastUpdate, member_name, member_extension (phone ext), member_account_id, queue_account_id.

| id | LastUpdate  | member_name | member_extension | member_account_id | queue_account_id |
-------------------------------------------------------------------------------------------
| 1  | 2013-10-15  | John Smith  | 2750             | 1195              | 1105             |
| 2  | 2013-10-15  | Bill Jones  | 2749             | 1172              | 1248             |
| 3  | 2013-10-15  | Bill Jones  | 2749             | 1172              | 1105             |
| 4  | 2013-10-15  | Fred Black  | 2745             | 1195              | 1105             |
-------------------------------------------------------------------------------------------

My problem is I need to show in a table the member_account_id's of each member in a queue. For instance queue_account_id 1105 has member_extensions 2450, 2741 & 2745 listed, I need to show those extensions in a table cell. I'm using php and mysql to access database. How do I approach this?

EDIT: Here is the table I need to display, I have all of it working except the techs logged in part. i guess my main problem is understanding how to get the queried tech identity data into the techs logged in field.

| Queue    | Calls | % total calls  | answered by us| % answered by us| abandoned | % abandoned | Redirected | % Redirected | Techs Logged In |   
----------------------------------------------------------------------------------------------------------------------------------------------|
| Premium  | 1     | 9%             | 0             | 0%              | 1         | 100%        | 0          | 0%           |                 |
| Standard | 2     | 0%             | 1             | 150%            | 0         | 0%          | 1          | 50%          |                 | 
| Queue 2  | 1     | 0%             | 1             | 100%            | 0         | 0%          | 0          | 0%           |                 | 
| Queue 3  | 7     | 64%            | 4             | 57%             | 3         | 43%         | 1          | 0%           |                 |                  
| Totals   | 11    | 100%           | 6             | 55%             | 4         | 36%         | 1          | 9%           |                 |                  
----------------------------------------------------------------------------------------------------------------------------------------------|
4

4 回答 4

2

目前尚不清楚这是一个 php 还是 mysql 问题。

您可以使用 mysql 查询收集项目,如下所示。

 SELECT member_account_id, member_name, 
        GROUP_CONCAT(queue_account_id 
                        ORDER BY group_account_id 
                        SEPARATOR ', ') AS ids
   FROM yourtable
  GROUP by member_account_id, member_name
于 2013-10-23T13:28:38.477 回答
1

你在找这个吗?

SELECT queue_account_id, 
       GROUP_CONCAT(member_extension) member_extension
  FROM table1
 GROUP BY queue_account_id

输出:

| QUEUE_ACCOUNT_ID | 会员扩展 |
|------------------|------------------|
| 1105 | 2750,2741,2745 |
| 第1248章 第2749章

这是SQLFiddle演示

于 2013-10-23T13:32:50.247 回答
1
SELECT
t1.queue_account_id, GROUP_CONCAT(DISTINCT t2.member_extension) member_extensions
FROM tblname t1
INNER JOIN tblname  t2 USING (queue_account_id)
GROUP BY t1.queue_account_id

这是您在场景中呈现它的方式:

<?php

$link = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('dbname');

$sql = 'SELECT
    t1.queue_account_id, GROUP_CONCAT(DISTINCT t2.member_extension) member_extensions
    FROM tblname t1
    INNER JOIN tblname t2 USING (queue_account_id)
    GROUP BY t1.queue_account_id';
$query = mysql_query($sql) or die(mysql_error());

echo '<table border="1">';

while ($rs = mysql_fetch_object($query)) {
echo '<tr>';
echo '<td>' . $rs->queue_account_id . '</td>';
echo '<td>';
echo '<ul>';
foreach (explode(',', $rs->member_extensions) as $extension) {
    echo '<li>' . $extension . '</li>';
}
echo '</ul>';
echo '</td>';
echo '</tr>';
}

echo '</table>';
于 2013-10-23T13:31:43.020 回答
0

解决方案:

$sth = $conn->prepare("SELECT queue_account_id, GROUP_CONCAT( member_extension ) member_extension
        FROM currentTechs
        GROUP BY queue_account_id"); 
$sth->execute();
$sql = $sth->fetchAll(PDO::FETCH_ASSOC);

echo '<table border="1">';

try {
    //$stmt = $conn->query($sql);
    //$result = $sql->setFetchMode(PDO::FETCH_NUM);

    foreach ($sql as $rs) {
        echo '<tr>';
        echo '<td>' . $rs['queue_account_id'] . '</td>';
        echo '<td>';
            foreach (explode(',', $rs['member_extension']) as $extension) {
                echo $extension . ", ";
            }
        echo '</td>';
        echo '</tr>';
        }

    echo '</table>';
    }
catch (PDOException $e) {
print $e->getMessage();
}
于 2013-10-23T16:57:33.887 回答