尽管我使用GROUP BY,但左连接返回重复数据

时间:2015-07-01 20:21:21

标签: php mysql sql

SQL:

[select* food-choice first_as_label "Preferred food?" "Cake" "Pizza" "Burger" "Salad" "Donut"]

它返回唯一数据。这是返回的数据:

 $sql = "SELECT orders.*,order_products.*,products.*,psettings.*,cities.*,locations.* FROM orders
                LEFT JOIN order_products ON orders.id=order_products.order_id
                LEFT JOIN products ON products.id=order_products.product_id
                LEFT JOIN psettings ON psettings.product_id=order_products.product_id 
                LEFT JOIN cities ON cities.id=orders.city_id
                LEFT JOIN locations ON locations.id=orders.location_id
                WHERE orders.status = '$status'
                ORDER BY  orders.id  ASC  ";

但是当我添加另一个表与订单表连接时,它返回重复的条目。另外,我将订单表左边的'action_bies'加入,如下所示:

Array
(
    [orders] => Array
        (
            [id] => 12
            [name] => Abdus Sattar Bhuiyan
            [email] => sattar.kuet@gmail.com
            [mobile] => 01673050495
            [alt_mobile] => 01818953250
            [city_id] => 2
            [location_id] => 5
            [status] => confirmed
            [cashed] => 1115
            [created] => 2015-07-02 01:07:18
            [modified] => 2015-07-02 01:01:57
            [comment] => 07/02/2015 06:00 am
        )

    [city] => Array
        (
            [id] => 2
            [name] => comilla
        )

    [location] => Array
        (
            [id] => 5
            [city_id] => 2
            [name] => homna
        )

    [order_products] => Array
        (
            [0] => Array
                (
                    [id] => 10
                    [order_id] => 12
                    [product_id] => 1
                    [pieces] => 1
                )

            [1] => Array
                (
                    [id] => 11
                    [order_id] => 12
                    [product_id] => 2
                    [pieces] => 1
                )

            [2] => Array
                (
                    [id] => 12
                    [order_id] => 12
                    [product_id] => 3
                    [pieces] => 3
                )

        )

    [products] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [category_id] => 1
                    [name] => নভোযানের নাম সি প্রোগ্রামিং
                    [writer] => Engr. Abdus Sattar Bhuiyan
                    [created] => 2015-06-24 16:17:45
                )

            [1] => Array
                (
                    [id] => 2
                    [category_id] => 1
                    [name] => Resonance of creativity with C++
                    [writer] => Engr. Abdus Sattar Bhuiyan
                    [created] => 2015-06-26 07:32:52
                )

            [2] => Array
                (
                    [id] => 3
                    [category_id] => 1
                    [name] => programming by story C
                    [writer] => Hasibul Hasan Shanto
                    [created] => 2015-06-26 07:35:57
                )

        )

    [psettings] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [category_id] => 1
                    [product_id] => 1
                    [img] => 1.jpg
                    [desc] => description
                    [created] => 2015-06-29 15:15:58
                    [bppp] => 165
                    [sppp] => 300
                    [discount] => 20
                    [service_charge] => 30
                )

            [1] => Array
                (
                    [id] => 2
                    [category_id] => 1
                    [product_id] => 2
                    [img] => 2.jpg
                    [desc] => 
                    [created] => 2015-06-26 07:33:41
                    [bppp] => 150
                    [sppp] => 250
                    [discount] => 20
                    [service_charge] => 30
                )

            [2] => Array
                (
                    [id] => 3
                    [category_id] => 1
                    [product_id] => 3
                    [img] => 3.jpg
                    [desc] => 
                    [created] => 2015-06-26 07:36:26
                    [bppp] => 150
                    [sppp] => 250
                    [discount] => 10
                    [service_charge] => 30
                )

        )

)

此sql返回重复数据。这是数据:

$sql = "SELECT orders.*,order_products.*,products.*,psettings.*,cities.*,locations.*,action_bies.* FROM orders
            LEFT JOIN order_products ON orders.id=order_products.order_id
             LEFT JOIN action_bies ON orders.id=action_bies.order_id AND action_bies.action='$action'
            LEFT JOIN products ON products.id=order_products.product_id
            LEFT JOIN psettings ON psettings.product_id=order_products.product_id 
            LEFT JOIN cities ON cities.id=orders.city_id
            LEFT JOIN locations ON locations.id=orders.location_id
             WHERE orders.status = '$status'
            ORDER BY  orders.id ASC  ";

这里应该提到action_bies表有重复数据,如下所示: enter image description here

在这种情况下,如何获取唯一数据?感谢您阅读这些大数据。

1 个答案:

答案 0 :(得分:0)

您可以选择最独特的标识符DISTINCT,最有可能是orders_products.id。

示例:

SELECT DISTINCT(orders_products.id) FROM orders_products WHERE x = '$y';

我在原始查询中看不到GROUP BY语句。