func_group.test
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· TEST 代码 · 共 865 行 · 第 1/2 页
TEST
865 行
insert into t1 values (1),(2);prepare stmt1 from 'SELECT COUNT(*) FROM t1';execute stmt1;execute stmt1;execute stmt1;deallocate prepare stmt1;drop table t1;create table t1 (a int, primary key(a));insert into t1 values (1),(2);prepare stmt1 from 'SELECT max(a) FROM t1';execute stmt1;execute stmt1;execute stmt1;deallocate prepare stmt1;drop table t1;## Bug #5406 min/max optimization for empty set#CREATE TABLE t1 (a int primary key);INSERT INTO t1 VALUES (1),(2),(3),(4);SELECT MAX(a) FROM t1 WHERE a > 5;SELECT MIN(a) FROM t1 WHERE a < 0;DROP TABLE t1;## Bug #5555 GROUP BY enum_field" returns incorrect results# CREATE TABLE t1 ( id int(10) unsigned NOT NULL auto_increment, val enum('one','two','three') NOT NULL default 'one', PRIMARY KEY (id)) ENGINE=MyISAM DEFAULT CHARSET=utf8; INSERT INTO t1 VALUES(1,'one'),(2,'two'),(3,'three'),(4,'one'),(5,'two'); select val, count(*) from t1 group by val;drop table t1;CREATE TABLE t1 ( id int(10) unsigned NOT NULL auto_increment, val set('one','two','three') NOT NULL default 'one', PRIMARY KEY (id)) ENGINE=MyISAM DEFAULT CHARSET=utf8;INSERT INTO t1 VALUES(1,'one'),(2,'two'),(3,'three'),(4,'one'),(5,'two');select val, count(*) from t1 group by val;drop table t1;## Bug #5615: type of aggregate function column wrong when using group by#create table t1(a int, b datetime);insert into t1 values (1, NOW()), (2, NOW());create table t2 select MAX(b) from t1 group by a;show create table t2;drop table t1, t2;## Bug 7833: Wrong datatype of aggregate column is returned#create table t1(f1 datetime);insert into t1 values (now());create table t2 select f2 from (select max(now()) f2 from t1) a;show columns from t2;drop table t2;create table t2 select f2 from (select now() f2 from t1) a;show columns from t2;drop table t2, t1;## Bug 8893: wrong result for min/max optimization with 2 indexes#CREATE TABLE t1( id int PRIMARY KEY, a int, b int, INDEX i_b_id(a,b,id), INDEX i_id(a,id));INSERT INTO t1 VALUES (1,1,4), (2,2,1), (3,1,3), (4,2,1), (5,1,1);SELECT MAX(id) FROM t1 WHERE id < 3 AND a=2 AND b=6;DROP TABLE t1;# change the order of the last two index definitionsCREATE TABLE t1( id int PRIMARY KEY, a int, b int, INDEX i_id(a,id), INDEX i_b_id(a,b,id));INSERT INTO t1 VALUES (1,1,4), (2,2,1), (3,1,3), (4,2,1), (5,1,1);SELECT MAX(id) FROM t1 WHERE id < 3 AND a=2 AND b=6;DROP TABLE t1;## Bug #18206: min/max optimization cannot be applied to partial index#CREATE TABLE t1 (id int PRIMARY KEY, b char(3), INDEX(b));INSERT INTO t1 VALUES (1,'xx'), (2,'aa');SELECT * FROM t1;SELECT MAX(b) FROM t1 WHERE b < 'ppppp';SHOW WARNINGS;SELECT MAX(b) FROM t1 WHERE b < 'pp';DROP TABLE t1;CREATE TABLE t1 (id int PRIMARY KEY, b char(16), INDEX(b(4)));INSERT INTO t1 VALUES (1, 'xxxxbbbb'), (2, 'xxxxaaaa');SELECT MAX(b) FROM t1;EXPLAIN SELECT MAX(b) FROM t1;DROP TABLE t1;CREATE TABLE t1 (id int , b varchar(512), INDEX(b(250))) COLLATE latin1_bin;INSERT INTO t1 VALUES (1,CONCAT(REPEAT('_', 250), "qq")), (1,CONCAT(REPEAT('_', 250), "zz")), (1,CONCAT(REPEAT('_', 250), "aa")), (1,CONCAT(REPEAT('_', 250), "ff"));SELECT MAX(b) FROM t1;EXPLAIN SELECT MAX(b) FROM t1;DROP TABLE t1;## Bug #16792 query with subselect, join, and group not returning proper values#CREATE TABLE t1 (a INT, b INT);INSERT INTO t1 VALUES (1,1),(1,2),(2,3);SELECT (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;SELECT (SELECT COUNT(DISTINCT 12)) FROM t1 GROUP BY t1.a;# an attempt to test all aggregate function with no table.SELECT AVG(2), BIT_AND(2), BIT_OR(2), BIT_XOR(2), COUNT(*), COUNT(12), COUNT(DISTINCT 12), MIN(2),MAX(2),STD(2), VARIANCE(2),SUM(2), GROUP_CONCAT(2),GROUP_CONCAT(DISTINCT 2);DROP TABLE t1;# End of 4.1 tests## decimal-related tests#create table t2 (ff double);insert into t2 values (2.2);select cast(sum(distinct ff) as decimal(5,2)) from t2;select cast(sum(distinct ff) as signed) from t2;select cast(variance(ff) as decimal(10,3)) from t2;select cast(min(ff) as decimal(5,2)) from t2;create table t1 (df decimal(5,1));insert into t1 values(1.1);insert into t1 values(2.2);select cast(sum(distinct df) as signed) from t1;select cast(min(df) as signed) from t1;select 1e8 * sum(distinct df) from t1;select 1e8 * min(df) from t1;create table t3 (ifl int);insert into t3 values(1), (2);select cast(min(ifl) as decimal(5,2)) from t3;drop table t1, t2, t3;## BUG#3190, WL#1639: Standard Deviation STDDEV - 2 different calculations#CREATE TABLE t1 (id int(11),value1 float(10,2));INSERT INTO t1 VALUES (1,0.00),(1,1.00), (1,2.00), (2,10.00), (2,11.00), (2,12.00), (2,13.00);select id, stddev_pop(value1), var_pop(value1), stddev_samp(value1), var_samp(value1) from t1 group by id;DROP TABLE t1;## BUG#8464 decimal AVG returns incorrect result#CREATE TABLE t1 (col1 decimal(16,12));INSERT INTO t1 VALUES (-5.00000000001),(-5.00000000002),(-5.00000000003),(-5.00000000000),(-5.00000000001),(-5.00000000002);insert into t1 select * from t1;select col1,count(col1),sum(col1),avg(col1) from t1 group by col1;DROP TABLE t1;## BUG#8465 decimal MIN and MAX return incorrect result#create table t1 (col1 decimal(16,12));insert into t1 values (-5.00000000001);insert into t1 values (-5.00000000001);select col1,sum(col1),max(col1),min(col1) from t1 group by col1;delete from t1;insert into t1 values (5.00000000001);insert into t1 values (5.00000000001);select col1,sum(col1),max(col1),min(col1) from t1 group by col1;DROP TABLE t1;## Test that new VARCHAR correctly works with COUNT(DISTINCT)#CREATE TABLE t1 (a VARCHAR(400));INSERT INTO t1 (a) VALUES ("A"), ("a"), ("a "), ("a "), ("B"), ("b"), ("b "), ("b ");SELECT COUNT(DISTINCT a) FROM t1;DROP TABLE t1;## Test for buf #9210: GROUP BY with expression if a decimal type#CREATE TABLE t1 (a int, b int, c int);INSERT INTO t1 (a, b, c) VALUES (1,1,1), (1,1,2), (1,1,3), (1,2,1), (1,2,2), (1,2,3), (1,3,1), (1,3,2), (1,3,3), (2,1,1), (2,1,2), (2,1,3), (2,2,1), (2,2,2), (2,2,3), (2,3,1), (2,3,2), (2,3,3), (3,1,1), (3,1,2), (3,1,3), (3,2,1), (3,2,2), (3,2,3), (3,3,1), (3,3,2), (3,3,3);SELECT b/c as v, a FROM t1 ORDER BY v;SELECT b/c as v, SUM(a) FROM t1 GROUP BY v;SELECT SUM(a) FROM t1 GROUP BY b/c;DROP TABLE t1;set div_precision_increment= @sav_dpi;## Bug #20868: Client connection is broken on SQL query error#CREATE TABLE t1 (a INT PRIMARY KEY, b INT);INSERT INTO t1 VALUES (1,1), (2,2);CREATE TABLE t2 (a INT PRIMARY KEY, b INT);INSERT INTO t2 VALUES (1,1), (3,3);SELECT SQL_NO_CACHE (SELECT SUM(c.a) FROM t1 ttt, t2 ccc WHERE ttt.a = ccc.b AND ttt.a = t.a GROUP BY ttt.a) AS minid FROM t1 t, t2 c WHERE t.a = c.b;DROP TABLE t1,t2;## Bug #10966: Variance functions return wrong data type#create table t1 select variance(0); show create table t1; drop table t1; create table t1 select stddev(0);show create table t1;drop table t1; ## Bug#22555: STDDEV yields positive result for groups with only one row#create table bug22555 (i smallint primary key auto_increment, s1 smallint, s2 smallint, e decimal(30,10), o double);insert into bug22555 (s1, s2, e, o) values (53, 78, 11.4276528, 6.828112), (17, 78, 5.916793, 1.8502951), (18, 76, 2.679231, 9.17975591), (31, 62, 6.07831, 0.1), (19, 41, 5.37463, 15.1), (83, 73, 14.567426, 7.959222), (92, 53, 6.10151, 13.1856852), (7, 12, 13.92272, 3.442007), (92, 35, 11.95358909, 6.01376678), (38, 84, 2.572, 7.904571);select std(s1/s2) from bug22555 group by i;select std(e) from bug22555 group by i;select std(o) from bug22555 group by i;drop table bug22555;create table bug22555 (i smallint, s1 smallint, s2 smallint, o1 double, o2 double, e1 decimal, e2 decimal);insert into bug22555 values (1,53,78,53,78,53,78),(2,17,78,17,78,17,78),(3,18,76,18,76,18,76);select i, count(*) from bug22555 group by i;select std(s1/s2) from bug22555 where i=1;select std(s1/s2) from bug22555 where i=2;select std(s1/s2) from bug22555 where i=3;select std(s1/s2) from bug22555 where i=1 group by i;select std(s1/s2) from bug22555 where i=2 group by i;select std(s1/s2) from bug22555 where i=3 group by i;select std(s1/s2) from bug22555 group by i order by i;select i, count(*), std(o1/o2) from bug22555 group by i order by i;select i, count(*), std(e1/e2) from bug22555 group by i order by i;set @saved_div_precision_increment=@@div_precision_increment;set div_precision_increment=19;select i, count(*), variance(s1/s2) from bug22555 group by i order by i;select i, count(*), variance(o1/o2) from bug22555 group by i order by i;select i, count(*), variance(e1/e2) from bug22555 group by i order by i;select i, count(*), std(s1/s2) from bug22555 group by i order by i;select i, count(*), std(o1/o2) from bug22555 group by i order by i;select i, count(*), std(e1/e2) from bug22555 group by i order by i;set div_precision_increment=20;select i, count(*), variance(s1/s2) from bug22555 group by i order by i;select i, count(*), variance(o1/o2) from bug22555 group by i order by i;select i, count(*), variance(e1/e2) from bug22555 group by i order by i;select i, count(*), std(s1/s2) from bug22555 group by i order by i;select i, count(*), std(o1/o2) from bug22555 group by i order by i;select i, count(*), std(e1/e2) from bug22555 group by i order by i;set @@div_precision_increment=@saved_div_precision_increment;insert into bug22555 values (1,53,78,53,78,53,78),(2,17,78,17,78,17,78),(3,18,76,18,76,18,76);insert into bug22555 values (1,53,78,53,78,53,78),(2,17,78,17,78,17,78),(3,18,76,18,76,18,76);insert into bug22555 values (1,53,78,53,78,53,78),(2,17,78,17,78,17,78),(3,18,76,18,76,18,76);select i, count(*), std(s1/s2) from bug22555 group by i order by i;select i, count(*), round(std(o1/o2), 16) from bug22555 group by i order by i;select i, count(*), std(e1/e2) from bug22555 group by i order by i;select std(s1/s2) from bug22555;select std(o1/o2) from bug22555;select std(e1/e2) from bug22555;set @saved_div_precision_increment=@@div_precision_increment;set div_precision_increment=19;select i, count(*), std(s1/s2) from bug22555 group by i order by i;select i, count(*), round(std(o1/o2), 16) from bug22555 group by i order by i;select i, count(*), std(e1/e2) from bug22555 group by i order by i;select round(std(s1/s2), 17) from bug22555;select std(o1/o2) from bug22555;select round(std(e1/e2), 17) from bug22555;set div_precision_increment=20;select i, count(*), std(s1/s2) from bug22555 group by i order by i;select i, count(*), round(std(o1/o2), 16) from bug22555 group by i order by i;select i, count(*), std(e1/e2) from bug22555 group by i order by i;select round(std(s1/s2), 17) from bug22555;select std(o1/o2) from bug22555;select round(std(e1/e2), 17) from bug22555;set @@div_precision_increment=@saved_div_precision_increment;drop table bug22555;create table bug22555 (s smallint, o double, e decimal);insert into bug22555 values (1,1,1),(2,2,2),(3,3,3),(6,6,6),(7,7,7);select var_samp(s), var_pop(s) from bug22555;select var_samp(o), var_pop(o) from bug22555;select var_samp(e), var_pop(e) from bug22555;drop table bug22555;create table bug22555 (s smallint, o double, e decimal);insert into bug22555 values (null,null,null),(null,null,null);select var_samp(s) as 'null', var_pop(s) as 'null' from bug22555;select var_samp(o) as 'null', var_pop(o) as 'null' from bug22555;select var_samp(e) as 'null', var_pop(e) as 'null' from bug22555;insert into bug22555 values (1,1,1);select var_samp(s) as 'null', var_pop(s) as '0' from bug22555;select var_samp(o) as 'null', var_pop(o) as '0' from bug22555;select var_samp(e) as 'null', var_pop(e) as '0' from bug22555;insert into bug22555 values (2,2,2);select var_samp(s) as '0.5', var_pop(s) as '0.25' from bug22555;select var_samp(o) as '0.5', var_pop(o) as '0.25' from bug22555;select var_samp(e) as '0.5', var_pop(e) as '0.25' from bug22555;drop table bug22555;## Bug #21976: Unnecessary warning with count(decimal)#create table t1 (a decimal(20));insert into t1 values (12345678901234567890);select count(a) from t1;select count(distinct a) from t1;drop table t1;## Bug #23184: SELECT causes server crash# CREATE TABLE t1 (a INT, b INT);INSERT INTO t1 VALUES (1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8);INSERT INTO t1 SELECT a, b+8 FROM t1;INSERT INTO t1 SELECT a, b+16 FROM t1;INSERT INTO t1 SELECT a, b+32 FROM t1;INSERT INTO t1 SELECT a, b+64 FROM t1;INSERT INTO t1 SELECT a, b+128 FROM t1;INSERT INTO t1 SELECT a, b+256 FROM t1;INSERT INTO t1 SELECT a, b+512 FROM t1;INSERT INTO t1 SELECT a, b+1024 FROM t1;INSERT INTO t1 SELECT a, b+2048 FROM t1;INSERT INTO t1 SELECT a, b+4096 FROM t1;INSERT INTO t1 SELECT a, b+8192 FROM t1;INSERT INTO t1 SELECT a, b+16384 FROM t1;INSERT INTO t1 SELECT a, b+32768 FROM t1;SELECT a,COUNT(DISTINCT b) AS cnt FROM t1 GROUP BY a HAVING cnt > 50;SELECT a,SUM(DISTINCT b) AS sumation FROM t1 GROUP BY a HAVING sumation > 50;SELECT a,AVG(DISTINCT b) AS average FROM t1 GROUP BY a HAVING average > 50;DROP TABLE t1;## Bug #27573: MIN() on an indexed column which is always NULL sets _other_ # results to NULL#CREATE TABLE t1 ( a INT, b INT, KEY(a) );INSERT INTO t1 VALUES (NULL, 1), (NULL, 2);EXPLAIN SELECT MIN(a), MIN(b) FROM t1;SELECT MIN(a), MIN(b) FROM t1;CREATE TABLE t2( a INT, b INT, c INT, KEY(a, b) );INSERT INTO t2 ( a, b, c ) VALUES ( 1, NULL, 2 ), ( 1, 3, 4 ), ( 1, 4, 4 );EXPLAIN SELECT MIN(b), MIN(c) FROM t2 WHERE a = 1;SELECT MIN(b), MIN(c) FROM t2 WHERE a = 1;CREATE TABLE t3 (a INT, b INT, c int, KEY(a, b));INSERT INTO t3 VALUES (1, NULL, 1), (2, NULL, 2), (2, NULL, 2), (3, NULL, 3);EXPLAIN SELECT MIN(a), MIN(b) FROM t3 where a = 2;SELECT MIN(a), MIN(b) FROM t3 where a = 2;CREATE TABLE t4 (a INT, b INT, c int, KEY(a, b));INSERT INTO t4 VALUES (1, 1, 1), (2, NULL, 2), (2, NULL, 2), (3, 1, 3);EXPLAIN SELECT MIN(a), MIN(b) FROM t4 where a = 2;SELECT MIN(a), MIN(b) FROM t4 where a = 2;SELECT MIN(b), min(c) FROM t4 where a = 2;CREATE TABLE t5( a INT, b INT, KEY( a, b) ); INSERT INTO t5 VALUES( 1, 1 ), ( 1, 2 );EXPLAIN SELECT MIN(a), MIN(b) FROM t5 WHERE a = 1;SELECT MIN(a), MIN(b) FROM t5 WHERE a = 1;SELECT MIN(a), MIN(b) FROM t5 WHERE a = 1 and b > 1;DROP TABLE t1, t2, t3, t4, t5;###--echo End of 5.0 tests
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?