选择MySQL子查询,返回多列

时间:2016-10-24 01:49:40

标签: mysql

我正在使用2张桌子。 locationus_zipcodes

location包含用户坐标和行进速度。

us_zipcodes包含所有城市,其邮政编码和美国的坐标。

此处的目标是返回用户的纬度,经度,速度和最近的城市。

在我的选择中,我试图在select子查询中从us_zipcodes返回2列。我理解有人可能会建议加入,但我的情况不允许使用ON

子查询根据最接近us_zipcodes表中城市坐标的用户坐标选择城市。

我想知道我想做什么是可能的,如果是,那么正确的语法。

我的尝试如下。提前谢谢。

SELECT
    l.lat,
    l.lng,
    l.speed
    (
        SELECT
            CONCAT(city, ' ', state_abrv) AS nearest, 
            (
                3959 * 
                acos( 
                    cos( radians( l.lat ) ) * 
                    cos( radians( lat ) ) * 
                    cos( 
                        radians( lng ) - 
                        radians( l.lng ) 
                    ) + 
                    sin( radians( l.lat ) ) * 
                    sin( radians( lat ) ) 
                ) 
            ) AS distance
        FROM
            us_zipcodes
        ORDER BY
            distance ASC
        LIMIT 0, 1
    ) AS nearest, distance
FROM
    location AS l
WHERE
    l.userID = :userID

编辑:我的表格结构如图所示。

表:location

ID      |       lat         |       lng         |       speed       |       userID  
___________________________________________________________________________________
1       |       55.159399   |       -74.98976   |       35          |       1       
2       |       45.168399   |       -52.56476   |       45          |       2       
3       |       64.593399   |       -64.32576   |       55          |       3       
4       |       98.193399   |       -72.81176   |       65          |       4       

表:us_zipcodes

ID      |       city        |       state_abrv      |       lat     |       lng
______________________________________________________________________________________
1       |       Foo City    |       MI              |       45.3265 |       -81.98747
2       |       Bar City    |       AK              |       65.3265 |       -65.98747
3       |       Fake City   |       FL              |       25.3265 |       -75.98747
4       |       Smith City  |       MI              |       64.3265 |       -89.98747

1 个答案:

答案 0 :(得分:0)

也许这样的事情可以帮助

SELECT
  state_abrv,
  city,
  3959 * acos(
      cos( radians( (SELECT lat FROM location WHERE userID = :userID) ) ) *
      cos( radians( lat ) ) *
      cos(
          radians( lng ) -
          radians( (SELECT lng FROM location WHERE userID = :userID) )
      ) +
      sin( radians( (SELECT lat FROM location WHERE userID = :userID) )) *
      sin( radians( lat ) )
  ) AS distanse,
  (SELECT lng FROM location WHERE userID = :userID) as lng,
  (SELECT lat FROM location WHERE userID = :userID) as lat,
  (SELECT speed FROM location WHERE userID = :userID) as speed

FROM us_zipcodes ORDER BY distanse LIMIT 1;

或只是两个单独的子查询

SELECT
  l.lat,
  l.lng,
  l.speed,
  (SELECT (3959 * acos(
                cos(radians(l.lat)) *
                cos(radians(lat)) *
                cos(radians(lng)-radians(l.lng)) +
                sin(radians(l.lat)) *
                sin(radians(lat)))
  ) 
   FROM us_zipcodes
   ORDER BY distance ASC
   LIMIT 0, 1
  ) AS distance,
  (SELECT CONCAT(city, ' ', state_abrv)
   FROM us_zipcodes
   WHERE 3959 *
         acos(cos(radians(l.lat)) * cos(radians(lat)) *
             cos( radians(lng) - radians(l.lng)) +
             sin(radians(l.lat)) *
             sin(radians(lat))
         ) = distance)  AS nearest
FROM
  location AS l
WHERE
  l.userID = :userID;