简体   繁体   English

仅选择1个不同的列-SQL

[英]Select only 1 distinct column - SQL

I have a table like this in my database 我的数据库中有一个这样的表

orderID| productID | qty
1        5           1
1        2           1
1        6           1
1        4           1
2        9           1
2        5           2
2        6           1

I want to output it into my HTML/php page in a similar way to this 我想以与此类似的方式将其输出到我的HTML / php页面中

Order ID:1
Product ID | Qty
5          | 1
2          | 1
6          | 1
4          | 1
Order ID:2
Product ID | Qty
9          | 1
5          | 2
6          | 1

Is this even possible to do? 这有可能吗? I have tried to use DISTINCT but that only outputs 1 row for each orderID. 我尝试使用DISTINCT,但是每个orderID仅输出1行。 Here is the closest way I have outputted using the ORDER BY statement at the moment but this makes the headers for every row from the database. 这是我目前使用ORDER BY语句输出的最接近的方法,但这使数据库中的每一行成为标头。

<?php
    if ($query = "SELECT * FROM productOrders ORDER BY orderID"){
        $result = mysqli_query($connection, $query);

        if (mysqli_num_rows($result) > 0)
    {
         while($row = mysqli_fetch_assoc($result)) 
         {
        ?>                                          

    <table>
    <tr>
        <th>Order ID: <?php echo $row['orderID'] ?></th>
    </tr>
    <tr>
        <th>Product ID</th>
        <th>Qty</th>
    </tr>
    <tr>
        <td><?php echo $row['productID'] ?></td>
        <td><?php echo $row['qty'] ?></td>
    </tr>

    </table>
<?php
        }
    } 
 ?>

Try this 尝试这个

            <?php
    $query = "SELECT * FROM productOrders ORDER BY orderID";
                if ($query){
                    $result = mysqli_query($connection, $query);

                    if (mysqli_num_rows($result) > 0)
                {
                     while($row = mysqli_fetch_assoc($result)) 
                     {
                          $id=$row['orderID'];
                          $sql="SELECT orderID, qty FROM productOrders WHERE orderID=$id";
                          $q=mysqli_query($connection,$sql);
                          if(!$q){ echo "error"; }
                           else { while($row1=mysqli_fetch_assoc($q)){

            ?>

                <table>
                <tr>
                    <th>Order ID <?php echo $id; ?></th>
                </tr>
                <tr>
                    <th>Product ID</th>
                    <th>Qty</th>
                </tr>
                <tr>
                    <td><?php echo $row1['productID'] ?></td>
                    <td><?php echo $row1['qty'] ?></td>
                </tr>

                </table>


            <?

                       }
          }
                    }
                } 
}
             ?>
select orderid, productid, sum(qty)
from productOrders
group by orderid, productid

which will give you layout of... 这将为您提供...的布局

OrderID | ProductID | Qty
1       | 5         | 1
1       | 2         | 1
1       | 6         | 1
1       | 4         | 1
2       | 9         | 1
2       | 5         | 2
2       | 6         | 1

Then just loop through each row and display as needed in PHP 然后只需遍历每一行并根据需要显示PHP

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

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