# # Bug with distinct and INSERT INTO # Bug with group by and not used fields # --source include/default_optimizer_switch.inc --source include/have_sequence.inc --disable_warnings drop table if exists t1,t2,t3; --enable_warnings CREATE TABLE t1 (id int,facility char(20)); CREATE TABLE t2 (facility char(20)); INSERT INTO t1 VALUES (NULL,NULL); INSERT INTO t1 VALUES (-1,''); INSERT INTO t1 VALUES (0,''); INSERT INTO t1 VALUES (1,'/L'); INSERT INTO t1 VALUES (2,'A01'); INSERT INTO t1 VALUES (3,'ANC'); INSERT INTO t1 VALUES (4,'F01'); INSERT INTO t1 VALUES (5,'FBX'); INSERT INTO t1 VALUES (6,'MT'); INSERT INTO t1 VALUES (7,'P'); INSERT INTO t1 VALUES (8,'RV'); INSERT INTO t1 VALUES (9,'SRV'); INSERT INTO t1 VALUES (10,'VMT'); INSERT INTO t2 SELECT DISTINCT FACILITY FROM t1; select id from t1 group by id; select * from t1 order by id; select id-5,facility from t1 order by "id-5"; select id,concat(facility) from t1 group by id ; select id+0 as a,max(id),concat(facility) as b from t1 group by a order by b desc,a; select id >= 0 and id <= 5 as grp,count(*) from t1 group by grp; SELECT DISTINCT FACILITY FROM t1; SELECT FACILITY FROM t2; SELECT count(*) from t1,t2 where t1.facility=t2.facility; select count(facility) from t1; select count(*) from t1; select count(*) from t1 where facility IS NULL; select count(*) from t1 where facility = NULL; select count(*) from t1 where facility IS NOT NULL; select count(*) from t1 where id IS NULL; select count(*) from t1 where id IS NOT NULL; drop table t1,t2; # # Problem with distinct without results # CREATE TABLE t1 (UserId int(11) DEFAULT '0' NOT NULL); INSERT INTO t1 VALUES (20); INSERT INTO t1 VALUES (27); SELECT UserId FROM t1 WHERE Userid=22; SELECT UserId FROM t1 WHERE UserId=22 group by Userid; SELECT DISTINCT UserId FROM t1 WHERE UserId=22 group by Userid; SELECT DISTINCT UserId FROM t1 WHERE UserId=22; drop table t1; # # Test of distinct # CREATE TABLE t1 (a int(10) unsigned not null primary key,b int(10) unsigned); INSERT INTO t1 VALUES (1,1),(2,1),(3,1),(4,1); CREATE TABLE t2 (a int(10) unsigned not null, key (A)); INSERT INTO t2 VALUES (1),(2); CREATE TABLE t3 (a int(10) unsigned, key(A), b text); INSERT INTO t3 VALUES (1,'1'),(2,'2'); SELECT DISTINCT t3.b FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a; INSERT INTO t2 values (1),(2),(3); INSERT INTO t3 VALUES (1,'1'),(2,'2'),(1,'1'),(2,'2'); explain SELECT distinct t3.a FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a; SELECT distinct t3.a FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a; # Create a lot of data into t3; create temporary table t4 select * from t3; insert into t3 select * from t4; insert into t4 select * from t3; insert into t3 select * from t4; insert into t4 select * from t3; insert into t3 select * from t4; insert into t4 select * from t3; insert into t3 select * from t4; explain select distinct t1.a from t1,t3 where t1.a=t3.a; #flush status; select distinct t1.a from t1,t3 where t1.a=t3.a; #show status like 'Handler%'; #flush status; select distinct 1 from t1,t3 where t1.a=t3.a; #show status like 'Handler%'; explain SELECT distinct t1.a from t1; explain SELECT distinct t1.a from t1 order by a desc; explain SELECT t1.a from t1 group by a order by a desc; explain SELECT distinct t1.a from t1 order by a desc limit 1; explain SELECT distinct a from t3 order by a desc limit 2; explain SELECT distinct a,b from t3 order by a+1; explain SELECT distinct a,b from t3 order by a limit 2; explain SELECT a,b from t3 group by a,b order by a+1; drop table t1,t2,t3,t4; CREATE TABLE t1 (name varchar(255)); INSERT INTO t1 VALUES ('aa'),('ab'),('ac'),('ad'),('ae'); SELECT DISTINCT * FROM t1 LIMIT 2; SELECT DISTINCT name FROM t1 LIMIT 2; SELECT DISTINCT 1 FROM t1 LIMIT 2; drop table t1; CREATE TABLE t1 ( ID int(11) NOT NULL auto_increment, NAME varchar(75) DEFAULT '' NOT NULL, LINK_ID int(11) DEFAULT '0' NOT NULL, PRIMARY KEY (ID), KEY NAME (NAME), KEY LINK_ID (LINK_ID) ); INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (1,'Mike',0),(2,'Jack',0),(3,'Bill',0); CREATE TABLE t2 ( ID int(11) NOT NULL auto_increment, NAME varchar(150) DEFAULT '' NOT NULL, PRIMARY KEY (ID), KEY NAME (NAME) ); SELECT DISTINCT t2.id AS key_link_id, t2.name AS link FROM t1 LEFT JOIN t2 ON t1.link_id=t2.id GROUP BY t1.id ORDER BY link; drop table t1,t2; # # Problem with table dependencies # create table t1 ( id int not null, name tinytext not null, unique (id) ); create table t2 ( id int not null, idx int not null, unique (id, idx) ); create table t3 ( id int not null, idx int not null, unique (id, idx) ); insert into t1 values (1,'yes'), (2,'no'); insert into t2 values (1,1); insert into t3 values (1,1); set @save_join_cache_level=@@join_cache_level; set join_cache_level=1; EXPLAIN SELECT DISTINCT t1.id from t1 straight_join t2 straight_join t3 straight_join t1 as j_lj_t2 left join t2 as t2_lj on j_lj_t2.id=t2_lj.id straight_join t1 as j_lj_t3 left join t3 as t3_lj on j_lj_t3.id=t3_lj.id WHERE ((t1.id=j_lj_t2.id AND t2_lj.id IS NULL) OR (t1.id=t2.id AND t2.idx=2)) AND ((t1.id=j_lj_t3.id AND t3_lj.id IS NULL) OR (t1.id=t3.id AND t3.idx=2)); SELECT DISTINCT t1.id from t1 straight_join t2 straight_join t3 straight_join t1 as j_lj_t2 left join t2 as t2_lj on j_lj_t2.id=t2_lj.id straight_join t1 as j_lj_t3 left join t3 as t3_lj on j_lj_t3.id=t3_lj.id WHERE ((t1.id=j_lj_t2.id AND t2_lj.id IS NULL) OR (t1.id=t2.id AND t2.idx=2)) AND ((t1.id=j_lj_t3.id AND t3_lj.id IS NULL) OR (t1.id=t3.id AND t3.idx=2)); drop table t1,t2,t3; set join_cache_level=@save_join_cache_level; # # Test using DISTINCT on a function that contains a group function # This also test the case when one doesn't use all fields in GROUP BY. # create table t1 (a int not null, b int not null, t time); insert into t1 values (1,1,"00:06:15"),(1,2,"00:06:15"),(1,2,"00:30:15"),(1,3,"00:06:15"),(1,3,"00:30:15"); select a,sec_to_time(sum(time_to_sec(t))) from t1 group by a,b; select distinct a,sec_to_time(sum(time_to_sec(t))) from t1 group by a,b; create table t2 (a int not null primary key, b int); insert into t2 values (1,1),(2,2),(3,3); select t1.a,sec_to_time(sum(time_to_sec(t))) from t1 left join t2 on (t1.b=t2.a) group by t1.a,t2.b; select distinct t1.a,sec_to_time(sum(time_to_sec(t))) from t1 left join t2 on (t1.b=t2.a) group by t1.a,t2.b; drop table t1,t2; # # Test problem with DISTINCT and HAVING # create table t1 (a int not null,b char(5), c text); insert into t1 (a) values (1),(2),(3),(4),(1),(2),(3),(4); select distinct a from t1 group by b,a having a > 2 order by a desc; select distinct a,c from t1 group by b,c,a having a > 2 order by a desc; drop table t1; # # Test problem with DISTINCT and ORDER BY DESC # create table t1 (a char(1), key(a)) engine=myisam; insert into t1 values('1'),('1'); select * from t1 where a >= '1'; select distinct a from t1 order by a desc; select distinct a from t1 where a >= '1' order by a desc; drop table t1; # # Test when using a not previously used column in ORDER BY # CREATE TABLE t1 (email varchar(50), infoID BIGINT, dateentered DATETIME); CREATE TABLE t2 (infoID BIGINT, shipcode varchar(10)); INSERT INTO t1 (email, infoID, dateentered) VALUES ('test1@testdomain.com', 1, '2002-07-30 22:56:38'), ('test1@testdomain.com', 1, '2002-07-27 22:58:16'), ('test2@testdomain.com', 1, '2002-06-19 15:22:19'), ('test2@testdomain.com', 2, '2002-06-18 14:23:47'), ('test3@testdomain.com', 1, '2002-05-19 22:17:32'); INSERT INTO t2(infoID, shipcode) VALUES (1, 'Z001'), (2, 'R002'); SELECT DISTINCTROW email, shipcode FROM t1, t2 WHERE t1.infoID=t2.infoID; SELECT DISTINCTROW email FROM t1 ORDER BY dateentered DESC; SELECT DISTINCTROW email, shipcode FROM t1, t2 WHERE t1.infoID=t2.infoID ORDER BY dateentered DESC; drop table t1,t2; # # test with table.* in DISTINCT # CREATE TABLE t1 (privatemessageid int(10) unsigned NOT NULL auto_increment, folderid smallint(6) NOT NULL default '0', userid int(10) unsigned NOT NULL default '0', touserid int(10) unsigned NOT NULL default '0', fromuserid int(10) unsigned NOT NULL default '0', title varchar(250) NOT NULL default '', message mediumtext NOT NULL, dateline int(10) unsigned NOT NULL default '0', showsignature smallint(6) NOT NULL default '0', iconid smallint(5) unsigned NOT NULL default '0', messageread smallint(6) NOT NULL default '0', readtime int(10) unsigned NOT NULL default '0', receipt smallint(6) unsigned NOT NULL default '0', deleteprompt smallint(6) unsigned NOT NULL default '0', multiplerecipients smallint(6) unsigned NOT NULL default '0', PRIMARY KEY (privatemessageid), KEY userid (userid)) ENGINE=MyISAM; INSERT INTO t1 VALUES (128,0,33,33,8,':D','',996121863,1,0,2,996122850,2,0,0); CREATE TABLE t2 (userid int(10) unsigned NOT NULL auto_increment, usergroupid smallint(5) unsigned NOT NULL default '0', username varchar(50) NOT NULL default '', password varchar(50) NOT NULL default '', email varchar(50) NOT NULL default '', styleid smallint(5) unsigned NOT NULL default '0', parentemail varchar(50) NOT NULL default '', coppauser smallint(6) NOT NULL default '0', homepage varchar(100) NOT NULL default '', icq varchar(20) NOT NULL default '', aim varchar(20) NOT NULL default '', yahoo varchar(20) NOT NULL default '', signature mediumtext NOT NULL, adminemail smallint(6) NOT NULL default '0', showemail smallint(6) NOT NULL default '0', invisible smallint(6) NOT NULL default '0', usertitle varchar(250) NOT NULL default '', customtitle smallint(6) NOT NULL default '0', joindate int(10) unsigned NOT NULL default '0', cookieuser smallint(6) NOT NULL default '0', daysprune smallint(6) NOT NULL default '0', lastvisit int(10) unsigned NOT NULL default '0', lastactivity int(10) unsigned NOT NULL default '0', lastpost int(10) unsigned NOT NULL default '0', posts smallint(5) unsigned NOT NULL default '0', timezoneoffset varchar(4) NOT NULL default '', emailnotification smallint(6) NOT NULL default '0', buddylist mediumtext NOT NULL, ignorelist mediumtext NOT NULL, pmfolders mediumtext NOT NULL, receivepm smallint(6) NOT NULL default '0', emailonpm smallint(6) NOT NULL default '0', pmpopup smallint(6) NOT NULL default '0', avatarid smallint(6) NOT NULL default '0', avatarrevision int(6) unsigned NOT NULL default '0', options smallint(6) NOT NULL default '15', birthday date NOT NULL default '0000-00-00', maxposts smallint(6) NOT NULL default '-1', startofweek smallint(6) NOT NULL default '1', ipaddress varchar(20) NOT NULL default '', referrerid int(10) unsigned NOT NULL default '0', nosessionhash smallint(6) NOT NULL default '0', autorefresh smallint(6) NOT NULL default '-1', messagepopup tinyint(2) NOT NULL default '0', inforum smallint(5) unsigned NOT NULL default '0', ratenum smallint(5) unsigned NOT NULL default '0', ratetotal smallint(5) unsigned NOT NULL default '0', allowrate smallint(5) unsigned NOT NULL default '1', PRIMARY KEY (userid), KEY usergroupid (usergroupid), KEY username (username), KEY inforum (inforum)) ENGINE=MyISAM; INSERT INTO t2 VALUES (33,6,'Kevin','0','kevin@stileproject.com',1,'',0,'http://www.stileproject.com','','','','',1,1,0,'Administrator',0,996120694,1,-1,1030996168,1031027028,1030599436,36,'-6',0,'','','',1,0,1,0,0,15,'0000-00-00',-1,1,'64.0.0.0',0,1,-1,0,0,4,19,1); SELECT DISTINCT t1.*, t2.* FROM t1 LEFT JOIN t2 ON (t2.userid = t1.touserid); DROP TABLE t1,t2; # # test with const_item in ORDER BY # CREATE TABLE t1 (a int primary key, b int, c int); INSERT t1 VALUES (1,2,3); CREATE TABLE t2 (a int primary key, b int, c int); INSERT t2 VALUES (3,4,5); SELECT DISTINCT t1.a, t2.b FROM t1, t2 WHERE t1.a=1 ORDER BY t2.c; DROP TABLE t1,t2; # # Test of LEFT() with distinct # CREATE table t1 ( `id` int(11) NOT NULL auto_increment, `name` varchar(50) NOT NULL default '', PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=3 ; INSERT INTO t1 VALUES (1, 'aaaaa'); INSERT INTO t1 VALUES (3, 'aaaaa'); INSERT INTO t1 VALUES (2, 'eeeeeee'); select distinct left(name,1) as name from t1; drop table t1; # # Test case from sel000100 # CREATE TABLE t1 ( ID int(11) NOT NULL auto_increment, NAME varchar(75) DEFAULT '' NOT NULL, LINK_ID int(11) DEFAULT '0' NOT NULL, PRIMARY KEY (ID), KEY NAME (NAME), KEY LINK_ID (LINK_ID) ); INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (1,'Mike',0); INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (2,'Jack',0); INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (3,'Bill',0); CREATE TABLE t2 ( ID int(11) NOT NULL auto_increment, NAME varchar(150) DEFAULT '' NOT NULL, PRIMARY KEY (ID), KEY NAME (NAME) ); SELECT DISTINCT t2.id AS key_link_id, t2.name AS link FROM t1 LEFT JOIN t2 ON t1.link_id=t2.id GROUP BY t1.id ORDER BY link; drop table t1,t2; # # test case for #674 # CREATE TABLE t1 ( html varchar(5) default NULL, rin int(11) default '0', rout int(11) default '0' ) ENGINE=MyISAM; INSERT INTO t1 VALUES ('1',1,0); SELECT DISTINCT html,SUM(rout)/(SUM(rin)+1) as 'prod' FROM t1 GROUP BY rin; drop table t1; # # Test cases for #12625: DISTINCT for a list with constants # CREATE TABLE t1 (a int); INSERT INTO t1 VALUES (1),(2),(3),(4),(5); SELECT DISTINCT a, 1 FROM t1; SELECT DISTINCT 1, a FROM t1; CREATE TABLE t2 (a int, b int); INSERT INTO t2 VALUES (1,1),(2,2),(2,3),(2,4),(3,5); SELECT DISTINCT a, b, 2 FROM t2; SELECT DISTINCT 2, a, b FROM t2; SELECT DISTINCT a, 2, b FROM t2; DROP TABLE t1,t2; # # Bug#16458: Simple SELECT FOR UPDATE causes "Result Set not updatable" # error. # CREATE TABLE t1(a INT PRIMARY KEY, b INT); INSERT INTO t1 VALUES (1,1), (2,1), (3,1); EXPLAIN SELECT DISTINCT a FROM t1; EXPLAIN SELECT DISTINCT a,b FROM t1; EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2; EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2 WHERE t1_1.a = t1_2.a; EXPLAIN SELECT a FROM t1 GROUP BY a; EXPLAIN SELECT a,b FROM t1 GROUP BY a,b; EXPLAIN SELECT DISTINCT a,b FROM t1 GROUP BY a,b; CREATE TABLE t2(a INT, b INT NOT NULL, c INT NOT NULL, d INT, PRIMARY KEY (a,b)); INSERT INTO t2 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4); EXPLAIN SELECT DISTINCT a FROM t2; EXPLAIN SELECT DISTINCT a,a FROM t2; EXPLAIN SELECT DISTINCT b,a FROM t2; EXPLAIN SELECT DISTINCT a,c FROM t2; EXPLAIN SELECT DISTINCT c,a,b FROM t2; EXPLAIN SELECT DISTINCT a,b,d FROM t2 GROUP BY c,b,d; CREATE UNIQUE INDEX c_b_unq ON t2 (c,b); EXPLAIN SELECT DISTINCT a,b,d FROM t2 GROUP BY c,b,d; DROP TABLE t1,t2; # Bug 9784 DISTINCT IFNULL truncates data # create table t1 (id int, dsc varchar(50)); insert into t1 values (1, "line number one"), (2, "line number two"), (3, "line number three"); select distinct id, IFNULL(dsc, '-') from t1; drop table t1; # # Bug 21456: SELECT DISTINCT(x) produces incorrect results when using order by # CREATE TABLE t1 (a int primary key, b int); INSERT INTO t1 (a,b) values (1,1), (2,3), (3,2); explain SELECT DISTINCT a, b FROM t1 ORDER BY b; SELECT DISTINCT a, b FROM t1 ORDER BY b; DROP TABLE t1; # End of 4.1 tests # # Bug #15745 ( COUNT(DISTINCT CONCAT(x,y)) returns wrong result) # CREATE TABLE t1 ( ID int(11) NOT NULL auto_increment, x varchar(20) default NULL, y decimal(10,0) default NULL, PRIMARY KEY (ID), KEY (y) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO t1 VALUES (1,'ba','-1'), (2,'ba','1150'), (306,'ba','-1'), (307,'ba','1150'), (611,'ba','-1'), (612,'ba','1150'); select count(distinct x,y) from t1; select count(distinct concat(x,y)) from t1; drop table t1; # # Bug #18068: SELECT DISTINCT # CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a,b)); INSERT INTO t1 VALUES (1, 101); INSERT INTO t1 SELECT a + 1, a + 101 FROM t1; INSERT INTO t1 SELECT a + 2, a + 102 FROM t1; INSERT INTO t1 SELECT a + 4, a + 104 FROM t1; INSERT INTO t1 SELECT a + 8, a + 108 FROM t1; EXPLAIN SELECT DISTINCT a,a FROM t1 WHERE b < 12 ORDER BY a; SELECT DISTINCT a,a FROM t1 WHERE b < 12 ORDER BY a; DROP TABLE t1; #Bug #20836: Selecting into variables results in wrong results being returned --disable_warnings DROP TABLE IF EXISTS t1; --enable_warnings CREATE TABLE t1 (id INT NOT NULL, fruit_id INT NOT NULL, fruit_name varchar(20) default NULL); INSERT INTO t1 VALUES (1,1,'ORANGE'); INSERT INTO t1 VALUES (2,2,'APPLE'); INSERT INTO t1 VALUES (3,2,'APPLE'); INSERT INTO t1 VALUES (4,3,'PEAR'); SELECT DISTINCT fruit_id, fruit_name INTO @v1, @v2 FROM t1 WHERE fruit_name = 'APPLE'; SELECT @v1, @v2; SELECT DISTINCT fruit_id, fruit_name INTO @v3, @v4 FROM t1 GROUP BY fruit_id, fruit_name HAVING fruit_name = 'APPLE'; SELECT @v3, @v4; SELECT DISTINCT @v5:= fruit_id, @v6:= fruit_name INTO @v7, @v8 FROM t1 WHERE fruit_name = 'APPLE'; SELECT @v5, @v6, @v7, @v8; SELECT DISTINCT @v5 + fruit_id, CONCAT(@v6, fruit_name) INTO @v9, @v10 FROM t1 WHERE fruit_name = 'APPLE'; SELECT @v5, @v6, @v7, @v8, @v9, @v10; SELECT DISTINCT @v11:= @v5 + fruit_id, @v12:= CONCAT(@v6, fruit_name) INTO @v13, @v14 FROM t1 WHERE fruit_name = 'APPLE'; SELECT @v11, @v12, @v13, @v14; SELECT DISTINCT @v13, @v14 INTO @v15, @v16 FROM t1 WHERE fruit_name = 'APPLE'; SELECT @v15, @v16; SELECT DISTINCT 2 + 2, 'Bob' INTO @v17, @v18 FROM t1 WHERE fruit_name = 'APPLE'; SELECT @v17, @v18; --disable_warnings DROP TABLE IF EXISTS t2; --enable_warnings CREATE TABLE t2 (fruit_id INT NOT NULL, fruit_name varchar(20) default NULL); --disable_ps2_protocol SELECT DISTINCT fruit_id, fruit_name INTO OUTFILE '../../tmp/data1.tmp' FROM t1 WHERE fruit_name = 'APPLE'; --enable_ps2_protocol LOAD DATA INFILE '../../tmp/data1.tmp' INTO TABLE t2; --error 0,1 --remove_file $MYSQLTEST_VARDIR/tmp/data1.tmp --disable_ps2_protocol SELECT DISTINCT @v19:= fruit_id, @v20:= fruit_name INTO OUTFILE '../../tmp/data2.tmp' FROM t1 WHERE fruit_name = 'APPLE'; --enable_ps2_protocol LOAD DATA INFILE '../../tmp/data2.tmp' INTO TABLE t2; --remove_file $MYSQLTEST_VARDIR/tmp/data2.tmp SELECT @v19, @v20; SELECT * FROM t2; DROP TABLE t1; DROP TABLE t2; # # Bug #15881: cast problems # CREATE TABLE t1 (a CHAR(1)); INSERT INTO t1 VALUES('A'), (0); SELECT a FROM t1 WHERE a=0; SELECT DISTINCT a FROM t1 WHERE a=0; DROP TABLE t1; CREATE TABLE t1 (a DATE); INSERT INTO t1 VALUES ('1972-07-29'), ('1972-02-06'); EXPLAIN SELECT (SELECT DISTINCT a FROM t1 WHERE a = '2002-08-03'); EXPLAIN SELECT (SELECT DISTINCT ADDDATE(a,1) FROM t1 WHERE ADDDATE(a,1) = '2002-08-03'); CREATE TABLE t2 (a CHAR(5) CHARACTER SET latin1 COLLATE latin1_general_ci); INSERT INTO t2 VALUES (0xf6); INSERT INTO t2 VALUES ('oe'); SELECT COUNT(*) FROM (SELECT DISTINCT a FROM t2) dt; SELECT COUNT(*) FROM (SELECT DISTINCT a FROM t2 WHERE a='oe' COLLATE latin1_german2_ci) dt; DROP TABLE t1, t2; # # Bug #25551: inconsistent behaviour in grouping NULL, depending on index type # CREATE TABLE t1 (a INT, UNIQUE (a)); INSERT INTO t1 VALUES (4),(null),(2),(1),(null),(3); EXPLAIN SELECT DISTINCT a FROM t1; #result must have one row with NULL SELECT DISTINCT a FROM t1; EXPLAIN SELECT a FROM t1 GROUP BY a; #result must have one row with NULL SELECT a FROM t1 GROUP BY a; DROP TABLE t1; # #Bug #27659: SELECT DISTINCT returns incorrect result set when field is #repeated # # CREATE TABLE t1 (a INT, b INT); INSERT INTO t1 VALUES(1,1),(1,2),(1,3); SELECT DISTINCT a, b FROM t1; SELECT DISTINCT a, a, b FROM t1; DROP TABLE t1; --echo End of 5.0 tests # # Bug #34928: Confusion by having Primary Key and Index # CREATE TABLE t1(a INT, b INT, c INT, d INT default 0, e INT default 0, PRIMARY KEY(a,b,c,d,e), KEY(a,b,d,c) ); 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); EXPLAIN SELECT DISTINCT a, b, d, c FROM t1; SELECT DISTINCT a, b, d, c FROM t1; DROP TABLE t1; --echo # --echo # Bug #46159: simple query that never returns --echo # # Set max_heap_table_size to the minimum value so that GROUP BY table in the # SELECT query below gets converted to MyISAM SET @old_max_heap_table_size = @@max_heap_table_size; SET @@max_heap_table_size = 16384; # Set sort_buffer_size to the mininum value so that remove_duplicates() calls # remove_dup_with_compare() SET @old_sort_buffer_size = @@sort_buffer_size; SET @@sort_buffer_size = 32804; CREATE TABLE t1(c1 int, c2 VARCHAR(20)); INSERT INTO t1 VALUES (1, '1'), (1, '1'), (2, '2'), (3, '1'), (3, '1'), (4, '4'); # Now we just need to pad the table with random data so we have enough unique # values to force conversion of the GROUP BY table to MyISAM INSERT INTO t1 SELECT 5 + 10000 * RAND(), '5' FROM t1; INSERT INTO t1 SELECT 5 + 10000 * RAND(), '5' FROM t1; INSERT INTO t1 SELECT 5 + 10000 * RAND(), '5' FROM t1; INSERT INTO t1 SELECT 5 + 10000 * RAND(), '5' FROM t1; INSERT INTO t1 SELECT 5 + 10000 * RAND(), '5' FROM t1; INSERT INTO t1 SELECT 5 + 10000 * RAND(), '5' FROM t1; INSERT INTO t1 SELECT 5 + 10000 * RAND(), '5' FROM t1; INSERT INTO t1 SELECT 5 + 10000 * RAND(), '5' FROM t1; # First rows of the GROUP BY table that will be processed by # remove_dup_with_compare() SELECT c1, c2, COUNT(*) FROM t1 GROUP BY c1 LIMIT 4; # The actual test case SELECT DISTINCT c2 FROM t1 GROUP BY c1 HAVING COUNT(*) > 1; # Cleanup DROP TABLE t1; SET @@sort_buffer_size = @old_sort_buffer_size; SET @@max_heap_table_size = @old_max_heap_table_size; --echo End of 5.1 tests # # test_if_equality_guarantees_uniqueness() and dates # create table t1 (a varchar(100)); insert t1 values ('2010-10-10'), ('20101010'); select * from t1 where a = DATE('2010-10-10'); select distinct a from t1 where a = DATE('2010-10-10'); explain select distinct a from t1 where a = DATE('2010-10-10'); drop table t1; # # test_if_equality_guarantees_uniqueness() and different type combinations # --echo # date = string create table t1 (a date); insert t1 values ('2010-10-10'), ('20101010'); explain select distinct a from t1 where a = '2010-10-10'; drop table t1; --echo # double = string create table t1 (a double); insert t1 values (2), (2); explain select distinct a from t1 where a = '2'; --echo # double = int explain select distinct a from t1 where a = 2; --echo # string = double alter table t1 modify a varchar(100); explain select distinct a from t1 where a = 2e0; drop table t1; # # lp:731124 Loss of precision on DISTINCT # create table t1 (f1 varchar(40)); insert into t1 values ('2010-10-10 00:00:00.0001'),('2010-10-10 00:00:00.0002'),('2010-10-10 00:00:00.0003'); select time(f1) from t1 ; select distinct time(f1) from t1 ; drop table t1; # # MDEV-3875 Wrong result (missing row) on a DISTINCT query with the same subquery in the SELECT list and GROUP BY # MySQL Bug#66896 Distinct not distinguishing 0 from NULL when GROUP BY is used # create table t1(i int, g int); # remove_dup_with_hash_index insert into t1 values (null, 1), (0, 2); select distinct i from t1 group by g; drop table t1; create table t1(i int, g blob); # remove_dup_with_compare insert into t1 values (null, 1), (0, 2); select distinct i from t1 group by g; drop table t1; create table t1 (a int) engine=myisam; insert into t1 values (0),(7); create table t2 (b int) engine=myisam; insert into t2 values (7),(0),(3); create algorithm=temptable view v as select distinct (select max(a) from t1 where alias.b = a) as field1 from t2 as alias group by field1; select * from v; select distinct (select max(a) from t1 where alias.b = a) as field1 from t2 as alias group by field1; drop view v; drop table t1, t2; # # Bug mdev-5028: invalid distinct optimization when join buffer is used # CREATE TABLE t1 ( id int, i1 int, i2 int DEFAULT 0, d1 date DEFAULT '2000-01-01', d2 date DEFAULT '2000-01-01', t1 time DEFAULT '00:00:00', t2 time DEFAULT '00:00:00', dt1 datetime NOT NULL DEFAULT '2000-01-01 00:00:00', dt2 datetime NOT NULL DEFAULT '2000-01-01 00:00:00', c1 varchar(1) NOT NULL, c2 varchar(1) NOT NULL ) ENGINE=MyISAM; CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT * FROM t1; INSERT INTO t1 (id,i1,c1,c2) VALUES (1,7,'t','t'),(2,4,'k','k'),(3,2,'e','e'),(4,0,'i','i'),(5,1,'t','t'),(6,91,'m','m'), (7,6,'z','z'),(8,3,'c','c'),(9,6,'i','i'),(10,8,'v','v'), (11,1,'l','l'),(12,4,'j','j'), (13,5,'w','w'),(14,0,'r','r'),(15,7,'o','o'),(16,9,'o','o'),(17,7,'u','u'),(18,6,'f','f'), (19,0,'l','l'),(20,6,'g','g'),(21,1,'e','e'),(22,7,'y','y'),(23,0,'p','p'),(24,6,'v','v'), (25,5,'d','d'),(26,9,'i','i'),(27,5,'z','z'),(28,2,'q','q'),(29,4,'j','j'),(30,9,'m','m'), (31,8,'d','d'),(32,5,'r','r'),(33,1,'r','r'),(34,1,'k','k'),(35,4,'p','p'),(36,2,'x','x'), (37,5,'w','w'),(38,0,'k','k'),(39,7,'y','y'),(40,4,'p','p'),(41,9,'l','l'),(42,2,'u','u'), (43,3,'r','r'),(44,5,'y','y'),(45,3,'u','u'),(46,9,'t','t'),(47,8,'f','f'),(48,2,'f','f'), (49,2,'q','q'),(50,6,'v','v'),(51,6,'u','u'),(52,0,'b','b'),(53,1,'n','n'),(54,2,'p','p'), (55,0,'y','y'),(56,1,'l','l'),(57,1,'c','c'),(58,0,'d','d'),(59,2,'y','y'),(60,7,'l','l'), (61,6,'m','m'),(62,9,'q','q'),(63,0,'j','j'),(64,3,'u','u'),(65,4,'w','w'),(66,5,'p','p'), (67,8,'z','z'),(68,5,'u','u'),(69,7,'b','b'),(70,0,'f','f'),(71,6,'u','u'),(72,1,'i','i'), (73,9,'s','s'),(74,3,'y','y'),(75,5,'s','s'),(76,8,'x','x'),(77,3,'s','s'),(78,3,'l','l'), (79,8,'b','b'),(80,0,'p','p'),(81,9,'m','m'),(82,5,'k','k'),(83,7,'u','u'),(84,0,'y','y'), (85,2,'x','x'),(86,5,'h','h'),(87,5,'j','j'),(88,5,'o','o'),(89,9,'o','o'),(90,1,'c','c'), (91,7,'k','k'),(92,9,'t','t'),(93,3,'h','h'),(94,6,'g','g'),(95,9,'r','r'),(96,2,'i','i'); CREATE TABLE t2 (i INT) ENGINE=MyISAM; INSERT INTO t2 VALUES (7),(8); SELECT STRAIGHT_JOIN COUNT(DISTINCT t1.id) FROM t1, v1, t2 WHERE v1.id = t2.i AND t1.i1 = v1.i1 AND t2.i != 3; EXPLAIN EXTENDED SELECT STRAIGHT_JOIN DISTINCT t1.id FROM t1, v1, t2 WHERE v1.id = t2.i AND t1.i1 = v1.i1 AND t2.i != 3; set join_buffer_size=1024; --sorted_result SELECT STRAIGHT_JOIN DISTINCT t1.id FROM t1, v1, t2 WHERE v1.id = t2.i AND t1.i1 = v1.i1 AND t2.i != 3; set join_buffer_size=1024*16; --sorted_result SELECT STRAIGHT_JOIN DISTINCT t1.id FROM t1, v1, t2 WHERE v1.id = t2.i AND t1.i1 = v1.i1 AND t2.i != 3; set join_buffer_size=default; SELECT STRAIGHT_JOIN DISTINCT t1.id FROM t1, v1, t2 WHERE v1.id = t2.i AND t1.i1 = v1.i1 AND t2.i != 3; DROP VIEW v1; DROP TABLE t1,t2; --echo # --echo # Bug #11744875: 4082: integer lengths cause truncation with distinct concat and innodb --echo # CREATE TABLE t1 (a INT(1), b INT(1)); INSERT INTO t1 VALUES (1111, 2222), (3333, 4444); SELECT DISTINCT CONCAT(a,b) AS c FROM t1 ORDER BY 1; DROP TABLE t1; --echo # --echo # Bug#16539979 BASIC SELECT COUNT(DISTINCT ID) IS BROKEN. --echo # Bug#17867117 ERROR RESULT WHEN "COUNT + DISTINCT + CASE WHEN" NEED MERGE_WALK --echo # SET @tmp_table_size_save= @@tmp_table_size; SET @@tmp_table_size= 1024; CREATE TABLE t1 (a INT); INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8); INSERT INTO t1 SELECT a+8 FROM t1; INSERT INTO t1 SELECT a+16 FROM t1; INSERT INTO t1 SELECT a+32 FROM t1; INSERT INTO t1 SELECT a+64 FROM t1; INSERT INTO t1 VALUE(NULL); SELECT COUNT(DISTINCT a) FROM t1; SELECT COUNT(DISTINCT (a+0)) FROM t1; DROP TABLE t1; create table tb( id int auto_increment primary key, v varchar(32)) engine=myisam charset=gbk; insert into tb(v) values("aaa"); insert into tb(v) (select v from tb); insert into tb(v) (select v from tb); insert into tb(v) (select v from tb); insert into tb(v) (select v from tb); insert into tb(v) (select v from tb); insert into tb(v) (select v from tb); update tb set v=concat(v, id); select count(distinct case when id<=64 then id end) from tb; select count(distinct case when id<=63 then id end) from tb; drop table tb; SET @@tmp_table_size= @tmp_table_size_save; --echo # --echo # MDEV-14695: Assertion `n < m_size' failed in Bounds_checked_array::operator --echo # CREATE TABLE t1 (b1 BIT, b2 BIT, b3 BIT, b4 BIT , b5 BIT, b6 BIT); INSERT INTO t1 VALUES (1,0,0,1,0,1),(0,1,0,0,1,0); SELECT DISTINCT b1+'0', b2+'0', b3+'0', b4+'0', b5+'0', b6 +'0' FROM t1; DROP TABLE t1; --echo # --echo # MDEV-12575: Server crash in AGGR_OP::put_record or in JOIN_CACHE::free --echo # or Invalid write in JOIN::make_aggr_tables_info --echo # CREATE TABLE t1 (pk INT PRIMARY KEY); INSERT INTO t1 VALUES (1),(2); explain ( SELECT DISTINCT 1 FROM t1 ORDER BY BENCHMARK(1, MIN(pk)) ) UNION ( SELECT DISTINCT 1 FROM t1 ORDER BY BENCHMARK(1, MIN(pk)) ); ( SELECT DISTINCT 1 FROM t1 ORDER BY BENCHMARK(1, MIN(pk)) ) UNION ( SELECT DISTINCT 1 FROM t1 ORDER BY BENCHMARK(1, MIN(pk)) ); drop table t1; --echo End of 5.5 tests --echo # --echo # MDEV-20057 Distinct SUM on CROSS JOIN and grouped returns wrong result --echo # create table t1 (c int, d int); insert into t1 values (5, 1), (0, 3); select distinct sum(distinct 1), sum(t1.d) > 2 from (t1 e join t1) group by t1.c; select distinct sum(distinct 1), sum(t1.d) > 2, t1.c from (t1 e join t1) group by t1.c; insert into t1 values (6,6); select distinct sum(distinct 1), sum(t1.d) > 5 from (t1 e join t1) group by t1.c; select distinct sum(distinct 1), sum(t1.d) > 5, t1.c from (t1 e join t1) group by t1.c; # Force usage of remove_dup_with_compare() algorithm SET @sort_buffer_size_save= @@sort_buffer_size; set @@sort_buffer_size=1024; insert into t1 select -seq,-seq from seq_1_to_100; select distinct sum(distinct 1), sum(t1.d) > 2, length(group_concat(t1.d)) > 1000 from (t1 e join t1) group by t1.c having t1.c > -2 ; select distinct sum(distinct 1), sum(t1.d) > 2, length(group_concat(t1.d)) > 1000,t1.c from (t1 e join t1) group by t1.c having t1.c > -2; drop table t1; set @@sort_buffer_size=@sort_buffer_size_save; --echo # --echo # MDEV-31113 Server crashes in store_length / Type_handler_string_result::make_sort_key --echo # with DISTINCT and group function --echo # CREATE TABLE t (f INT); INSERT INTO t VALUES (1),(2); --replace_column 2 # SELECT DISTINCT CONVERT(STDDEV(f), CHAR(16)) AS f1, UUID() AS f2 FROM t GROUP BY f2 WITH ROLLUP; DROP TABLE t; --echo # End of 10.4 tests --echo # --echo # MDEV-27382: OFFSET is ignored when it is combined with the DISTINCT, IN() and JOIN --echo # CREATE TABLE t1 ( id int(7) NOT NULL AUTO_INCREMENT, name varchar(50) DEFAULT NULL, primary key (id) ); INSERT INTO t1 VALUES (1, 'Reed'), (10, 'no-child'); CREATE TABLE t2 ( id int(11) NOT NULL AUTO_INCREMENT, parent_id int(7) NOT NULL, name varchar(100) DEFAULT NULL, primary key (id), key(parent_id) ); INSERT INTO t2 VALUES (1, 1,'John'), (2, 2,'no-parent'); SELECT DISTINCT p.id FROM t1 p LEFT JOIN t2 c ON p.id = c.parent_id WHERE p.id=1 LIMIT 0; SELECT DISTINCT p.id FROM t1 p LEFT JOIN t2 c ON p.id = c.parent_id WHERE p.id=1 LIMIT 0 offset 5; --echo # Test the second part of the fix: just check that "LIMIT 0 OFFSET n" is --echo # handled in the same way as "LIMIT 0" explain select * from t1 limit 0; explain select * from t1 limit 0 offset 10; drop table t1, t2; --echo # --echo # MDEV-28285 Unexpected result when combining DISTINCT, subselect --echo # and LIMIT --echo # create table t1 (a int primary key); create table t2 (a int primary key, b int not null); insert into t1 select seq from seq_1_to_10; insert into t2 select seq,seq from seq_1_to_10; select distinct a from t1 where t1.a=1 and t1.a in (select a from t2 where t2.b in (1,2)); explain select distinct a from t1 where t1.a=1 and t1.a in (select a+0 from t2 where t2.b in (1,2)) limit 10,10; select distinct a from t1 where t1.a=1 and t1.a in (select a+0 from t2 where t2.b in (1,2)) limit 10,10; select distinct a from t1 where t1.a=1 and t1.a in (select a+0 from t2 where t2.b in (1,2)) limit 0,1; drop table t1,t2; --echo # --echo # MDEV-31743 Server crash in store_length, assertion failure in --echo # Type_handler_string_result::sort_length --echo # create table t1 (a int, b longtext, c varchar(18)); insert into t1 values (1, 'Aa123456', 'abc'), (2, 'Bb7897777', 'def'), (3, 'Cc01287', 'xyz'), (5, 'd12345', 'efg'); select distinct if(sum(a), b, 0) from t1 group by value(c) with rollup; drop table t1; --echo # --echo # end of 10.5 tests --echo #