这是我正在运行的查询:
SELECT * FROM (
(SELECT wp_posts.*, wp_postmeta.meta_value AS views FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND ( wp_postmeta.meta_key = 'views' ) GROUP BY wp_posts.ID)
UNION
( SELECT wp_2_posts.*, wp_2_postmeta.meta_value AS views FROM wp_2_posts INNER JOIN wp_2_postmeta ON ( wp_2_posts.ID = wp_2_postmeta.post_id ) WHERE 1=1 AND wp_2_posts.post_type = 'post' AND (wp_2_posts.post_status = 'publish' OR wp_2_posts.post_status = 'private') AND ( wp_2_postmeta.meta_key = 'views' ) GROUP BY wp_2_posts.ID)
) AS posts ORDER BY views DESC LIMIT 0, 10
我希望整个结果集按视图排序,但我似乎要回来的是订购的子查询。从第一个查询返回共有4个帖子,从第二个查询返回2个帖子。第一个查询的帖子有8个视图和7个视图。第二个查询的帖子有13个视图和0个视图。所以返回的订单应该是13,8 7,0但是我得到的是8,7,13,0
我哪里错了?
答案 0 :(得分:0)
好的,我解决了。这是因为视图字段不是数字字段所以它将它们排序为字符串而不是数字!所以因为13从1开始,它在8和7之后出现。