dongyue0263 2013-10-20 10:23
浏览 37
已采纳

MySQL查询从不同的表中获取数据并将其用于距离计算

I have two MySQL tables:

CREATE TABLE IF NOT EXISTS `wp_zipcode` (
  `zip` int(5) NOT NULL AUTO_INCREMENT,
  `lat` float(10,6) NOT NULL,
  `lng` float(10,6) NOT NULL,
  PRIMARY KEY (`zip`)
)

and

CREATE TABLE IF NOT EXISTS `wp_events` (
  `EventID` int(11) NOT NULL,
  `Event` varchar(256) NOT NULL,
  `Zip` int(11) NOT NULL,

Unfortunately I cannot alter the wp_events(it gets overwritten with new data al the time) and I am doing a "distance" search for the nearest events to the user. I am using Google's Example found here

( 3959 * acos( cos( radians(34.070358) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-118.349258) ) + sin( radians(34.070358) ) * sin( radians( lat ) ) ) ) AS distance FROM wp_events HAVING distance < 25 ORDER BY distance LIMIT 0 , 20

My issue is like this: For the algorithim to work, I need to have the lng and lat of a zipcode, but since Im searching in wp_events for the events, is there a way to get the event's zipcode from wp_events and somehow(this is where my question comes in) get the corresponding lat and lng for that specific zipcode from wp_zipcodes and return to the SQL statment to get the appropriate events near the desired zipcode.

thanks in advance

  • 写回答

2条回答 默认 最新

  • douzhan1935 2013-10-20 10:51
    关注
    SELECT wp_events.*, 
           wp_zipcode.lat, 
           wp_zipcode.lng,
           ( 3959 * acos( cos( radians(34.070358) ) * cos( radians( wp_zipcode.lat ) ) * cos( radians( wp_zipcode.lng ) - radians(-118.349258) ) + sin( radians(34.070358) ) * sin( radians( wp_zipcode.lat ) ) ) ) AS distance
      FROM wp_events 
      LEFT JOIN wp_zipcode 
        ON wp_zipcode.zip = wp_events.Zip
    HAVING distance < 25 
     ORDER BY distance 
     LIMIT 0 , 20
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决