选择一个表中的所有值和另一个表中的一些值,并使用php显示它们

时间:2016-09-27 08:47:52

标签: php mysql join

我试图从一个表中获取所有值,从另一个表中获取一些值,并使用php和html显示它们。这是我试图修改的演示销售点程序。

我要修改的部分是销售报告页面,最初我有TRANSACTION IDDATEINVOICE NUMBERAMOUNTREMARKS。我想从另一个表中添加一些值(即销售" NAME"和" QTY")。

这是我做的:

            <?php
                include('../connect.php');
                $d1=$_GET['d1'];
                $d2=$_GET['d2'];
                $result = $db->prepare("SELECT sales.* , sales_order.name, sales_order.qty FROM sales,sales_order WHERE date BETWEEN :a AND :b");
                $result->bindParam(':a', $d1);
                $result->bindParam(':b', $d2);
                $result->execute();
                for($i=0; $row = $result->fetch(); $i++){
            ?>
            <tr class="record">
            <td>STI-000<?php echo $row['transaction_id']; ?></td>
            <td><?php echo $row['date']; ?></td>
            <td><?php echo $row['name']; ?></td>
            <td><?php echo $row['invoice_number']; ?></td>
            <tf><?php echo $row{'invoice_number'};?></td>
            <td><?php
            $dsdsd=$row['amount'];
            echo formatMoney($dsdsd, true);
            ?></td>
            <td><?php echo $row['type']; ?></td>
            </tr>
            <?php
                }
            ?>

    </tbody>
    <thead>
        <tr>
            <th colspan="4" style="border-top:1px solid #999999"> Total </th>
            <th colspan="2" style="border-top:1px solid #999999"> 
            <?php
                function formatMoney($number, $fractional=false) {
                    if ($fractional) {
                        $number = sprintf('%.2f', $number);
                    }
                    while (true) {
                        $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
                        if ($replaced != $number) {
                            $number = $replaced;
                        } else {
                            break;
                        }
                    }
                    return $number;
                }
                $d1=$_GET['d1'];
                $d2=$_GET['d2'];
                $results = $db->prepare("SELECT sum(amount) FROM sales WHERE date BETWEEN :a AND :b");
                $results->bindParam(':a', $d1);
                $results->bindParam(':b', $d2);
                $results->execute();
                for($i=0; $rows = $results->fetch(); $i++){
                $dsdsd=$rows['sum(amount)'];
                echo formatMoney($dsdsd, true);
                }
                ?>
            </th>
        </tr>
    </thead>
</table>

1 个答案:

答案 0 :(得分:0)

假设链接表tx <stream:features></stream:features> tx <auth user="6285648145xxx" mechanism="WAUTH-2"></auth> rx <from s.whatsapp.net=""></from> rx <stream:features></stream:features> rx <challenge>376a507c7e915642d85255fe96cb1d101fa23819</challenge> tx <response>200e09fa50b7c6a3fa3416878f1e165e8d7670f054ddeaa1c77278a5f8e5b881f4f9f75dce809f42cbdcf17931db8c15641399488b90e9cb74e87de94560802f98bb028af9b5967403fb278c28e06c</response> Fatal error: Maximum execution time of 30 seconds exceeded in D:\XAMPP\htdocs\waonline\Chat-API-master\src\whatsprot.class.php on line 2609sales的键是字段sales_order,您必须使用INNER JOIN(或LEFT JOIN,取决于您的需要)将两个表连接为以下:

invoice_number

如果链接两个表的键是其他的,那么只需更新$result = $db->prepare("SELECT sales.* , sales_order.name, sales_order.qty FROM sales INNER JOIN sales_order ON sales.invoice_number = sales_order.invoice_number WHERE sales.date BETWEEN :a AND :b"); 以匹配好的列名称

另外,不要忘记在连接表时在每个字段之前添加表的名称,因为如果在多个表中具有相同的字段名称,它将给出错误,因为mysql不知道在哪个表中它必须从中获取数据。