Mysql 的bug?子查詢包含group by結果各異

來源:互聯網
上載者:User

標籤:

三張表,欄位和資料如下,簡單起見都是整形:

zzz_repair:==》維修訂單表, 列依次為:訂單號, 省份

 

zzz_repair_goods:==》維修商品表, 列依次為 訂單號、商品名稱、商品數量

zzz_sell: ==》銷售表,列依次為銷售省份、銷售商品、銷售數量

 

sql: =>求平均每個省的返修比率

select

--省份分組

m.province,

--返修數 / 總銷售數

sum(num)/(select sum(num) from zzz_sell z where z.province=m.province) as rate

from 

--連表

(select a.*, b.num from zzz_repair a, zzz_repair_goods b where a.sn=b.sn) m

--分組求和

group by m.province

 最後的結果如下,可見對於省份2,計算結果0.5是正確的才對,為何MySQL 5.5 顯示為null

MySQL 5.5

MySQL 5.6

 

 附上所有指令碼:

drop table IF EXISTS zzz_repair;
drop table IF EXISTS zzz_repair_goods;
drop table IF EXISTS zzz_sell;

CREATE TABLE IF NOT EXISTS `zzz_repair` (
`sn` int(11) NOT NULL,
`province` int(11) NOT NULL
) ;
INSERT INTO `zzz_repair` (`sn`, `province`) VALUES
(1, 2),
(3, 3),
(5, 5),
(8, 8);


CREATE TABLE IF NOT EXISTS `zzz_repair_goods` (
`sn` int(11) NOT NULL,
`name` int(11) NOT NULL,
`num` int(11) NOT NULL
) ;

INSERT INTO `zzz_repair_goods` (`sn`, `name`, `num`) VALUES
(1, 2, 10),
(3, 4, 10),
(5, 6, 10);

CREATE TABLE IF NOT EXISTS `zzz_sell` (
`sn` int(11) NOT NULL,
`province` int(11) NOT NULL,
`num` int(11) NOT NULL
) ;
INSERT INTO `zzz_sell` (`sn`, `province`, `num`) VALUES
(1, 2, 20),
(3, 4, 20),
(5, 6, 20),
(9, 9, 20);

 

Mysql 的bug?子查詢包含group by結果各異

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.