“字段列表”中的未知列“纬度”

时间:2015-01-10 13:50:53

标签: mysql

SELECT GROUP_CONCAT(TRIM(postmeta.meta_value) SEPARATOR ",")
     , SUBSTRING_INDEX(GROUP_CONCAT(TRIM(postmeta.meta_value) SEPARATOR ","),",",1) latitude
     , SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(TRIM(postmeta.meta_value) SEPARATOR ","),",",2),",",-1) longitude
     , ( 6371 * acos( cos( radians(52.2734487789) ) * cos( radians(latitude ) ) * cos( radians(longitude ) - radians(10.5438383386) ) + sin( radians(52.2734487789) ) * sin( radians(latitude ) ) ) ) AS distance 
  FROM `wp_umkreissuche_posts` AS posts
     ,  `wp_umkreissuche_postmeta` AS postmeta 
 WHERE posts.post_type = "adresses" 
   AND posts.ID=postmeta.post_id 
   AND postmeta.meta_key="latitude" 
    OR posts.post_type="adresses" 
   AND posts.ID=postmeta.post_id 
   AND postmeta.meta_key="longitude" 
 GROUP 
    BY postmeta.post_id 
HAVING distance < 0.5 
 ORDER 
    BY postmeta.post_id
     ,  distance ASC

2 个答案:

答案 0 :(得分:1)

这是您的查询,格式更好一些,语法更清晰{/ 1}:

join

您的直接问题是SELECT GROUP_CONCAT(TRIM(postmeta.meta_value) SEPARATOR ","), SUBSTRING_INDEX(GROUP_CONCAT(TRIM(postmeta.meta_value) SEPARATOR ","),",",1) AS latitude, SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(TRIM(postmeta.meta_value) SEPARATOR ","),",",2),",",-1) AS longitude, ( 6371 * acos( cos( radians(52.2734487789) ) * cos( radians(latitude ) ) * cos( radians(longitude ) - radians(10.5438383386) ) + sin( radians(52.2734487789) ) * sin( radians(latitude ) ) ) ) AS distance FROM `wp_umkreissuche_posts` AS posts JOIN `wp_umkreissuche_postmeta` AS postmeta ON posts.ID = postmeta.post_id AND posts.post_type = 'adresses' AND (postmeta.meta_key IN ('latitude', 'longitude') ) GROUP BY postmeta.post_id HAVING distance < 0.5 ORDER BY postmeta.post_id, distance ASC latitude中定义了longitudeselect,因此无法在同一选择级别使用它们。通常的解决方案是重复表达式或使用子查询,因此别名是已知的。但是,查询的逻辑看起来并不像。我怀疑它应该是更像这样的东西:

SELECT CONCATE_WS, ',', lat.postmeta.meta_value, lng.postmeta.meta_value) ,
       lat.postmeta.meta_value AS latitude, 
       lng.meta_value AS longitude,
       . . .
FROM `wp_umkreissuche_posts` posts JOIN
     `wp_umkreissuche_postmeta` lat
     ON posts.ID = postmeta.post_id AND posts.post_type = 'adresses' AND lat.meta_key = 'latitude' JOIN
     `wp_umkreissuche_postmeta` lng
     ON posts.ID = postmeta.post_id AND posts.post_type = 'adresses' AND lng.meta_key = 'longitude' 
HAVING distance < 0.5
ORDER BY postmeta.post_id, distance ASC;

注意:可以在MySQL中使用having而不使用group by。然后它表现得像where,但它允许列别名。

答案 1 :(得分:0)

除非使用子查询或联接,否则不能在同一查询的其他字段中使用别名。

使用

(SUBSTRING_INDEX(GROUP_CONCAT(TRIM(postmeta.meta_value) SEPARATOR ","),",",1)

而不是latitude

尝试这种方式:

SELECT GROUP_CONCAT(TRIM(postmeta.meta_value) SEPARATOR ","),
SUBSTRING_INDEX(GROUP_CONCAT(TRIM(postmeta.meta_value) SEPARATOR ","),",",1) AS latitude,
SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(TRIM(postmeta.meta_value) SEPARATOR ","),",",2),",",-1) AS longitude,
( 6371 * acos( cos( radians(52.2734487789) ) * cos( radians(latitude ) ) * cos( radians(longitude ) - radians(10.5438383386) ) + sin( radians(52.2734487789) ) * sin( radians((SUBSTRING_INDEX(GROUP_CONCAT(TRIM(postmeta.meta_value) SEPARATOR ","),",",1)) ) ) ) AS distance 
FROM wp_umkreissuche_posts AS posts, wp_umkreissuche_postmeta AS postmeta 
WHERE posts.post_type="adresses" AND posts.ID=postmeta.post_id AND postmeta.meta_key="latitude" OR posts.post_type="adresses" AND posts.ID=postmeta.post_id AND postmeta.meta_key="longitude" 
GROUP BY postmeta.post_id HAVING distance < 0.5 
ORDER BY postmeta.post_id, distance ASC