SQL - 从两个不同的表中选择行并回显它

时间:2018-02-11 21:51:59

标签: php html mysql sql-server database

我想从两个不同的表中选择不同的行。但是我希望能够在一个回声中表现出来。 '现在我的问题是我无法找到解决问题的好方法。

第一张表:

TABLE "bands"

band_id
band_name

第二张表:

TABLE "product"

product_id
product_name
product_image
amount
buy
sell

我需要展示的是品牌名称作为表格标题,product_name和product_image以及金额在这样的表格中买卖

Resault:

________________band_name___________________
product_image|product_name|amount|buy |sell 
-------------|------------|------|----|-----
image        |SAMSUNG     |50    |200€|220€
image        |IPHONE      |20    |300€|400€

这是我的PHP代码

enter code here
<?php
    require_once 'connections/dbc.php';
    $query = "select 'brands.brand_name', 'product.product_name', 'product.amount', 'product.buy', 'product.sell'";
    $response = @mysqli_query($conn, $query);
    if (!$response) {
        $_SESSION['message'] = "Couldn't issue database query<br />";
        header("location: error.php");
        exit();
    } else {
        while($row = mysqli_fetch_array($response)){
            echo '
                <div id="band">

                    <br>
                    <p style="font-size: 100%; text-align:center;">'. $row['brand']. '</p>'.'<br>

                <table>
                    <tr>
                    <th>Name</th>
                    <th>amount</th>
                    <th>Preis/buy</th>
                    <th>Preis/sell</th>
                    </tr>
                    <tr>
                    <td>'.$row['product_name'].'</td>
                    <td>'.$row['amount'].'</td>
                    <td>'.$row['buy'].'€'.'</td>
                    <td>'.$row['sell'].'€'.'</td>
                    </tr>
               </table>

        </div>
        }
    }

    ?>

1 个答案:

答案 0 :(得分:0)

使用视图SQL:

(CREATE VIEW view1 AS (
    SELECT yourRow FROM table1
    UNION
    SELECT yourRow FROM table2
));

然后使用echo from view1