简体   繁体   English

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

[英]select all the values from one table and some values from another table and display them with php

I am trying to get all the values from one table and some values from another table and display them using php and html. 我试图从一个表中获取所有值,并从另一表中获取一些值,并使用php和html显示它们。 It is a demo point of sale program that I am trying to modify. 这是我要修改的演示销售点程序。

The section I am trying to modify is the sales report page, where initially I have TRANSACTION ID , DATE , INVOICE NUMBER , AMOUNT and REMARKS . 我要修改的部分是销售报告页面,最初我具有TRANSACTION IDDATEINVOICE NUMBERAMOUNTREMARKS I would like to add some values from another table (ie sales "NAME" and "QTY"). 我想从另一个表中添加一些值(即销售“ NAME”和“ QTY”)。

Here is what I did: 这是我所做的:

            <?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>

Assuming the key linking the tables sales and sales_order are the field invoice_number you have to join the two tables using an INNER JOIN (or a LEFT JOIN depending on your needs) as following : 假设链接表salessales_order的键是字段invoice_number您必须使用INNER JOIN(或根据您的需要选择LEFT JOIN)将两个表连接起来,如下所示:

$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");

If the key linking the two tables is something else, then just update the ON sales.invoice_number = sales_order.invoice_number to match the good column name 如果链接两个表的键是其他键,则只需更新ON sales.invoice_number = sales_order.invoice_number以匹配良好的列名

Also, do not forget to add the name of the table before each fields when you join tables because if you have the same field name in more than one table it will give you an error because mysql won't know in which table it has to get the data from. 另外,在连接表时,请不要忘记在每个字段之前添加表名,因为如果在多个表中具有相同的字段名,则会给您一个错误,因为mysql不会知道必须在哪个表中从中获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM