【问题标题】:Joining of two sets of result from a single table连接单个表中的两组结果
【发布时间】:2013-05-30 00:58:23
【问题描述】:

我有一个广告表,其中包含具有纬度和经度值的精选和非精选广告, 当用户从某个位置搜索广告距离时,我想在顶部显示距离所选位置 500 公里以下的特色广告,所有其他广告距离无限远,然后是特色列表。

我可以先显示特色列表到无限距离,然后使用距离功能和按特色条款排序的其他剩余广告。但是如何让精选列表先显示在 500 公里以下,然后将剩余的广告显示到无限距离。

我需要使用union吗,有没有其他解决方案?

源位置的

latitude = -37.814563
longitude = 144.970267
SELECT da.*,( 6371 * acos( cos(radians(144.970267)) * cos(radians(da.loc_lng)) * 
cos(radians(-37.814563) - radians(da.loc_lat)) + sin(radians(144.970267)) *  
sin(radians(da.loc_lng)) )) as distanceSort FROM directory_ads da WHERE da.approved = 1 
AND da.deleted = 0  ORDER BY da.featured DESC, distanceSort ASC

【问题讨论】:

标签: php mysql join union


【解决方案1】:

您可以使用以下带有条件的order by 子句来做到这一点:

order by (case when da.featured > 0 and DistanceSort < 500
               then distance_sort else 500
          end),
         DistanceSort

【讨论】:

  • 戈尔丹,你太棒了。你拯救了我的一天。
  • @tanz 。 . .很高兴我能提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-04
  • 2016-07-27
  • 2021-02-24
  • 1970-01-01
相关资源
最近更新 更多