SQL' Group By'条款更改数组结果

时间:2013-07-16 14:57:59

标签: php sql

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

GROUP:

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"}]

1 个答案:

答案 0 :(得分:1)

Group By子句将匹配的行合并为一行。因此,当您在cable_no上进行分组时,您会得到两行,因为您有两个唯一的cable_no(80010和90017)。

如果您想要消除重复项,请下拉,您应该在字段上使用您要过滤重复项的唯一子句。