我需要在表格中显示数据库中的项目,我该如何处理?

时间:2013-10-23 13:22:26

标签: php mysql

49我需要帮助理解解决问题的方法。我有一个数据库,其中包含字段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             |
-------------------------------------------------------------------------------------------

我的问题是我需要在表中显示队列中每个成员的member_account_id。例如,queue_account_id 1105具有member_extensions 2450,2741&列出了2745,我需要在表格单元格中显示这些扩展名。我正在使用php和mysql来访问数据库。我该如何处理?

编辑:这是我需要显示的表格,除了部分记录的技术外,我所有的工作都在运行。我想我的主要问题是了解如何将查询的技术身份数据输入到现场记录的技术中。

| 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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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>';

答案 2 :(得分:1)

你在找这个吗?

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

输出:

| QUEUE_ACCOUNT_ID | MEMBER_EXTENSION |
|------------------|------------------|
|             1105 |   2750,2741,2745 |
|             1248 |             2749 |

这是 SQLFiddle 演示

答案 3 :(得分: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();
}