删除列中的重复数据 [英] Remove repeating data in a column

查看:86
本文介绍了删除列中的重复数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个订购系统,客户可以在其中订购许多物品.我还有一个管理员,他/她可以在当天看到所有订单.管理员可以查看客户名称,应付总额,客户订购的产品和产品数量.

I am currently developing an ordering system where a customer can order many items. I also have an admin where he/she can see all the orders on that day. The Admin can view the name of the customer, the total payable, the products and the quantity of the product the customer have ordered.

我目前正在通过查询看到此结果.

I am currently seeing this results using my query.

 Name   | Payable | Product   | Quantity
 Test   |   165   | keychain  | 3
 Test   |   165   | Tumbler   | 1
 Miguel |   525   | Keychain  | 3
 Miguel |   525   | Magic Mug | 3
 Dandel |   1010  | keychain  | 3
 Dandel |   1010  | T-shirt   | 2
 Dandel |   1010  | Keychain  | 3
 Dandel |   1010  | Mug       | 5

这是我的查询.

 $result = mysql_query("
        SELECT reservation.firstname, reservation.lastname, reservation.payable, reservation.city, orders.product, orders.qty, reservation.date
        FROM orders
        INNER JOIN reservation
        ON orders.confirmation = reservation.confirmation 
        WHERE reservation.date = CURDATE() && reservation.city = '24th Floor'
        ");
    while($row = mysql_fetch_array($result))
        {
            echo '<tr>';
            echo '<td style="border: 1px solid black;">'.$row['firstname'].'</td>';
            echo '<td>'.$row['payable'].'</td>';
            echo '<td>'.$row['product'].'</td>';
            echo '<td>'.$row['qty'].'</td>';
            echo '</tr>';
        } 

我想得到这样的结果.我该怎么办?

I want to get results like this. How can I do it?

 Name   | Payable | Product   | Quantity
 Test   |   165   | keychain  | 3
        |         | Tumbler   | 1
 Miguel |   525   | Keychain  | 3
        |         | Magic Mug | 3
 Dandel |   1010  | keychain  | 3
        |         | T-shirt   | 2
        |         | Keychain  | 3
        |         | Mug       | 5

推荐答案

您可以在迭代时维护某些状态,以跟踪当前行是否为新名称/应付款:

You can maintain some state while iterating which keeps track of whether the current row is a new name/payable:

$last_name_seen = NULL;

while ($row = mysql_fetch_array($result)) {
    $firstname = "";
    $payable = "";
    if ($last_name_seen === NULL || $row['firstname'] != $last_name_seen) {
        $last_name_seen = $row['firstname'];
        $firstname = $row['firstname'];
        $payable = $row['payable'];
    }
    echo '<tr>';
    echo '<td style="border: 1px solid black;">'.$firstname.'</td>';
    echo '<td>'.$payable.'</td>';
    echo '<td>'.$row['product'].'</td>';
    echo '<td>'.$row['qty'].'</td>';
    echo '</tr>';
}

请注意,您的MySQL查询应具有一些ORDER BY子句,以生成所需的顺序,例如

Note that your MySQL query should have some ORDER BY clause, to generate the ordering you want, e.g.

ORDER BY Name, Product;

正如上面的评论还暗示的那样,如果您使用的PHP API已弃用,则应考虑升级到更现代的版本.但是,上述循环中使用的逻辑不会随着更改MySQL API的改变而改变.

As the comments above also suggest, if you are using a deprecated PHP API, you should consider upgrading to something more modern. But, the logic used in the above loop would not change much with changing the MySQL API.

这篇关于删除列中的重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆