› 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 · Oct 11, 2016 · 3798 views
    This topic created in 3636 days ago, the information mentioned may be changed or developed.

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

    Supplement 1  ·  Oct 11, 2016
    找到原因了,原来是 price_current 这个字段设置了 UNSIGNED ,用 order by 会生成临时表,而临时表也就会同样是 UNSIGNED ,这就导致所有负值都变成 0.00 了
    1 replies  •  2016-10-22 17:58:15 +08:00
    mingyun
        1
    mingyun  
       Oct 22, 2016
    后来怎么处理的,不排序了?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2450 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 04:45 · PVG 12:45 · LAX 21:45 · JFK 00:45
    ♥ Do have faith in what you're doing.