Three tables, fields and data are as follows, and for simplicity all is shaping:
zzz_repair:== Repair Order Form: order number, Province
zzz_repair_goods:== "Maintenance commodity table, order number, product name, number of goods
Zzz_sell: = = "Sales table, in order of sales provinces, sales of goods, sales quantity
sql: = To find the average repair ratio per province
Select
--Province grouping
M.province,
--Number of rework/total sales
SUM (num)/(select SUM (num) from Zzz_sell Z where z.province=m.province) as rate
From
--even table
(select A.*, B.num from Zzz_repair A, zzz_repair_goods b where a.sn=b.sn) m
--Group summation
GROUP BY M.province
The final result is as follows, for the province 2, the result 0.5 is correct, why MySQL 5.5 is shown as null
MySQL 5.5
MySQL 5.6
Attach All scripts:
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 (one) is not NULL,
' Province ' int (one) 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 (one) is not NULL,
' Name ' int (one) is not NULL,
' num ' int (one) 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 (one) is not NULL,
' Province ' int (one) is not NULL,
' num ' int (one) not NULL
) ;
INSERT into ' Zzz_sell ' (' sn ', ' Province ', ' num ') VALUES
(1, 2, 20),
(3, 4, 20),
(5, 6, 20),
(9, 9, 20);
Mysql bug? Sub-query contains group by results vary