1

我正在尝试合并两个 MySQL 语句,我想我已经接近解决这个问题但我得到“#1054 - '字段列表'中的未知列 'lat'”指的是下面的粗体文本。

我试过用“”、“”甚至“”来包含latlng文本,但没有效果。

SELECT post_attributes_3.value AS name, clean, post_attributes.value AS lat, post_attributes_1.value AS lng, post_attributes_2.value AS category, ( 3959 * acos( cos( radians('%s') ) * cos( radians( **lat** ) ) * cos( radians( **lng** ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( **lat** ) ) ) ) AS distance
FROM post_attributes AS post_attributes_3 INNER JOIN ((post_attributes AS post_attributes_1 INNER JOIN (posts INNER JOIN post_attributes ON posts.id = post_attributes.post_id) ON post_attributes_1.post_id = posts.id) INNER JOIN post_attributes AS post_attributes_2 ON posts.id = post_attributes_2.post_id) ON post_attributes_3.post_id = posts.id
WHERE (((post_attributes_3.name)="name") AND ((post_attributes_2.name)="category_id") AND ((post_attributes.name)="lat") AND ((post_attributes_1.name)="lng"))
HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20;

我在下面组合的两个 SQL 语句:

SELECT name, clean, lat, lng, category, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM categorize HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20;

SELECT post_attributes_3.value AS name, clean, post_attributes.value AS lat, post_attributes_1.value AS lng, post_attributes_2.value AS category
FROM post_attributes AS post_attributes_3 INNER JOIN ((post_attributes AS post_attributes_1 INNER JOIN (posts INNER JOIN post_attributes ON posts.id = post_attributes.post_id) ON post_attributes_1.post_id = posts.id) INNER JOIN post_attributes AS post_attributes_2 ON posts.id = post_attributes_2.post_id) ON post_attributes_3.post_id = posts.id
WHERE (((post_attributes_3.name)="name") AND ((post_attributes_2.name)="category_id") AND ((post_attributes.name)="lat") AND ((post_attributes_1.name)="lng"));

这两个 MySQL 语句分别很好,但它可能与使用 AS 语句有关。

更新:感谢您的帮助,但由于使用此选择查询的方式,我选择创建一个 MySQL 视图并使用它来运行与我的第一个类似的 SQL 命令。

4

1 回答 1

1

尝试用真实的列名替换别名:

SELECT post_attributes_3.value AS name, clean, post_attributes.value AS lat, post_attributes_1.value AS lng, post_attributes_2.value AS category, ( 3959 * acos( cos( radians('%s') ) * cos( radians( post_attributes.value ) ) * cos( radians( post_attributes_1.value ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( post_attributes.value ) ) ) ) AS distance
FROM post_attributes AS post_attributes_3 INNER JOIN ((post_attributes AS post_attributes_1 INNER JOIN (posts INNER JOIN post_attributes ON posts.id = post_attributes.post_id) ON post_attributes_1.post_id = posts.id) INNER JOIN post_attributes AS post_attributes_2 ON posts.id = post_attributes_2.post_id) ON post_attributes_3.post_id = posts.id
WHERE (((post_attributes_3.name)="name") AND ((post_attributes_2.name)="category_id") AND ((post_attributes.name)="lat") AND ((post_attributes_1.name)="lng"))
HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20;

SQL fiddle 展示如何使用WHEREHAVING别名

最后一个查询导致错误,这也是您遇到的问题。

于 2012-09-21T10:43:08.213 回答