MySQL如何根据WHERE子句中的参数顺序获取结果
我有以下查询语句:
SELECT idelm_na AS ID, nd.lat_nd AS lat, nd.lon_nd AS 'lon' , adr.road_adr AS 'road' FROM node_nd nd, node_address_na na, address_adr adr WHERE ((ROUND(lat_nd,6)=ROUND(14.654733,6) AND ROUND(lon_nd,6)=ROUND(121.058403,6)) OR (ROUND(lat_nd,6)=ROUND(14.654791,6) AND ROUND(lon_nd,6)=ROUND(121.062386,6)) OR (ROUND(lat_nd,6)=ROUND(14.654791,6) AND ROUND(lon_nd,6)=ROUND(121.064343,6)) OR (ROUND(lat_nd,6)=ROUND(14.654754,6) AND ROUND(lon_nd,6)=ROUND(121.064403,6)) OR (ROUND(lat_nd,6)=ROUND(14.654648,6) AND ROUND(lon_nd,6)=ROUND(121.06445,6)) OR (ROUND(lat_nd,6)=ROUND(14.653869,6) AND ROUND(lon_nd,6)=ROUND(121.064798,6)) OR (ROUND(lat_nd,6)=ROUND(14.653865,6) AND ROUND(lon_nd,6)=ROUND(121.065399,6)) OR (ROUND(lat_nd,6)=ROUND(14.653880,6) AND ROUND(lon_nd,6)=ROUND(121.066532,6))) AND na.idelm_na = nd.idelm_nd AND adr.id_adr = na.idadr_na;
它的作用是返回与坐标(纬度,经度)关联的adr.road_adr。默认情况下,它返回的内容按 idelm_na 排序。有没有办法重写此代码,根据 OR 参数/参数的顺序返回结果?我可以将 UNION ALL 与单独的 SELECT 语句一起使用,但我认为这太慢了。我希望有人能在这里帮助我。谢谢!
I have the following query statement:
SELECT idelm_na AS ID, nd.lat_nd AS lat, nd.lon_nd AS 'lon' , adr.road_adr AS 'road' FROM node_nd nd, node_address_na na, address_adr adr WHERE ((ROUND(lat_nd,6)=ROUND(14.654733,6) AND ROUND(lon_nd,6)=ROUND(121.058403,6)) OR (ROUND(lat_nd,6)=ROUND(14.654791,6) AND ROUND(lon_nd,6)=ROUND(121.062386,6)) OR (ROUND(lat_nd,6)=ROUND(14.654791,6) AND ROUND(lon_nd,6)=ROUND(121.064343,6)) OR (ROUND(lat_nd,6)=ROUND(14.654754,6) AND ROUND(lon_nd,6)=ROUND(121.064403,6)) OR (ROUND(lat_nd,6)=ROUND(14.654648,6) AND ROUND(lon_nd,6)=ROUND(121.06445,6)) OR (ROUND(lat_nd,6)=ROUND(14.653869,6) AND ROUND(lon_nd,6)=ROUND(121.064798,6)) OR (ROUND(lat_nd,6)=ROUND(14.653865,6) AND ROUND(lon_nd,6)=ROUND(121.065399,6)) OR (ROUND(lat_nd,6)=ROUND(14.653880,6) AND ROUND(lon_nd,6)=ROUND(121.066532,6))) AND na.idelm_na = nd.idelm_nd AND adr.id_adr = na.idadr_na;
What it does is that it returns the adr.road_adr associated to the coordinates(lat,lon). What it returns is ordered by idelm_na by default. Is there a way to rewrite this code that will return the result based on the order of the OR arguments/parameters? I can use UNION ALL with individual SELECT statements, but I think this is too slow. I hope someone can help me here. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,不要对连接使用逗号分隔语法。使用 ISO 标准。其次,为了实现您想要的目标,您需要以不同的方式构建查询。如果您为输入提供排序顺序,您就可以实现您想要的效果。除了提供排序功能之外,您还可以更轻松地构建输入列表:
First, do not use the comma-separated syntax for Joins. Use the ISO standard. Second, to achieve what you want, you need to build the query differently. You can achieve what you want if you supply a sort order to the inputs. In addition to providing the ability to do your sort, you also make it easier to build the list of inputs:
您可以在 SELECT 语句中添加“ORDER BY”子句:
SELECT idelm_na AS ID, ... WHERE ... ORDER BY lat_nd, lon_nd;
这将首先按 lat_nd 对结果进行排序,然后按 lon_nd 对结果进行排序。
You can add an "ORDER BY" clause to the SELECT statement:
SELECT idelm_na AS ID, ... WHERE ... ORDER BY lat_nd, lon_nd;
This will sort the results first by lat_nd, then by lon_nd.