Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Compex SQL查询_Mysql_Sql - Fatal编程技术网

Mysql Compex SQL查询

Mysql Compex SQL查询,mysql,sql,Mysql,Sql,我有一张城市表: cities(..., name, x, y, ...) 和用户表: users(..., city-name, ....) 另外,我有两个变量,C和R。 第一个包含城市名称,第二个包含半径。 我需要制定有效的SQL查询,从城市C中选择radius R中的所有用户 谢谢。我想: select user_id from Users where city_id in ( select city_id

我有一张城市表:

cities(..., name, x, y, ...)
和用户表:

users(..., city-name, ....)
另外,我有两个变量,C和R。 第一个包含城市名称,第二个包含半径。 我需要制定有效的SQL查询,从城市C中选择radius R中的所有用户


谢谢。

我想:

select
    user_id
from
    Users
where
    city_id in (
        select
            city_id
        from
            Cities
        where
            sqrt(((select x from Cities where name = "London") - x)^2 + ((select y from Cities where name = "London")-y)^2) < 50
);

我会想到以下几点:

select
    user_id
from
    Users
where
    city_id in (
        select
            city_id
        from
            Cities
        where
            sqrt(((select x from Cities where name = "London") - x)^2 + ((select y from Cities where name = "London")-y)^2) < 50
);

使用粗略距离计算:-

SELECT
FROM Users
INNER JOIN Cities UsersCities
ON Users.CityName = UsersCities.CityName
INNER JOIN Cities
ON SQRT(POW((Cities.x-UsersCities.X), 2) + POW((Cities.y-UsersCities.Y), 2)) < someradiusconverted
WHERE Cities.CityName = somecityname

使用粗略距离计算:-

SELECT
FROM Users
INNER JOIN Cities UsersCities
ON Users.CityName = UsersCities.CityName
INNER JOIN Cities
ON SQRT(POW((Cities.x-UsersCities.X), 2) + POW((Cities.y-UsersCities.Y), 2)) < someradiusconverted
WHERE Cities.CityName = somecityname


你为什么不自己动手做sql,然后把你的想法发布出来,这样我们就可以看到你在努力解决什么问题了?你能提供一个样本输出吗?有哪些表C和R?城市表中x和y的关系是什么?为了高效查询,您需要一个设计良好的模式,即在用户中,您应该使用城市ID作为FK索引,而不是城市名称。@Nithesh城市:伦敦x=300 y=200在半径50范围内,从克罗伊登x=310 y=215,因为:sqrt300-310^2+200-215^2=18<50为什么你不自己动手做sql,然后发布你的想法,这样我们就可以看到你正在努力解决的问题..半径是多少?你能提供一个样本输出吗?有哪些表C和R?城市表中x和y的关系是什么?为了高效查询,您需要一个设计良好的模式,即在用户中,您应该使用城市ID作为FK索引,而不是城市名称。@Nithesh城市:伦敦x=300 y=200在半径50范围内,从克罗伊登x=310 y=215,因为:sqrt300-310^2+200-215^2=18<50这是所有建议中最有效的。。。尝试提出更有效的建议…从性能的角度来看,根据我的经验,使用子查询比使用连接更安全,因为这会迫使优化人员想出最佳计划,即首先找到附近的城市。从所有建议中?我只看到这个答案。。。你等了23分钟?这看起来像是在使用一个相关的子选择,使用x从用户的内部选择,这些通常执行badly@zeharon当前位置你对此持肯定态度,还是只是想鼓励别人提出更好的建议?这是所有建议中最有效的。。。尝试提出更有效的建议…从性能的角度来看,根据我的经验,使用子查询比使用连接更安全,因为这会迫使优化人员想出最佳计划,即首先找到附近的城市。从所有建议中?我只看到这个答案。。。你在等什么,23分钟?看起来它使用了一个相关的子选择,使用内部选择中用户的x,这些子选择通常执行badly@zeharon当前位置您对此持肯定态度,还是只是为了鼓励他人提出更好的建议?请立即尝试,但不要进行测试,因为我不知道您的数据库是什么样的。请立即尝试,但不要像我一样进行测试不知道你的数据库是什么样的