mysql - 在 MySQL 中隐藏别名列

标签 mysql sql

这可能是重复的问题,但需要帮助。 我是 MySQL 新手。

这是我的问题。

我有一个查询来计算纬度和经度之间的距离。根据距离顺序我必须返回 id。

SELECT dlo.id,
       (3959 * acos(cos(radians(12.9)) * cos(radians(y(gproperty))) * cos(radians(x(gproperty)) - radians(77.5)) +sin(radians(12.9)) * sin(radians(y(gproperty))))) AS distance
FROM db1.gfeature dgf,
     db2.loc dlo,
     db2.cust dcu
WHERE gf.o_type = 6
  AND dcu.id = 240
  AND dgf.o_id = dlo.p_id HAVING distance < 20
ORDER BY distance LIMIT 10;

返回为

+------+-----------------------+
| id   | distance              |
+------+-----------------------+
|  101 | 0.00025714756425665   |
|  199 | 0.10971525612556807   |
|  722 | 0.22772618588406165   |
+------+-----------------------+

但我只需要显示 id 列。我asked same-question昨天。但现在我使用三个表来获取数据。连接 3 个表时很困惑。

有人可以推荐我吗?

我试过这个方法

select id from (
  select 
    dlo.id,
    ( 3959 * acos(   cos( radians(12.9) ) 
                   * cos( radians( y(gproperty) ) )
                   * cos( radians( x(gproperty) ) - radians(77.5) )

                   + sin( radians(12.9) )
                   * sin( radians(y(gproperty) ) )
                 )
    ) AS distance 
  from db1.gfeature dgf 
       join db2.cust dcu, db2.loc dlo 
         on dgf.o_type = 6 and dcu.id = 10 and dgf.o_id = dlo.w_id
) t 
where distance < 10 
order by distance 
limit 10;

但是在“on...”附近出现语法错误

最佳答案

也许是这样的。只需使用子查询即可实现。

SELECT S.ID 
FROM
    (SELECT dlo.id,
           (3959 * acos(cos(radians(12.9)) * cos(radians(y(gproperty))) * cos(radians(x(gproperty)) - radians(77.5)) +sin(radians(12.9)) * sin(radians(y(gproperty))))) AS distance
    FROM db1.gfeature dgf, db2.loc dlo , db2.cust dcu
    WHERE gf.o_type = 6 AND dcu.id = 240 AND dgf.o_id = dlo.p_id 
    HAVING distance < 20) S 
ORDER BY S.distance 
LIMIT 10;

关于mysql - 在 MySQL 中隐藏别名列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24423711/

相关文章:

sql - PostgreSQL 通配符 LIKE 用于子查询返回的任何单词列表

PHP/MySQL 查询 : Show results based on 2 conflicting conditions

android - 检查 SQL 查询是否在 Android SQLite 上成功

mysql - SQL 分组 : conditional select in each group

php - 致命异常 AsyncTask #2?

MySQL 在工作台上运行良好的查询上出现错误

MySQL导入命令仅返回帮助信息

mysql - 编写一个 sql 查询以使用来自另一列的子字符串将列添加到数据框

android - 表 "Tablename"没有名为 "columnname"的列,但它有一个

java - MySQL last_insert_id() 在正确的事务中返回 0