我正在尝试一个mysql查询来通过距离对帖子进行排序让我们说在半径250英里范围内,一切正常,但我需要以特色帖子始终位于顶部的方式订购帖子。 这是我的mysql查询(我在wordpress上做的)。
SELECT SQL_CALC_FOUND_ROWS wp_posts.*, ( 3959 * acos( cos( radians(40.140711) ) * cos( radians( latitude.meta_value ) ) * cos( radians( longitude.meta_value ) - radians(-74.20619299999998) ) + sin( radians(40.140711) ) * sin( radians( latitude.meta_value ) ) ) ) AS distance ,
latitude.meta_value AS latitude ,
longitude.meta_value AS longitude
FROM wp_posts
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS latitude ON wp_posts.ID = latitude.post_id
INNER JOIN wp_postmeta AS longitude ON wp_posts.ID = longitude.post_id
WHERE 1=1
AND wp_posts.post_type = 'event'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'recurring')
AND (wp_postmeta.meta_key = 'st_date' )
AND latitude.meta_key="geo_latitude"
AND longitude.meta_key="geo_longitude"
AND wp_posts.ID in (SELECT tr.object_id FROM wp_term_relationships tr JOIN wp_term_taxonomy t on t.term_taxonomy_id=tr.term_taxonomy_id WHERE t.term_id IN ('3','4','5','6','7','8','9','10'))
AND (wp_posts.ID IN (SELECT wp_postmeta.post_id FROM wp_postmeta WHERE wp_postmeta.meta_key='st_date'
AND date_format(wp_postmeta.meta_value,'%Y-%m-%d') >'2013-09-23 14:35:58'))
GROUP BY wp_posts.ID HAVING distance <= 250
ORDER BY (SELECT wp_postmeta.meta_value FROM wp_postmeta, wp_posts AS p where wp_postmeta.post_id=p.ID AND wp_postmeta.meta_key = 'featured_h') ASC, distance ASC LIMIT 0, 10
问题在于ORDER BY,它给出了错误“Subquery返回超过1行”
如果我删除ORDER BY(从wp_postmeta中选择wp_postmeta.meta_value,wp_posts为p,其中wp_postmeta.post_id = p.ID和wp_postmeta.meta_key ='featured_h'
它工作正常,但它不会显示最好的帖子,无论如何修复它。
答案 0 :(得分:0)
在结果集中放置一列显示featured
状态。你相信,你必须再次加入wp_postmeta来实现这一目标。
然后使用
ORDER BY status, distance
或者
ORDER BY STATUS='featured', distance
这将使您的精选帖子首先按距离排序,然后是其余部分。
(将子查询放在ORDER BY
子句中非常奇怪。)
答案 1 :(得分:0)
试试这个,我从子查询中选择了custom_order_by
列并按此顺序
SELECT SQL_CALC_FOUND_ROWS wp_posts.*, ( 3959 * acos( cos( radians(40.140711) ) * cos( radians( latitude.meta_value ) ) * cos( radians( longitude.meta_value ) - radians(-74.20619299999998) ) + sin( radians(40.140711) ) * sin( radians( latitude.meta_value ) ) ) ) AS distance ,
latitude.meta_value AS latitude ,
longitude.meta_value AS longitude,
(SELECT wp.meta_value FROM wp_postmeta wp where wp.post_id=wpm.post_id AND wp.meta_key = 'featured_h') AS custom_order_by
FROM wp_posts
INNER JOIN wp_postmeta AS wpm ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS latitude ON wp_posts.ID = latitude.post_id
INNER JOIN wp_postmeta AS longitude ON wp_posts.ID = longitude.post_id
WHERE 1=1
AND wp_posts.post_type = 'event'
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'recurring')
AND (wpm.meta_key = 'st_date' )
AND latitude.meta_key="geo_latitude"
AND longitude.meta_key="geo_longitude"
AND wp_posts.ID in (SELECT tr.object_id FROM wp_term_relationships tr JOIN wp_term_taxonomy t on t.term_taxonomy_id=tr.term_taxonomy_id WHERE t.term_id IN ('3','4','5','6','7','8','9','10'))
AND (wp_posts.ID IN (SELECT wp_postmeta.post_id FROM wp_postmeta WHERE wp_postmeta.meta_key='st_date'
AND date_format(wp_postmeta.meta_value,'%Y-%m-%d') >'2013-09-23 14:35:58'))
GROUP BY wp_posts.ID HAVING distance <= 250
ORDER BY custom_order_by ASC, distance ASC LIMIT 0, 10