需求:
数据库中  一个位置点address  含有2个字段  lon (经度-double)   lat (纬度-double)我要传入一个指定点pointAddress  myLon(经度-double)  myLat(纬度-double)查询是需要根据我的指定点 到一位置点之间的距离进行排序:SQL语句该怎么写啊?  select * from table where order by ( [color=#0000FF]sqrt( pow(abs(lon-mylon)) + pow(abs(lat-myLat)) )[/color] )  descsqrt:正平方根,  pow :平方  abs:绝对值     2.像这样的排序是应该放在sql里面还是在我的编程语言(java)中实现比较好(效率更高)?

解决方案 »

  1.   

    贴记录及要求结果出来看看,更直观一些
    一般来讲,尽量用SQL语句解决,当然要根据具体情况来判断。
      

  2.   

    数据库中的记录 
    lon                        lat
    101.735549926758           25.498735427856
    101.804817199707           26.151327133179
    101.830314636230           26.199480056763…………我传入的
    mylon ,mylat
    101.9   27.0
    根据点到点的距离的公式算出排序后的结果
    101.830314636230           26.199480056763
    101.804817199707           26.151327133179
    101.735549926758           25.498735427856也就是根据      sqrt( pow(abs(lon-mylon)) + pow(abs(lat-myLat)) )   as distace    根据distance来排序
      

  3.   

    select * from tt order by sqrt( pow(abs(lon-mylon)) + pow(abs(lat-myLat)) )or
    select *,sqrt( pow(abs(lon-mylon)) + pow(abs(lat-myLat)) )  as distace 
    from tt order by distace
      

  4.   

    select *
    from address
    order by POW(lon-101.9,2)+POW(lat-27.0,2)
      

  5.   

    排序一般直接在SQL中实现让数据库系统来完成。
    如果你不需要计算距离,只是排序的话,则只需要依据 (x1-x2)^2 + (y1-Y2)^2 排就可以,无需再计算平方根。select *
    from address
    order by (lon-101.9)*(lon-101.9)+(lat-27.0)*(lat-27.0);
      

  6.   

    括号好像有问题?You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') + pow(abs(lat-myLat)) )  as distace
    from sichuan_2_26 order by distace' at line 1
      

  7.   

    还有一个严重的问题。。
    本来我的数据查询都是使用  索引  进行查询的。。
    是这样  select * from table whre id in(idArrays....) and order by  ( (lon-101.9)*(lon-101.9)+(lat-27.0)*(lat-27.0)  )
    如果在数据库添加这个距离的查询,是不是要遍历表啊??还是先选出  id  再在这些id中判断距离?
      

  8.   

    将ID存入临时表中再与工作表连接,
    先查ID,再判断距离
      

  9.   

    还有请问一下  where  id<100000 and order by ( (lon-101.9)*(lon-101.9)+(lat-27.0)*(lat-27.0)  )  查询不起啊?,基础不好。请见谅!还有可以不可以在记录的末尾添加一个字段,就是找出的记录个数 count(*)?
      

  10.   

    where  id <100000 
    order by ( (lon-101.9)*(lon-101.9)+(lat-27.0)*(lat-27.0)  ) 
      

  11.   

    还有可以不可以在记录的末尾添加一个字段,就是找出的记录个数 count(*)? 
    是一条还是每一条记录后面都要有count(*)
      

  12.   

    哦。。!谢谢哈。。!看您2种都写下呢。。将就学习一下啊
    一条的话  是不是这样?select count(*) from tt where ......
    还是 在找出的结果
    第一个记录是count(*) 的记录数?
      

  13.   

    在找出的结果 再COUNT(*)
    每一条记录要用到分组
    SELECT F1,F2... FROM ...
    LEFT JOIN .... ON ....
    GROUP BY F1,F2....基本这种语法
      

  14.   


    不是很懂。。
    请问一下能不能在  结果的第一行  显示  count(*)
      

  15.   

    请问一下能不能在  结果的第一行  显示  count(*) :举例说明
      

  16.   

    根据我的需求,。。我想了下。。还是在每条记录的末尾添加一个字段   counts--显示count(*)
      

  17.   

    select * from sichuan_2_26 where gridID in(5518184078,5518184079,5518184080,5518364078,5518364079,5518364080,5518544078,5518544079,5518544080) and kind like '220%' order by ((longitude-104.079429626465)*(longitude-104.079429626465)+(latitude-30.657341003418)*(latitude-30.657341003418)) asc如果我的查询像上面这个语句?
    怎么添加啊?
    上面语句是可以执行的!!
      

  18.   

    例如:
    table1:
     id   name 
     1    fank 
      

  19.   

    例如:
    table1:
     id   name 
     1    test1
     2    asd2
     3    test3
     4    asd4
     5    test5
    结果类似:id   name   counts
    1    test1   3
    3    test3   3
    5    test5   3
     
      

  20.   

    统计就是:
    select count(*) from sichuan_2_26 where gridID in(5518184078,5518184079,5518184080,5518364078,5518364079,5518364080,5518544078,5518544079,5518544080) and kind like '220%' order by ((longitude-104.079429626465)*(longitude-104.079429626465)+(latitude-30.657341003418)*(latitude-30.657341003418)) asc
      

  21.   

    就是说第一条记录都要有count(*),速度慢一些,
    select * from sichuan_2_26,
    (select count(*) from sichuan_2_26 where gridID in(5518184078,5518184079,5518184080,5518364078,5518364079,5518364080,5518544078,5518544079,5518544080) and kind like '220%' order by ((longitude-104.079429626465)*(longitude-104.079429626465)+(latitude-30.657341003418)*(latitude-30.657341003418)) asc
    )
     where gridID in(5518184078,5518184079,5518184080,5518364078,5518364079,5518364080,5518544078,5518544079,5518544080) and kind like '220%' order by ((longitude-104.079429626465)*(longitude-104.079429626465)+(latitude-30.657341003418)*(latitude-30.657341003418)) asc
      

  22.   

    谢谢 可以运行了select * from sichuan_2_26,
    (select count(*) from sichuan_2_26 where gridID in(5518184078,5518184079,5518184080,5518364078,5518364079,5518364080,5518544078,5518544079,5518544080) and kind like '220%' order by ((longitude-104.079429626465)*(longitude-104.079429626465)+(latitude-30.657341003418)*(latitude-30.657341003418)) asc
    )   as  counts
    where gridID in(5518184078,5518184079,5518184080,5518364078,5518364079,5518364080,5518544078,5518544079,5518544080) and kind like '220%' order by ((longitude-104.079429626465)*(longitude-104.079429626465)+(latitude-30.657341003418)*(latitude-30.657341003418)) asc
      

  23.   

    不过从效率上讲,最后一条COUNT(*) 要好一些