简体   繁体   English

我的MySQL SELECT查询返回相同行的倍数

[英]My MySQL SELECT Query is Returning Multiples of the Same Rows

For some reason I'm getting just one duplicate row that I can see, but can't really pinpoint where in the query I've gone wrong. 由于某种原因,我只能看到一个重复的行,但无法真正查明查询中我哪里出错了。

$stmt = $mysqli->query("
    SELECT DISTINCT vendors_tbl.email AS email,
    (vendor_avails_tbl.standard_pricing - vendor_loc_tbl.offpeak_time_pricing) AS best_margins,
    vendor_loc_tbl.location_id AS locationID,
    vendor_loc_tbl.loc_img_path AS locImg, 
    vendor_loc_tbl.offpeak_time_pricing AS offpeak,
    vendor_loc_tbl.address1 AS address1,
    vendor_loc_tbl.address2 AS address2,
    vendor_loc_tbl.zip_code AS zip,
    vendor_loc_tbl.geocodes AS geo,
    vendor_loc_tbl.has_valet AS valet,
    vendor_loc_tbl.has_transport AS transport,
    vendor_loc_tbl.has_wheelchair AS wheelchair,
    vendor_loc_tbl.has_desk AS desk,
    vendor_loc_tbl.has_24hours AS open24hrs,
    vendor_loc_tbl.has_covered AS covered,
    vendor_loc_tbl.has_security AS security, 
    (vendor_avails_tbl.available_economy + vendor_avails_tbl.available_standard + vendor_avails_tbl.available_midsize + vendor_avails_tbl.available_truck_suv) AS avail_total, 
    vendor_avails_tbl.standard_pricing AS standard_pricing, 69 *
    DEGREES(ACOS(COS(RADIANS($e_lat))
         * COS(RADIANS(SUBSTR(vendor_loc_tbl.geocodes, 1, 10)))
         * COS(RADIANS($e_lon) - RADIANS(SUBSTR(vendor_loc_tbl.geocodes, 13)))
         + SIN(RADIANS($e_lat))
         * SIN(RADIANS(SUBSTR(vendor_loc_tbl.geocodes, 1, 10))))) AS distance_in_m
    FROM vendors_tbl
    INNER JOIN vendor_loc_tbl ON vendor_loc_tbl.vendor_id = vendors_tbl.vendor_id
    INNER JOIN vendor_avails_tbl ON vendor_avails_tbl.location_id = vendor_loc_tbl.location_id
    WHERE vendor_avails_tbl.available_standard > 0
    ORDER BY vendor_loc_tbl.override_level DESC, best_margins DESC, distance_in_m ASC
    LIMIT 5
");

Well In some cases it's bettor to use GROUP BY statement than DISTINCT . 在某些情况下,最好使用GROUP BY语句而不是DISTINCT In your example you should add a group by at least on one col (some unique for example id). 在您的示例中,您应至少在一个栏上添加一个组(例如ID的某些唯一名称)。 When you're using aggregates in query it is better to use group by, when using only joins - distinct is enough. 当您在查询中使用聚合时,最好仅在使用联接时使用分组方式-单独就足够了。

Your distinct create the group of all the fields you have specified as you did not specify the group by condition. 由于您没有按条件指定分组,因此您可以创建已指定的所有字段的分组。 So there is no chance to get duplicate value. 因此,没有机会获得重复的价值。

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

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