MySQL如何根据WHERE子句中的参数顺序获取结果

发布于 2024-10-18 11:10:00 字数 1116 浏览 1 评论 0原文

我有以下查询语句:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女 2024-10-25 11:10:00

首先,不要对连接使用逗号分隔语法。使用 ISO 标准。其次,为了实现您想要的目标,您需要以不同的方式构建查询。如果您为输入提供排序顺序,您就可以实现您想要的效果。除了提供排序功能之外,您还可以更轻松地构建输入列表:

Select idelm_na AS ID, nd.lat_nd AS lat, nd.lon_nd AS 'lon' , adr.road_adr AS 'road' 
From node_nd As ND
    Join node_address As NA
        On NA.idelm_nd = ND.idelm_na
    Join address_adr As ADR
        On ADR.id_adr = NA.idadr_na
    Join    (
            Select 1 As Rnk, 14.654733 As Lat, 121.058403 As Lon
            Union All Select 2, 14.654791, 121.062386
            Union All Select 3, 14.654791, 121.064343
            Union All Select 4, 14.654754, 121.064403
            Union All Select 5, 14.654648, 121.064450
            Union All Select 6, 14.653869, 121.064798
            Union All Select 7, 14.653865, 121.065399
            Union All Select 8, 14.653880, 121.066532
            ) As Z
        On Round(lat_nd, 6) = Z.Lat
            And Round(lon_nd, 6) = Z.Lon
Order By Z.Rnk

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 idelm_na AS ID, nd.lat_nd AS lat, nd.lon_nd AS 'lon' , adr.road_adr AS 'road' 
From node_nd As ND
    Join node_address As NA
        On NA.idelm_nd = ND.idelm_na
    Join address_adr As ADR
        On ADR.id_adr = NA.idadr_na
    Join    (
            Select 1 As Rnk, 14.654733 As Lat, 121.058403 As Lon
            Union All Select 2, 14.654791, 121.062386
            Union All Select 3, 14.654791, 121.064343
            Union All Select 4, 14.654754, 121.064403
            Union All Select 5, 14.654648, 121.064450
            Union All Select 6, 14.653869, 121.064798
            Union All Select 7, 14.653865, 121.065399
            Union All Select 8, 14.653880, 121.066532
            ) As Z
        On Round(lat_nd, 6) = Z.Lat
            And Round(lon_nd, 6) = Z.Lon
Order By Z.Rnk
沦落红尘 2024-10-25 11:10:00

您可以在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文