【问题标题】:Loop to Display Multiple Records when Associated Column Equals ____当关联列等于 ____ 时循环显示多条记录
【发布时间】:2015-10-12 12:43:06
【问题描述】:

我仍然是 PHP 和 MySQL 的新手,但愿意学习,经过三天的修修补补,现在我需要帮助。

首先,为了帮助您了解我的数据库结构,我有一个名为“o70vm_invoices_items”的 MySQL 数据库表,我正在尝试遍历每个 invoice_ID 的结果。 items 表中的每个项目都有一个 PK (o70vm_invoices_items.id),invoice_ID 列是指向另一个表 (invoices) 的链接。

例如,在项目表中,一些发票可能只有一个项目,而其他发票可能有两个或三个项目。我对如何循环显示与每张发票相关联的所有项目 (o70vm_invoices_items.name) 感到非常困难。

这是我的 MySQL 查询语句:

$queryItems = "SELECT       
    o70vm_invoices_items.id AS 'Item_ID',
    o70vm_invoices_items.invoice_id AS 'Invoice_ID_on_Items',
    o70vm_invoices_items.name AS 'Program',
    o70vm_invoices_items.value AS 'Fee',
    o70vm_invoices_items.amount AS 'Qty',
    o70vm_invoices_items.desc AS 'forWeek',

    GROUP_CONCAT(o70vm_invoices_items.desc SEPARATOR ' & ') As 'forWeekGroup',          
    ROUND(SUM((o70vm_invoices_items.value)*(o70vm_invoices_items.amount)),2) AS 'INV-TOTAL'
    FROM o70vm_invoices_items
    WHERE o70vm_invoices_items.invoice_id = $invoiceID
    GROUP BY o70vm_invoices_items.invoice_id";

如您所见,我正在从 INVOICE_ID 搜索结果。这部分效果很好。我得到这些结果很容易显示。问题是我希望在“o70vm_invoices_items.name”列中显示所选发票 ID 的每个值,并且我只能显示一个项目。

这是我尝试循环遍历结果并显示信息:

// storing the result of this MySQL query
$resultItems = mysql_query($queryItems) or die(mysql_error());

    echo "<table><tr>";

while($rowItems = mysql_fetch_array($resultItems))
    {

    echo "<td>";                
    echo "<input type='text' title='' name='name' id='name' value='". $rowItems['Program']. "' /><br>";
    echo            "</td>";
    }
    echo "</tr></table>";

同样,我只能得到一个项目的结果,而不是所有项目的结果。任何帮助,非常感谢。我想我可能需要一种不同的方式来编写一个数组。此外,如您所见,我将它显示在一个输入标签中,希望以后能够编辑内容。

请注意:一旦我弄清楚了这一点,我还需要对费用和数量列执行相同的操作。

【问题讨论】:

  • 请不要使用mysql_*函数。至于查询,不用到处放o70vm_invoices_items.

标签: php mysql arrays


【解决方案1】:

首先,我将重写 sql 使其看起来更易于阅读,为此将 alias 分配给表并在引用字段时使用它。这在您连接多个表时特别有用 - 每个 alias 必须是唯一的。在原始 sql 的情况下,因为您只从一个表中绘制数据,所以实际上不需要别名,也不需要使用完整的表名作为单个字段的前缀(即: table.field ) - 只需字段名即可已经足够了。

<?php
    $queryitems = "select 
            o.`id` as 'item_id',
            o.`invoice_id` as 'invoice_id_on_items',
            o.`name` as 'program',
            o.`value` as 'fee',
            o.`amount` as 'qty',
            o.`desc` as 'forweek',
            group_concat( o.`desc` separator ' & ' ) as 'forweekgroup',          
            round( sum( ( o.`value` ) * ( o.`amount` ) ),2 ) as 'inv-total'
            from `o70vm_invoices_items` o
            where o.`invoice_id` = '$invoiceid';";


    // storing the result of this MySQL query
    $resultItems = mysql_query( $queryItems );
    if( $resultItems ){
        echo '
            <table>
                <tr>
                    <th scope="col">ID</th>
                    <th scope="col">Program</th>
                    <th scope="col">fee</th>
                </tr>';

        $id=0; /* Each field / element that has an id must have a unique id ~ use a counter to achieve this */
        while( $row = mysql_fetch_object( $resultItems ) ){

            $id++;/* Increment the id counter */

            echo '
                <tr>
                    <td>'.$row->item_id.'</td>
                    <td>
                        <input type="text" title="'.$row->program.'" name="name" id="name'.$id.'" value="' . $row->program. '" />
                    </td>
                    <td>'.$row->fee.'</td>
                </tr>';
        }

        echo '
            </table>';
    } else {/* Do not give away too much information and degrade gracefully */
        echo 'Sorry, there was an error retrieving relevant information from the database';
    }

?>

【讨论】:

  • 看起来如果我只是在 MySQL 语句中添加一个 GROUP BY o.id,它就可以工作。 . .哇 。 . .非常感谢。
【解决方案2】:

您的查询包含这两行。它们一起保证您只会在结果集中获得一行。

WHERE    o70vm_invoices_items.invoice_id = $invoiceID
GROUP BY o70vm_invoices_items.invoice_id

您的 WHERE 行过滤您的表,因此结果集仅包含特定 invoice_id 值的行。然后,GROUP BY 在一个结果集行中汇总该特定值的信息。

从您的问题来看,您似乎每张发票有多个项目,并且您希望显示这些项目的详细信息行。在这种情况下,您将需要按项目分组,而不是 invoice_id

试试这个 GROUP BY。

 GROUP BY  o70vm_invoices_items.id

而且,您对GROUP BY 功能使用了一个非常令人困惑的非标准MySQL 扩展。如果您开始使用多张桌子,这会让您很难受。阅读它。 https://dev.mysql.com/doc/refman/5.6/en/group-by-handling.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2013-08-27
    • 2015-08-13
    • 1970-01-01
    相关资源
    最近更新 更多