mysql - 如何在 mysql 中运行子查询

标签 mysql sql

我正在尝试从查询中的另一个表运行子查询

我的查询如下:

SELECT *, (6371000 * acos(cos(radians(select point_oi.lng
                                     from point_oi
                                     where point_oi.name like '%Main Square%')
                            ) 
* cos(radians(restaurants.lat)) * cos(radians(restaurants.lng) 
- radians(select point_oi.lng
         from point_oi
         where point_oi.name like '%Main Square%'
        )) 
+ sin(radians(select point_oi.lng
             from point_oi
             where point_oi.name like '%Main Square%'))
* sin(radians(restaurants.lat)))) AS distance
FROM restaurants
HAVING distance < 500;

当我运行查询时,我收到一条错误消息,指出 select 附近存在错误。 我想使用嵌套选择查询从另一个表获取纬度和经度,而不是对值进行硬编码。 我该如何解决这个问题。

感谢您的帮助

最佳答案

如果 suquery 返回多于一行,则出现错误,则不应使用子查询来检索 point_poi lat, lnt ..

尝试使用正确的连接(在这种情况下,FATC 是否在 point_poi 和餐厅之间没有关系,您可以使用交叉连接)

  SELECT restaurants.*, 
    (6371000 * acos(cos(radians(point_oi.lng )) 
  * cos(radians(restaurants.lat)) * cos(radians(restaurants.lng) 
  - radians(point_oi.lng )) 
  + sin(radians(point_oi.lng ))
  * sin(radians(restaurants.lat)))) AS distance
  FROM restaurants
  CROSS JOIN point_oi 
  WHERE   point_oi.name like '%Main Square%'
  AND (6371000 * acos(cos(radians(point_oi.lng )) 
  * cos(radians(restaurants.lat)) * cos(radians(restaurants.lng) 
  - radians(point_oi.lng )) 
  + sin(radians(point_oi.lng ))
  * sin(radians(restaurants.lat)))) < 500;

关于mysql - 如何在 mysql 中运行子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59074665/

相关文章:

php - 从 2 个表中获取内容?

mysql - 连接 3 个表,其中 1 个表行成为列

php - 更好地理解如何传递变量

c# - 更新 SQL 命令 C#

java - 从 SQL 中的单独表中获取 ID

mysql - 查询将获取 orderID 12345 中所有商品的商品名称和商品类别名称

sql - 使用单列表

php - 当我从 MySQL 向网页发送信息时更改表的属性

mysql - 当值不存在时如何显示 0?

php - 加一到mysql取值问题