
使用 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 mingyun 2016-10-22 17:58:15 +08:00 后来怎么处理的,不排序了? |