Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.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-查找同一列中任意两个整数之间的差异_Mysql - Fatal编程技术网

MySQL-查找同一列中任意两个整数之间的差异

MySQL-查找同一列中任意两个整数之间的差异,mysql,Mysql,我有不同纬度和经度的商品价格数据。我希望通过比较价格差异和旅行距离来寻找利润机会,以获得更好的价格。 伪编码公式目前看起来如下所示: select (100*diff(avg(price))-50*diff(lon)-70*diff(lat)) as profit 因此,我希望找出数据集中任意两个纬度值之间的差异。我看到过一些回答,解释了如何找到连续值之间的差异或日期差异,但似乎没有任何回答我的特定问题 使用我当前的查询进行编辑这应该只需按降序向我提供两个最遥远的城市纬度: 选择lat作为la

我有不同纬度和经度的商品价格数据。我希望通过比较价格差异和旅行距离来寻找利润机会,以获得更好的价格。 伪编码公式目前看起来如下所示:

select (100*diff(avg(price))-50*diff(lon)-70*diff(lat)) as profit
因此,我希望找出数据集中任意两个纬度值之间的差异。我看到过一些回答,解释了如何找到连续值之间的差异或日期差异,但似乎没有任何回答我的特定问题

使用我当前的查询进行编辑这应该只需按降序向我提供两个最遥远的城市纬度: 选择lat作为lat2,lat2-lat作为lat距离,城市 从购买内部连接在lat2上购买=lat 按latdistanceasc订购

输出应该列出涉及的两个城市,因为每个城市都有一个纬度,尽管我不确定如何使它们都显示在输出中

想象3个带有价格、纬度和经度的数据点: A 10,30,50 B 15、50、60 C 5、20、30

为了保持简单,任意两点之间的纬度距离是多少? 输出应为:

AB-20 AC-10
BC-30将当前位置与所有其他城市进行比较并显示利润:

SELECT
    here.city AS Here,
    there.city AS There,
    here.price - there.price AS Profit,
    ABS(here.lat - there.lat) AS Distance
FROM buying AS here
-- Join to the table itself, but not to the same city
LEFT JOIN buying AS there ON (here.city <> there.city)
-- Remove this to compare all cities to every other city
WHERE here.city = 'Springfield'

此查询适用于您的条件:

SELECT b1.city start,
       b2.city finish,
       abs(b1.latitude - b2.latitude) latdistance
FROM buying b1, buying b2
WHERE b1.city < b2.city
ORDER BY 3

在.

的两个查询中,您应该提供具体的示例:您对输入有什么要求,以及您对输出有什么要求。如上所述,这可能很简单,因为SELECT abslat1-LAT2添加了一个包含更多信息的编辑。您的编辑似乎并没有让它变得更清楚:您应该提供具体的示例,说明您拥有什么,以及您希望得到什么,最好是数字。顺便说一句,你的距离测量是奇怪的。即使不完美,我也会使用sqrtlat1-lat2^2+lon1-lon2^2作为距离的度量。此查询将生成比要求的更多的输出行。
SELECT b1.city start,
       b2.city finish,
       sqrt(pow(b1.latitude  - b2.latitude,  2)
          + pow(b1.longitude - b2.longitude, 2)) latdistance
FROM buying b1, buying b2
WHERE b1.city < b2.city
ORDER BY 3