douqinlu4217 2015-01-19 12:53
浏览 41
已采纳

Mysql联合订单不工作[重复]

This question already has an answer here:

This is the query I am running:

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

I want the entire result set to be ordered by views but what I seem to be getting back is the subqueries ordered. There are 4 posts in total 2 are returned from the first query and 2 are returned from the second query. The posts from the first query have 8 views and 7 views. The posts from the second query have 13 views and 0 views. So the order returned should go 13, 8 7, 0 but instead I'm getting 8, 7, 13, 0

Where am I going wrong?

</div>
  • 写回答

1条回答 默认 最新

  • duai5344 2015-01-19 13:09
    关注

    Ah ok I solved it. It's because the views field isn't a numeric field so its ordering them as a string not a number! So because 13 begins with a 1 it comes after 8 and 7.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部