V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
BlueGood
V2EX  ›  MySQL

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

  •  
  •   BlueGood · 2016-10-11 11:30:27 +08:00 · 3054 次点击
    这是一个创建于 2746 天前的主题,其中的信息可能已经有所发展或是发生改变。

    使用 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 了,查了一上午都没找到原因,求指导

    第 1 条附言  ·  2016-10-11 13:24:47 +08:00
    找到原因了,原来是 price_current 这个字段设置了 UNSIGNED ,用 order by 会生成临时表,而临时表也就会同样是 UNSIGNED ,这就导致所有负值都变成 0.00 了
    1 条回复    2016-10-22 17:58:15 +08:00
    mingyun
        1
    mingyun  
       2016-10-22 17:58:15 +08:00
    后来怎么处理的,不排序了?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1542 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 17:04 · PVG 01:04 · LAX 10:04 · JFK 13:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.