gpt4 book ai didi

mysql - django 的原始 SQL 语法有错误

转载 作者:行者123 更新时间:2023-11-29 16:02:07 25 4
gpt4 key购买 nike

我遇到错误

(1064, "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 'radians(151.20711)) + sin(radians(-33.86714)) * sin(radians(latitude)))) AS dist' at line 1")

当我运行下面的代码时。

query = '''SELECT id, longitude, latitude, (6378 * acos(cos(radians(-33.86714)) * cos(radians(latitude)) * cos(radians(longitude) – radians(151.20711)) + sin(radians(-33.86714)) * sin(radians(latitude)))) AS distance 
FROM ads_suburb HAVING distance < 10 ORDER BY distance;'''
surrounded_suburbs = Suburb.objects.raw(query)
for suburb in surrounded_suburbs:
print(suburb.id)

哪里需要修复?

已更新

我尝试了另一种方法,但仍然遇到相同错误。

cursor = connection.cursor()
cursor.execute('''SELECT id, longitude, latitude, (6378 * acos(cos(radians(-33.86714)) * cos(radians(latitude)) * cos(radians(longitude) – radians(151.20711)) + sin(radians(-33.86714)) * sin(radians(latitude)))) AS distance FROM ads_suburb HAVING distance < 10 ORDER BY distance;''')
ids = [row[0] for row in cursor.fetchall()]
print(ids)

最佳答案

终于找到解决办法了。这是因为查询中的“-”。我把“-”改成了“-”,成功了。

关于mysql - django 的原始 SQL 语法有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56100293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com