Mysql 使用 select as 的值 order by 出现数据错误问题

2016-10-11 11:30:27 +08:00
 BlueGood

使用 inner join 查询,如果使用 percent1 排序就会出现结果里面的 percent1 值是错误的,先看不排序的sql。

select `stock`.*, `stock1`.`price_current` as `price_current1`, `stock1`.`rank` as `rank1`, `stock`.`price_current` - `stock1`.`price_current` as `percent1` from `stock` inner join `stock` as `stock1` on `stock`.`code` = `stock1`.`code` and `stock`.`date` = '2016-10-10' and `stock1`.`date` = '2016-09-30' and `stock`.`code` = '600817'\G

结果

             id: 174
           code: 600817
           name: *ST 宏盛
price_yesterday: 18.55
    price_today: 18.61
  price_current: 18.53
        percent: -0.11
        highest: 18.67
         lowest: 18.35
          swing: 1.73
     high_limit: 19.48
      low_limit: 17.62
    value_total: 29.82
    value_trade: 28.85
           date: 2016-10-10
           rank: 9
     created_at: 2016-10-10 09:10:08
     updated_at: 2016-10-10 14:57:08
 price_current1: 18.55
          rank1: 9
       percent1: -0.02

此时percent1是-0.02,我们加上order by percent1再看

select `stock`.*, `stock1`.`price_current` as `price_current1`, `stock1`.`rank` as `rank1`, `stock`.`price_current` - `stock1`.`price_current` as `percent1` from `stock` inner join `stock` as `stock1` on `stock`.`code` = `stock1`.`code` and `stock`.`date` = '2016-10-10' and `stock1`.`date` = '2016-09-30' and `stock`.`code` = '600817' order by `percent1` desc\G

结果

             id: 174
           code: 600817
           name: *ST 宏盛
price_yesterday: 18.55
    price_today: 18.61
  price_current: 18.53
        percent: -0.11
        highest: 18.67
         lowest: 18.35
          swing: 1.73
     high_limit: 19.48
      low_limit: 17.62
    value_total: 29.82
    value_trade: 28.85
           date: 2016-10-10
           rank: 9
     created_at: 2016-10-10 09:10:08
     updated_at: 2016-10-10 14:57:08
 price_current1: 18.55
          rank1: 9
       percent1: 0.00

可以看到最后的 percent1 竟然变成 0.00 了,查了一上午都没找到原因,求指导

3141 次点击
所在节点    MySQL
1 条回复
mingyun
2016-10-22 17:58:15 +08:00
后来怎么处理的,不排序了?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/311842

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX