在SELECT语句之外操作变量

时间:2018-05-29 14:56:47

标签: mysql sql

SELECT @rownum := @rownum +1 AS rank, 
    round(LAT_N,4) as lat 
FROM STATION 
HAVING rank = CEIL(@rowCount/2) 
ORDER BY LAT_N 

我怎么能不显示排名?或者如何操作/分配变量以外的变量?

1 个答案:

答案 0 :(得分:1)

使用子查询:

SELECT lat
FROM (SELECT @rownum := @rownum +1 AS rank, 
            round(LAT_N,4) as lat 
      FROM STATION CROSS JOIN
           (SELECT @rownum := 0) params
      ORDER BY lat_n
     ) s
WHERE rank = CEIL(@rowCount / 2) ;

请注意,在更新版本的MySQL中,您可能需要在子查询中进行排序。