0

出于某种原因,当我在 DISTANCE 区域中使用 group by 子句时,我得到了不同的结果。当我使用 GROUP BY 时,我能够正确合并所有电缆编号,但是距离似乎与我不使用 GROUP BY 子句时不同......

团体:

SELECT cable_no,
       3959.0 * 5280.0 * acos(sin(radians($lat)) * sin(radians(mh_lat_a))
       + cos(radians($lat)) * cos(radians(mh_lat_a))
       * cos(radians(mh_long_a) - radians($lon))) as distance
 FROM extraction_worksheet
 WHERE mh_lat_a != ''
 GROUP BY cable_no
 HAVING distance <= $radius

 [{"cable_no":"90017","distance":"1604.6550964957787"},{"cable_no":"90018","distance":"41.09618442526487"}]

常规的

SELECT cable_no, section_no,
      3959.0 * 5280.0 * acos(sin(radians($lat)) * sin(radians(mh_lat_a))
      + cos(radians($lat)) * cos(radians(mh_lat_a))
      * cos(radians(mh_long_a) - radians($lon))) as distance
FROM extraction_worksheet
WHERE mh_lat_a != ''
HAVING distance <= $radius

[{"cable_no":"80010","section_no":"9","distance":"1398.9443263587134"},{"cable_no":"80010","section_no":"8","distance":"1669.0854859250592"},{"cable_no":"90017","section_no":"1","distance":"1604.6550964957787"},{"cable_no":"80010","section_no":"7","distance":"1831.1275113138754"},{"cable_no":"80010","section_no":"10","distance":"917.955911900792"},{"cable_no":"80017","section_no":"7","distance":"1788.0906282411715"},{"cable_no":"80017","section_no":"8","distance":"1685.9977194337882"},{"cable_no":"80017","section_no":"9","distance":"1544.2115596354815"},{"cable_no":"80017","section_no":"10","distance":"869.511708783779"},{"cable_no":"80010","section_no":"11","distance":"479.2133866402672"},{"cable_no":"80017","section_no":"11","distance":"428.6618645292709"},{"cable_no":"90018","section_no":"1","distance":"41.09618442526487"}]
4

1 回答 1

1

The Group By clause consolidates the matched rows into one row. Thus when you are grouping on the cable_no you get two rows, since you have two unique cable_no (80010 and 90017).

For you drop down if you wanted to eliminate duplicates you should use the unique clause on the field that you want to filter duplicates for.

于 2013-07-16T16:31:32.997 回答