Mysql bug? Sub-query contains group by results vary

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.