douqinlu4217 2015-01-19 20: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 21: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.

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

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画