2001-09-28 07:05:54 +02:00
drop table if exists t1,t2,t3;
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;
2000-12-28 02:56:38 +01:00
id
NULL
-1
0
1
2
3
4
5
6
7
8
9
10
2001-09-28 07:05:54 +02:00
select * from t1 order by id;
2000-12-28 02:56:38 +01:00
id facility
NULL NULL
-1
0
1 /L
2 A01
3 ANC
4 F01
5 FBX
6 MT
7 P
8 RV
9 SRV
10 VMT
2001-09-28 07:05:54 +02:00
select id-5,facility from t1 order by "id-5";
2000-12-28 02:56:38 +01:00
id-5 facility
NULL NULL
-6
-5
-4 /L
-3 A01
-2 ANC
-1 F01
0 FBX
1 MT
2 P
3 RV
4 SRV
5 VMT
2001-09-28 07:05:54 +02:00
select id,concat(facility) from t1 group by id ;
2000-12-28 02:56:38 +01:00
id concat(facility)
NULL NULL
-1
0
1 /L
2 A01
3 ANC
4 F01
5 FBX
6 MT
7 P
8 RV
9 SRV
10 VMT
2001-09-28 07:05:54 +02:00
select id+0 as a,max(id),concat(facility) as b from t1 group by a order by b desc,a;
2000-12-28 02:56:38 +01:00
a max(id) b
10 10 VMT
9 9 SRV
8 8 RV
7 7 P
6 6 MT
5 5 FBX
4 4 F01
3 3 ANC
2 2 A01
1 1 /L
-1 -1
0 0
2003-02-06 15:55:59 +01:00
NULL NULL NULL
2001-09-28 07:05:54 +02:00
select id >= 0 and id <= 5 as grp,count(*) from t1 group by grp;
2000-12-28 02:56:38 +01:00
grp count(*)
2002-09-03 14:44:25 +02:00
NULL 1
0 6
2000-12-28 02:56:38 +01:00
1 6
2001-09-28 07:05:54 +02:00
SELECT DISTINCT FACILITY FROM t1;
2000-12-28 02:56:38 +01:00
FACILITY
NULL
/L
A01
ANC
F01
FBX
MT
P
RV
SRV
VMT
2001-09-28 07:05:54 +02:00
SELECT FACILITY FROM t2;
2000-12-28 02:56:38 +01:00
FACILITY
NULL
/L
A01
ANC
F01
FBX
MT
P
RV
SRV
VMT
2001-09-28 07:05:54 +02:00
SELECT count(*) from t1,t2 where t1.facility=t2.facility;
2000-12-28 02:56:38 +01:00
count(*)
12
2001-09-28 07:05:54 +02:00
select count(facility) from t1;
2000-12-28 02:56:38 +01:00
count(facility)
12
2001-09-28 07:05:54 +02:00
select count(*) from t1;
2000-12-28 02:56:38 +01:00
count(*)
13
2001-09-28 07:05:54 +02:00
select count(*) from t1 where facility IS NULL;
2000-12-28 02:56:38 +01:00
count(*)
1
2001-09-28 07:05:54 +02:00
select count(*) from t1 where facility = NULL;
2000-12-28 02:56:38 +01:00
count(*)
0
2001-09-28 07:05:54 +02:00
select count(*) from t1 where facility IS NOT NULL;
2000-12-28 02:56:38 +01:00
count(*)
12
2001-09-28 07:05:54 +02:00
select count(*) from t1 where id IS NULL;
2000-12-28 02:56:38 +01:00
count(*)
1
2001-09-28 07:05:54 +02:00
select count(*) from t1 where id IS NOT NULL;
2000-12-28 02:56:38 +01:00
count(*)
12
2001-09-28 07:05:54 +02:00
drop table t1,t2;
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;
2000-12-28 02:56:38 +01:00
UserId
2001-09-28 07:05:54 +02:00
SELECT UserId FROM t1 WHERE UserId=22 group by Userid;
2000-12-28 02:56:38 +01:00
UserId
2001-09-28 07:05:54 +02:00
SELECT DISTINCT UserId FROM t1 WHERE UserId=22 group by Userid;
2000-12-28 02:56:38 +01:00
UserId
2001-09-28 07:05:54 +02:00
SELECT DISTINCT UserId FROM t1 WHERE UserId=22;
2000-12-28 02:56:38 +01:00
UserId
2001-09-28 07:05:54 +02:00
drop table t1;
CREATE TABLE t1 (a int(10) unsigned not null primary key,b int(10) unsigned);
2003-06-27 02:04:54 +02:00
INSERT INTO t1 VALUES (1,1),(2,1),(3,1),(4,1);
2001-09-28 07:05:54 +02:00
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;
2000-12-28 02:56:38 +01:00
b
1
2001-09-28 07:05:54 +02:00
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;
2002-09-26 22:08:22 +02:00
id select_type table type possible_keys key key_len ref rows Extra
2010-09-28 21:39:33 +02:00
1 SIMPLE t1 ALL PRIMARY NULL NULL NULL 4 Using where; Using temporary
2009-12-15 08:16:46 +01:00
1 SIMPLE t3 ref a a 5 test.t1.b 2 Using index
2010-10-18 22:33:05 +02:00
1 SIMPLE t2 index a a 4 NULL 5 Using where; Using index; Distinct; Using join buffer (flat, BNL join)
2001-09-28 07:05:54 +02:00
SELECT distinct t3.a FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a;
2000-12-28 02:56:38 +01:00
a
1
2001-09-28 07:05:54 +02:00
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;
2002-09-26 22:08:22 +02:00
id select_type table type possible_keys key key_len ref rows Extra
2003-08-11 21:44:43 +02:00
1 SIMPLE t1 index PRIMARY PRIMARY 4 NULL 4 Using index; Using temporary
2009-12-15 08:16:46 +01:00
1 SIMPLE t3 ref a a 5 test.t1.a 11 Using index; Distinct
2001-09-28 07:05:54 +02:00
select distinct t1.a from t1,t3 where t1.a=t3.a;
2000-12-28 02:56:38 +01:00
a
1
2
2001-09-28 07:05:54 +02:00
select distinct 1 from t1,t3 where t1.a=t3.a;
2000-12-28 02:56:38 +01:00
1
1
2002-12-03 12:08:25 +01:00
explain SELECT distinct t1.a from t1;
2002-12-06 20:11:27 +01:00
id select_type table type possible_keys key key_len ref rows Extra
2004-10-11 19:10:07 +02:00
1 SIMPLE t1 index NULL PRIMARY 4 NULL 4 Using index
2002-12-03 12:08:25 +01:00
explain SELECT distinct t1.a from t1 order by a desc;
2002-12-06 20:11:27 +01:00
id select_type table type possible_keys key key_len ref rows Extra
2004-10-11 19:10:07 +02:00
1 SIMPLE t1 index NULL PRIMARY 4 NULL 4 Using index
2002-12-03 12:08:25 +01:00
explain SELECT t1.a from t1 group by a order by a desc;
2002-12-06 20:11:27 +01:00
id select_type table type possible_keys key key_len ref rows Extra
2004-10-11 19:10:07 +02:00
1 SIMPLE t1 index NULL PRIMARY 4 NULL 4 Using index
2002-12-03 12:08:25 +01:00
explain SELECT distinct t1.a from t1 order by a desc limit 1;
2002-12-06 20:11:27 +01:00
id select_type table type possible_keys key key_len ref rows Extra
2007-08-02 21:45:56 +02:00
1 SIMPLE t1 index NULL PRIMARY 4 NULL 1 Using index
2002-12-03 12:08:25 +01:00
explain SELECT distinct a from t3 order by a desc limit 2;
2002-12-06 20:11:27 +01:00
id select_type table type possible_keys key key_len ref rows Extra
2007-08-02 21:45:56 +02:00
1 SIMPLE t3 index NULL a 5 NULL 40 Using index
2002-12-03 12:08:25 +01:00
explain SELECT distinct a,b from t3 order by a+1;
2002-12-06 20:11:27 +01:00
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t3 ALL NULL NULL NULL NULL 204 Using temporary; Using filesort
2007-08-02 21:45:56 +02:00
explain SELECT distinct a,b from t3 order by a limit 2;
2002-12-06 20:11:27 +01:00
id select_type table type possible_keys key key_len ref rows Extra
2007-08-02 21:45:56 +02:00
1 SIMPLE t3 index NULL a 5 NULL 2 Using temporary
2002-12-03 12:08:25 +01:00
explain SELECT a,b from t3 group by a,b order by a+1;
2002-12-06 20:11:27 +01:00
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t3 ALL NULL NULL NULL NULL 204 Using temporary; Using filesort
2001-09-28 07:05:54 +02:00
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;
2000-12-28 02:56:38 +01:00
name
aa
ab
2001-09-28 07:05:54 +02:00
SELECT DISTINCT name FROM t1 LIMIT 2;
2000-12-28 02:56:38 +01:00
name
aa
ab
2001-09-28 07:05:54 +02:00
SELECT DISTINCT 1 FROM t1 LIMIT 2;
2000-12-28 02:56:38 +01:00
1
1
2001-09-28 07:05:54 +02:00
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;
2000-12-28 02:56:38 +01:00
key_link_id link
NULL NULL
2001-09-28 07:05:54 +02:00
drop table t1,t2;
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);
2011-12-15 23:26:59 +01:00
set @save_join_cache_level=@@join_cache_level;
set join_cache_level=1;
2001-09-28 07:05:54 +02:00
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));
2002-09-26 22:08:22 +02:00
id select_type table type possible_keys key key_len ref rows Extra
2007-05-29 14:58:18 +02:00
1 SIMPLE t1 index id id 4 NULL 2 Using index; Using temporary
2010-10-18 22:33:05 +02:00
1 SIMPLE t2 index id id 8 NULL 1 Using index; Distinct; Using join buffer (flat, BNL join)
1 SIMPLE t3 index id id 8 NULL 1 Using index; Distinct; Using join buffer (flat, BNL join)
1 SIMPLE j_lj_t2 index id id 4 NULL 2 Using where; Using index; Distinct; Using join buffer (flat, BNL join)
2007-05-29 14:58:18 +02:00
1 SIMPLE t2_lj ref id id 4 test.j_lj_t2.id 1 Using where; Using index; Distinct
2010-10-18 22:33:05 +02:00
1 SIMPLE j_lj_t3 index id id 4 NULL 2 Using where; Using index; Distinct; Using join buffer (flat, BNL join)
2003-10-08 11:01:58 +02:00
1 SIMPLE t3_lj ref id id 4 test.j_lj_t3.id 1 Using where; Using index; Distinct
2001-09-28 07:05:54 +02:00
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));
2000-12-28 02:56:38 +01:00
id
2
2001-09-28 07:05:54 +02:00
drop table t1,t2,t3;
2011-12-15 23:26:59 +01:00
set join_cache_level=@save_join_cache_level;
2001-09-28 07:05:54 +02:00
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;
2001-01-28 20:35:50 +01:00
a sec_to_time(sum(time_to_sec(t)))
1 00:06:15
1 00:36:30
1 00:36:30
2001-09-28 07:05:54 +02:00
select distinct a,sec_to_time(sum(time_to_sec(t))) from t1 group by a,b;
2001-01-28 20:35:50 +01:00
a sec_to_time(sum(time_to_sec(t)))
1 00:06:15
1 00:36:30
2001-09-28 07:05:54 +02:00
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;
2001-01-28 20:35:50 +01:00
a sec_to_time(sum(time_to_sec(t)))
1 00:06:15
1 00:36:30
1 00:36:30
2001-09-28 07:05:54 +02:00
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;
2001-01-28 20:35:50 +01:00
a sec_to_time(sum(time_to_sec(t)))
1 00:06:15
1 00:36:30
2001-09-28 07:05:54 +02:00
drop table t1,t2;
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;
2001-08-10 18:02:23 +02:00
a
4
3
2001-09-28 07:05:54 +02:00
select distinct a,c from t1 group by b,c,a having a > 2 order by a desc;
2001-08-10 18:02:23 +02:00
a c
4 NULL
3 NULL
2001-09-28 07:05:54 +02:00
drop table t1;
2003-12-10 05:31:42 +01:00
create table t1 (a char(1), key(a)) engine=myisam;
2002-03-02 08:51:24 +01:00
insert into t1 values('1'),('1');
select * from t1 where a >= '1';
a
1
1
select distinct a from t1 order by a desc;
a
1
select distinct a from t1 where a >= '1' order by a desc;
a
1
drop table t1;
2002-09-10 21:23:59 +02:00
CREATE TABLE t1 (email varchar(50), infoID BIGINT, dateentered DATETIME);
2002-08-16 23:35:51 +02:00
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;
email shipcode
test1@testdomain.com Z001
test2@testdomain.com Z001
test2@testdomain.com R002
2004-05-10 14:48:50 +02:00
test3@testdomain.com Z001
2002-08-16 23:35:51 +02:00
SELECT DISTINCTROW email FROM t1 ORDER BY dateentered DESC;
email
test1@testdomain.com
test2@testdomain.com
test3@testdomain.com
SELECT DISTINCTROW email, shipcode FROM t1, t2 WHERE t1.infoID=t2.infoID ORDER BY dateentered DESC;
email shipcode
test1@testdomain.com Z001
test2@testdomain.com Z001
test2@testdomain.com R002
test3@testdomain.com Z001
drop table t1,t2;
2003-12-10 05:31:42 +01:00
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;
2002-09-04 13:13:31 +02:00
INSERT INTO t1 VALUES (128,0,33,33,8,':D','',996121863,1,0,2,996122850,2,0,0);
2003-12-10 05:31:42 +01:00
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;
2002-09-04 13:13:31 +02:00
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);
2002-09-03 22:00:23 +02:00
privatemessageid folderid userid touserid fromuserid title message dateline showsignature iconid messageread readtime receipt deleteprompt multiplerecipients userid usergroupid username password email styleid parentemail coppauser homepage icq aim yahoo signature adminemail showemail invisible usertitle customtitle joindate cookieuser daysprune lastvisit lastactivity lastpost posts timezoneoffset emailnotification buddylist ignorelist pmfolders receivepm emailonpm pmpopup avatarid avatarrevision options birthday maxposts startofweek ipaddress referrerid nosessionhash autorefresh messagepopup inforum ratenum ratetotal allowrate
128 0 33 33 8 :D 996121863 1 0 2 996122850 2 0 0 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
2003-01-06 00:48:59 +01:00
DROP TABLE t1,t2;
2002-09-10 21:23:59 +02:00
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;
a b
1 4
2003-01-06 00:48:59 +01:00
DROP TABLE t1,t2;
2003-12-10 05:31:42 +01:00
CREATE table t1 ( `id` int(11) NOT NULL auto_increment, `name` varchar(50) NOT NULL default '', PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=3 ;
2003-07-05 15:29:11 +02:00
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;
name
a
e
drop table t1;
2003-09-12 03:18:07 +02:00
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;
key_link_id link
NULL NULL
drop table t1,t2;
CREATE TABLE t1 (
html varchar(5) default NULL,
rin int(11) default '0',
2003-09-24 15:26:20 +02:00
rout int(11) default '0'
2003-12-10 05:31:42 +01:00
) ENGINE=MyISAM;
2003-09-12 03:18:07 +02:00
INSERT INTO t1 VALUES ('1',1,0);
2003-09-24 15:26:20 +02:00
SELECT DISTINCT html,SUM(rout)/(SUM(rin)+1) as 'prod' FROM t1 GROUP BY rin;
2003-09-12 03:18:07 +02:00
html prod
2005-05-05 22:01:39 +02:00
1 0.0000
2003-09-12 03:18:07 +02:00
drop table t1;
2005-08-19 10:57:22 +02:00
CREATE TABLE t1 (a int);
INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
SELECT DISTINCT a, 1 FROM t1;
a 1
1 1
2 1
3 1
4 1
5 1
SELECT DISTINCT 1, a FROM t1;
1 a
1 1
1 2
1 3
1 4
1 5
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;
a b 2
1 1 2
2 2 2
2 3 2
2 4 2
3 5 2
SELECT DISTINCT 2, a, b FROM t2;
2 a b
2 1 1
2 2 2
2 2 3
2 2 4
2 3 5
SELECT DISTINCT a, 2, b FROM t2;
a 2 b
1 2 1
2 2 2
2 2 3
2 2 4
3 2 5
DROP TABLE t1,t2;
2006-06-27 16:40:19 +02:00
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;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL PRIMARY 4 NULL 3 Using index
EXPLAIN SELECT DISTINCT a,b FROM t1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3
EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2;
id select_type table type possible_keys key key_len ref rows Extra
2007-05-29 14:58:18 +02:00
1 SIMPLE t1_1 ALL NULL NULL NULL NULL 3 Using temporary
2010-10-18 22:33:05 +02:00
1 SIMPLE t1_2 index NULL PRIMARY 4 NULL 3 Using index; Distinct; Using join buffer (flat, BNL join)
2006-06-27 16:40:19 +02:00
EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2
WHERE t1_1.a = t1_2.a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1_1 ALL PRIMARY NULL NULL NULL 3 Using temporary
1 SIMPLE t1_2 eq_ref PRIMARY PRIMARY 4 test.t1_1.a 1 Using index; Distinct
EXPLAIN SELECT a FROM t1 GROUP BY a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL PRIMARY 4 NULL 3 Using index
EXPLAIN SELECT a,b FROM t1 GROUP BY a,b;
id select_type table type possible_keys key key_len ref rows Extra
2007-08-28 18:01:29 +02:00
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using filesort
2006-06-27 16:40:19 +02:00
EXPLAIN SELECT DISTINCT a,b FROM t1 GROUP BY a,b;
id select_type table type possible_keys key key_len ref rows Extra
2007-08-28 18:01:29 +02:00
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using filesort
2007-01-31 09:18:26 +01:00
CREATE TABLE t2(a INT, b INT NOT NULL, c INT NOT NULL, d INT,
PRIMARY KEY (a,b));
2006-06-27 16:40:19 +02:00
INSERT INTO t2 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4);
EXPLAIN SELECT DISTINCT a FROM t2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 index NULL PRIMARY 8 NULL 3 Using index
EXPLAIN SELECT DISTINCT a,a FROM t2;
id select_type table type possible_keys key key_len ref rows Extra
2006-06-28 14:53:54 +02:00
1 SIMPLE t2 index NULL PRIMARY 8 NULL 3 Using index
2006-06-27 16:40:19 +02:00
EXPLAIN SELECT DISTINCT b,a FROM t2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 index NULL PRIMARY 8 NULL 3 Using index
EXPLAIN SELECT DISTINCT a,c FROM t2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 3 Using temporary
EXPLAIN SELECT DISTINCT c,a,b FROM t2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 3
EXPLAIN SELECT DISTINCT a,b,d FROM t2 GROUP BY c,b,d;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 ALL NULL NULL NULL NULL 3 Using temporary; Using filesort
CREATE UNIQUE INDEX c_b_unq ON t2 (c,b);
EXPLAIN SELECT DISTINCT a,b,d FROM t2 GROUP BY c,b,d;
id select_type table type possible_keys key key_len ref rows Extra
2007-08-28 18:01:29 +02:00
1 SIMPLE t2 ALL NULL NULL NULL NULL 3 Using filesort
2006-06-27 16:40:19 +02:00
DROP TABLE t1,t2;
2005-06-08 17:35:37 +02:00
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;
id IFNULL(dsc, '-')
1 line number one
2 line number two
3 line number three
drop table t1;
2006-08-23 15:46:57 +02:00
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;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using filesort
SELECT DISTINCT a, b FROM t1 ORDER BY b;
a b
1 1
3 2
2 3
DROP TABLE t1;
2006-03-05 17:48:31 +01:00
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;
count(distinct x,y)
2
select count(distinct concat(x,y)) from t1;
count(distinct concat(x,y))
2
drop table t1;
BUG#18068: SELECT DISTINCT (with duplicates and covering index)
When converting DISTINCT to GROUP BY where the columns are from the covering
index and they are quoted twice in the SELECT list the optimizer is creating
improper processing sequence. This is because of the fact that the columns
of the covering index are not recognized as such and treated as non-index
columns.
Generally speaking duplicate columns can safely be removed from the GROUP
BY/DISTINCT list because this will not add or remove new rows in the
resulting set. Duplicates can be removed even if they are not consecutive
(as is the case for ORDER BY, where the duplicate columns can be removed
only if they are consecutive).
So we can safely transform "SELECT DISTINCT a,a FROM ... ORDER BY a" to
"SELECT a,a FROM ... GROUP BY a ORDER BY a" instead of
"SELECT a,a FROM .. GROUP BY a,a ORDER BY a". We can even transform
"SELECT DISTINCT a,b,a FROM ... ORDER BY a,b" to
"SELECT a,b,a FROM ... GROUP BY a,b ORDER BY a,b".
The fix to this bug consists of checking for duplicate columns in the SELECT
list when constructing the GROUP BY list in transforming DISTINCT to GROUP
BY and skipping the ones that are already in.
mysql-test/r/distinct.result:
test case for the bug without loose index scan
mysql-test/r/group_min_max.result:
test case for the bug
mysql-test/t/distinct.test:
test case for the bug without loose index scan
mysql-test/t/group_min_max.test:
test case for the bug
sql/sql_select.cc:
duplicates check and removal
2006-05-09 17:13:01 +02:00
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;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL PRIMARY 8 NULL 16 Using where; Using index
SELECT DISTINCT a,a FROM t1 WHERE b < 12 ORDER BY a;
a a
DROP TABLE t1;
2008-09-23 11:24:32 +02:00
DROP TABLE IF EXISTS t1;
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;
@v1 @v2
2 APPLE
SELECT DISTINCT fruit_id, fruit_name INTO @v3, @v4 FROM t1 GROUP BY fruit_id,
fruit_name HAVING fruit_name = 'APPLE';
SELECT @v3, @v4;
@v3 @v4
2 APPLE
SELECT DISTINCT @v5:= fruit_id, @v6:= fruit_name INTO @v7, @v8 FROM t1 WHERE
fruit_name = 'APPLE';
SELECT @v5, @v6, @v7, @v8;
@v5 @v6 @v7 @v8
2009-02-24 15:47:12 +01:00
2 APPLE 2 APPLE
2008-09-23 11:24:32 +02:00
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;
@v5 @v6 @v7 @v8 @v9 @v10
2009-02-24 15:47:12 +01:00
2 APPLE 2 APPLE 4 APPLEAPPLE
2008-09-23 11:24:32 +02:00
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;
@v11 @v12 @v13 @v14
2009-02-24 15:47:12 +01:00
4 APPLEAPPLE 4 APPLEAPPLE
2008-09-23 11:24:32 +02:00
SELECT DISTINCT @v13, @v14 INTO @v15, @v16 FROM t1 WHERE fruit_name = 'APPLE';
SELECT @v15, @v16;
@v15 @v16
2009-02-24 15:47:12 +01:00
4 APPLEAPPLE
2008-09-23 11:24:32 +02:00
SELECT DISTINCT 2 + 2, 'Bob' INTO @v17, @v18 FROM t1 WHERE fruit_name =
'APPLE';
SELECT @v17, @v18;
@v17 @v18
4 Bob
DROP TABLE IF EXISTS t2;
CREATE TABLE t2 (fruit_id INT NOT NULL, fruit_name varchar(20)
default NULL);
SELECT DISTINCT fruit_id, fruit_name INTO OUTFILE
2008-10-29 18:38:18 +01:00
'../../tmp/data1.tmp' FROM t1 WHERE fruit_name = 'APPLE';
LOAD DATA INFILE '../../tmp/data1.tmp' INTO TABLE t2;
2008-09-23 11:24:32 +02:00
SELECT DISTINCT @v19:= fruit_id, @v20:= fruit_name INTO OUTFILE
2008-10-29 18:38:18 +01:00
'../../tmp/data2.tmp' FROM t1 WHERE fruit_name = 'APPLE';
LOAD DATA INFILE '../../tmp/data2.tmp' INTO TABLE t2;
2008-09-23 11:24:32 +02:00
SELECT @v19, @v20;
@v19 @v20
2 APPLE
SELECT * FROM t2;
fruit_id fruit_name
2 APPLE
2 APPLE
DROP TABLE t1;
DROP TABLE t2;
2007-01-05 13:02:50 +01:00
CREATE TABLE t1 (a CHAR(1));
INSERT INTO t1 VALUES('A'), (0);
SELECT a FROM t1 WHERE a=0;
a
A
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: 'A'
SELECT DISTINCT a FROM t1 WHERE a=0;
a
A
0
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: 'A'
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');
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
EXPLAIN SELECT (SELECT DISTINCT ADDDATE(a,1) FROM t1
WHERE ADDDATE(a,1) = '2002-08-03');
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
2007-04-26 22:12:09 +02:00
2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
2007-01-05 13:02:50 +01:00
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;
COUNT(*)
2
SELECT COUNT(*) FROM
(SELECT DISTINCT a FROM t2 WHERE a='oe' COLLATE latin1_german2_ci) dt;
COUNT(*)
2
DROP TABLE t1, t2;
2007-01-31 09:18:26 +01:00
CREATE TABLE t1 (a INT, UNIQUE (a));
INSERT INTO t1 VALUES (4),(null),(2),(1),(null),(3);
EXPLAIN SELECT DISTINCT a FROM t1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL a 5 NULL 6 Using index
SELECT DISTINCT a FROM t1;
a
NULL
1
2
3
4
EXPLAIN SELECT a FROM t1 GROUP BY a;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL a 5 NULL 6 Using index
SELECT a FROM t1 GROUP BY a;
a
NULL
1
2
3
4
DROP TABLE t1;
2007-04-10 15:55:48 +02:00
CREATE TABLE t1 (a INT, b INT);
INSERT INTO t1 VALUES(1,1),(1,2),(1,3);
SELECT DISTINCT a, b FROM t1;
a b
1 1
1 2
1 3
SELECT DISTINCT a, a, b FROM t1;
a a b
1 1 1
1 1 2
1 1 3
DROP TABLE t1;
End of 5.0 tests
2008-03-26 17:37:36 +01:00
CREATE TABLE t1(a INT, b INT, c INT, d INT, e INT,
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;
id select_type table type possible_keys key key_len ref rows Extra
2013-08-21 09:51:08 +02:00
1 SIMPLE t1 range NULL a 16 NULL 7 Using index for group-by
2008-03-26 17:37:36 +01:00
SELECT DISTINCT a, b, d, c FROM t1;
a b d c
1 1 0 1
1 1 0 2
1 1 0 3
1 2 0 1
1 2 0 2
1 2 0 3
DROP TABLE t1;
2009-09-05 22:42:17 +02:00
#
# Bug #46159: simple query that never returns
#
SET @old_max_heap_table_size = @@max_heap_table_size;
SET @@max_heap_table_size = 16384;
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');
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;
SELECT c1, c2, COUNT(*) FROM t1 GROUP BY c1 LIMIT 4;
c1 c2 COUNT(*)
1 1 2
2 2 1
3 1 2
4 4 1
SELECT DISTINCT c2 FROM t1 GROUP BY c1 HAVING COUNT(*) > 1;
c2
1
5
DROP TABLE t1;
SET @@sort_buffer_size = @old_sort_buffer_size;
SET @@max_heap_table_size = @old_max_heap_table_size;
2008-03-26 17:37:36 +01:00
End of 5.1 tests
wl#173 - temporal types with sub-second resolution
and collateral changes.
* introduce my_hrtime_t, my_timediff_t, and conversion macros
* inroduce TIME_RESULT, but it can only be returned from Item::cmp_type(),
never from Item::result_type()
* pack_time/unpack_time function for "packed" representation of
MYSQL_TIME in a longlong that can be compared
* ADDTIME()/SUBTIME()/+- INTERVAL now work with TIME values
* numbers aren't quoted in EXPLAIN EXTENDED
* new column I_S.COLUMNS.DATETIME_PRECISION
* date/time values are compares to anything as date/time, not as strings or numbers.
* old timestamp(X) is no longer supported
* MYSQL_TIME to string conversion functions take precision as an argument
* unified the warnings from Field_timestamp/datetime/time/date/newdate store methods
* Field_timestamp_hires, Field_datetime_hires, Field_time_hires
* Field_temporal
* Lazy_string class to pass a value (string, number, time) polymorphically down the stack
* make_truncated_value_warning and Field::set_datetime_warning use Lazy_string as an argument, removed char*/int/double variants
* removed Field::can_be_compared_as_longlong(). Use Field::cmp_type() == INT_RESULT instead
* introduced Item::cmp_result() instead of Item::is_datetime() and Item::result_as_longlong()
* in many cases date/time types are treated like other types, not as special cases
* greatly simplified Arg_comparator (regarding date/time/year code)
* SEC_TO_TIME is real function, not integer.
* microsecond precision in NOW, CURTIME, etc
* Item_temporal. All items derived from it only provide get_date, but no val* methods
* replication of NOW(6)
* Protocol::store(time) now takes the precision as an argument
* @@TIMESTAMP is a double
client/mysqlbinlog.cc:
remove unneded casts
include/my_sys.h:
introduce my_hrtime_t, my_timediff_t, and conversion macros
include/my_time.h:
pack_time/unpack_time, etc.
convenience functions to work with MYSQL_TIME::second_part
libmysql/libmysql.c:
str_to_time() is gone. str_to_datetime() does it now.
my_TIME_to_str() takes the precision as an argument
mysql-test/include/ps_conv.inc:
time is not equal to datetime anymore
mysql-test/r/distinct.result:
a test for an old MySQL bug
mysql-test/r/explain.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/func_default.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/func_sapdb.result:
when decimals=NOT_FIXED_DEC it means "not fixed" indeed
mysql-test/r/func_test.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/func_time.result:
ADDTIME()/SUBTIME()/+- INTERVAL now work with TIME values
mysql-test/r/having.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/information_schema.result:
new column I_S.COLUMNS.DATETIME_PRECISION
mysql-test/r/join_outer.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/metadata.result:
TIMESTAMP no longer has zerofill flag
mysql-test/r/range.result:
invalid datetime is not compared with as a string
mysql-test/r/select.result:
NO_ZERO_IN_DATE, etc only affect storage - according to the manual
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/subselect.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/sysdate_is_now.result:
when decimals=NOT_FIXED_DEC it means "not fixed" indeed
mysql-test/r/type_blob.result:
TIMESTAMP(N) is not deprecated
mysql-test/r/type_timestamp.result:
old TIMESTAMP(X) semantics is not supported anymore
mysql-test/r/union.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/varbinary.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/t/distinct.test:
test for an old MySQL bug
mysql-test/t/func_time.test:
+- INTERVAL now works with TIME values
mysql-test/t/select.test:
typo
mysql-test/t/subselect.test:
only one error per statement, please
mysql-test/t/system_mysql_db_fix40123.test:
old timestamp(X) is no longer supported
mysql-test/t/system_mysql_db_fix50030.test:
old timestamp(X) is no longer supported
mysql-test/t/system_mysql_db_fix50117.test:
old timestamp(X) is no longer supported
mysql-test/t/type_blob.test:
old timestamp(X) is no longer supported
mysql-test/t/type_timestamp.test:
old timestamp(X) is no longer supported
mysys/my_getsystime.c:
functions to get the time with microsecond precision
mysys/my_init.c:
move the my_getsystime.c initialization code to my_getsystime.c
mysys/my_static.c:
no need to make these variables extern
mysys/my_static.h:
no need to make these variables extern
scripts/mysql_system_tables.sql:
old timestamp(X) is no longer supported
scripts/mysql_system_tables_fix.sql:
old timestamp(X) is no longer supported
scripts/mysqlhotcopy.sh:
old timestamp(X) is no longer supported
sql-common/my_time.c:
* call str_to_time from str_to_datetime, as appropriate
* date/time to string conversions take precision as an argument
* number_to_time()
* TIME_to_double()
* pack_time() and unpack_time()
sql/event_data_objects.cc:
cast is not needed
my_datetime_to_str() takes precision as an argument
sql/event_db_repository.cc:
avoid dangerous downcast (because the pointer is
not always Field_timestamp, see events_1.test)
sql/event_queue.cc:
avoid silly double-work for cond_wait
(having an endpoint of wait, subtract the current time to get the timeout,
and use set_timespec() macro to fill in struct timespec, by adding the current
time to the timeout)
sql/field.cc:
* remove virtual Field::get_time(), everyone should use only Field::get_date()
* remove lots of #ifdef WORDS_BIGENDIAN
* unified the warnings from Field_timestamp/datetime/time/date/newdate store methods
* Field_timestamp_hires, Field_datetime_hires, Field_time_hires
* Field_temporal
* make_truncated_value_warning and Field::set_datetime_warning use Lazy_string as an argument, removed char*/int/double variants
sql/field.h:
* remove virtual Field::get_time(), everyone should use only Field::get_date()
* remove lots of #ifdef WORDS_BIGENDIAN
* unified the warnings from Field_timestamp/datetime/time/date/newdate store methods
* Field_timestamp_hires, Field_datetime_hires, Field_time_hires
* Field_temporal
* make_truncated_value_warning and Field::set_datetime_warning use Lazy_string as an argument, removed char*/int/double variants
* removed Field::can_be_compared_as_longlong(). Use Field::cmp_type() == INT_RESULT instead
sql/filesort.cc:
TIME_RESULT, cmp_time()
sql/item.cc:
* numbers aren't quoted in EXPLAIN EXTENDED
* Item::cmp_result() instead of Item::is_datetime() and Item::result_as_longlong()
* virtual Item::get_time() is gone
* Item_param::field_type() is set correctly
* Item_datetime, for a datetime constant
* time to anything is compared as a time
* Item_cache::print() prints the value is available
* bug fixed in Item_cache_int::val_str()
sql/item.h:
* Item::print_value(), to be used from Item_xxx::print() when needed
* Item::cmp_result() instead of Item::is_datetime() and Item::result_as_longlong()
* virtual Item::get_time() is gone
* Item_datetime, for a datetime constant
* better default for cast_to_int_type()
* Item_cache objects now *always* have the field_type() set
sql/item_cmpfunc.cc:
* get_year_value, get_time_value are gone. get_datetime_value does it all
* get_value_a_func, get_value_b_func are gone
* can_compare_as_dates() is gone too, TIME_RESULT is used instead
* cmp_type() instead or result_type() when doing a comparison
* compare_datetime and compate_e_datetime in the comparator_matrix, is_nulls_eq is gone
* Item::cmp_result() instead of Item::is_datetime() and Item::result_as_longlong()
sql/item_cmpfunc.h:
greatly simplified Arg_comparator
sql/item_create.cc:
* fix a bug in error messages in CAST
sql/item_func.cc:
Item::cmp_result() instead of Item::is_datetime() and Item::result_as_longlong()
mention all possibitiles in switch over Item_result values, or use default:
sql/item_row.h:
overwrite the default cmp_type() for Item_row,
as no MYSQL_TYPE_xxx value corresponds to ROW_RESULT
sql/item_timefunc.cc:
rewrite make_datetime to support precision argument
SEC_TO_TIME is real function, not integer.
many functions that returned temporal values had duplicate code in val_* methods,
some of them did not have get_date() which resulted in unnecessary date->str->date conversions.
Now they all are derived from Item_temporal_func and *only* provide get_date, not val* methods.
many fixes to set decimals (datetime precision) correctly.
sql/item_timefunc.h:
SEC_TO_TIME is real function, not integer.
many functions that returned temporal values had duplicate code in val_* methods,
some of them did not have get_date() which resulted in unnecessary date->str->date conversions.
Now they all are derived from Item_temporal_func and *only* provide get_date, not val* methods.
many fixes to set decimals (datetime precision) correctly.
sql/log_event.cc:
replication of NOW(6)
sql/log_event.h:
replication of NOW(6)
sql/mysql_priv.h:
Lazy_string class to pass a value (string, number, time) polymorphically down the stack.
make_truncated_value_warning() that uses it.
sql/mysqld.cc:
datetime in Arg_comparator::comparator_matrix
sql/opt_range.cc:
cleanup: don't disable warnings before calling save_in_field_no_warnings()
sql/protocol.cc:
Protocol::store(time) now takes the precision as an argument
sql/protocol.h:
Protocol::store(time) now takes the precision as an argument
sql/rpl_rli.cc:
small cleanup
sql/set_var.cc:
SET TIMESTAMP=double
sql/set_var.h:
@@TIMESTAMP is a double
sql/share/errmsg.txt:
precision and scale are unsigned
sql/slave.cc:
replication of NOW(6)
sql/sp_head.cc:
cleanup
sql/sql_class.cc:
support for NOW(6)
sql/sql_class.h:
support for NOW(6)
sql/sql_insert.cc:
support for NOW(6)
sql/sql_select.cc:
use item->cmp_type().
move a comment where it belongs
sql/sql_show.cc:
new column I_S.COLUMNS.DATETIME_PRECISION
sql/sql_yacc.yy:
TIME(X), DATETIME(X), cast, NOW(X), CURTIME(X), etc
sql/time.cc:
fix date_add_interval() to support MYSQL_TIMESTAMP_TIME argument
storage/myisam/ha_myisam.cc:
TIMESTAMP no longer carries ZEROFIELD flag, still we keep MYI file compatible.
strings/my_vsnprintf.c:
warnings
tests/mysql_client_test.c:
old timestamp(X) does not work anymore
datetime is no longer equal to time
2011-03-01 13:24:36 +01:00
create table t1 (a varchar(100));
insert t1 values ('2010-10-10'), ('20101010');
select * from t1 where a = DATE('2010-10-10');
a
2010-10-10
20101010
select distinct a from t1 where a = DATE('2010-10-10');
a
2010-10-10
20101010
2011-06-10 10:14:20 +02:00
explain select distinct a from t1 where a = DATE('2010-10-10');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using where; Using temporary
drop table t1;
# 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';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using where
drop table t1;
# double = string
create table t1 (a double);
insert t1 values (2), (2);
explain select distinct a from t1 where a = '2';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using where
# double = int
explain select distinct a from t1 where a = 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using where
# string = double
alter table t1 modify a varchar(100);
explain select distinct a from t1 where a = 2e0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using where; Using temporary
wl#173 - temporal types with sub-second resolution
and collateral changes.
* introduce my_hrtime_t, my_timediff_t, and conversion macros
* inroduce TIME_RESULT, but it can only be returned from Item::cmp_type(),
never from Item::result_type()
* pack_time/unpack_time function for "packed" representation of
MYSQL_TIME in a longlong that can be compared
* ADDTIME()/SUBTIME()/+- INTERVAL now work with TIME values
* numbers aren't quoted in EXPLAIN EXTENDED
* new column I_S.COLUMNS.DATETIME_PRECISION
* date/time values are compares to anything as date/time, not as strings or numbers.
* old timestamp(X) is no longer supported
* MYSQL_TIME to string conversion functions take precision as an argument
* unified the warnings from Field_timestamp/datetime/time/date/newdate store methods
* Field_timestamp_hires, Field_datetime_hires, Field_time_hires
* Field_temporal
* Lazy_string class to pass a value (string, number, time) polymorphically down the stack
* make_truncated_value_warning and Field::set_datetime_warning use Lazy_string as an argument, removed char*/int/double variants
* removed Field::can_be_compared_as_longlong(). Use Field::cmp_type() == INT_RESULT instead
* introduced Item::cmp_result() instead of Item::is_datetime() and Item::result_as_longlong()
* in many cases date/time types are treated like other types, not as special cases
* greatly simplified Arg_comparator (regarding date/time/year code)
* SEC_TO_TIME is real function, not integer.
* microsecond precision in NOW, CURTIME, etc
* Item_temporal. All items derived from it only provide get_date, but no val* methods
* replication of NOW(6)
* Protocol::store(time) now takes the precision as an argument
* @@TIMESTAMP is a double
client/mysqlbinlog.cc:
remove unneded casts
include/my_sys.h:
introduce my_hrtime_t, my_timediff_t, and conversion macros
include/my_time.h:
pack_time/unpack_time, etc.
convenience functions to work with MYSQL_TIME::second_part
libmysql/libmysql.c:
str_to_time() is gone. str_to_datetime() does it now.
my_TIME_to_str() takes the precision as an argument
mysql-test/include/ps_conv.inc:
time is not equal to datetime anymore
mysql-test/r/distinct.result:
a test for an old MySQL bug
mysql-test/r/explain.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/func_default.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/func_sapdb.result:
when decimals=NOT_FIXED_DEC it means "not fixed" indeed
mysql-test/r/func_test.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/func_time.result:
ADDTIME()/SUBTIME()/+- INTERVAL now work with TIME values
mysql-test/r/having.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/information_schema.result:
new column I_S.COLUMNS.DATETIME_PRECISION
mysql-test/r/join_outer.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/metadata.result:
TIMESTAMP no longer has zerofill flag
mysql-test/r/range.result:
invalid datetime is not compared with as a string
mysql-test/r/select.result:
NO_ZERO_IN_DATE, etc only affect storage - according to the manual
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/subselect.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/sysdate_is_now.result:
when decimals=NOT_FIXED_DEC it means "not fixed" indeed
mysql-test/r/type_blob.result:
TIMESTAMP(N) is not deprecated
mysql-test/r/type_timestamp.result:
old TIMESTAMP(X) semantics is not supported anymore
mysql-test/r/union.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/r/varbinary.result:
numbers aren't quoted in EXPLAIN EXTENDED
mysql-test/t/distinct.test:
test for an old MySQL bug
mysql-test/t/func_time.test:
+- INTERVAL now works with TIME values
mysql-test/t/select.test:
typo
mysql-test/t/subselect.test:
only one error per statement, please
mysql-test/t/system_mysql_db_fix40123.test:
old timestamp(X) is no longer supported
mysql-test/t/system_mysql_db_fix50030.test:
old timestamp(X) is no longer supported
mysql-test/t/system_mysql_db_fix50117.test:
old timestamp(X) is no longer supported
mysql-test/t/type_blob.test:
old timestamp(X) is no longer supported
mysql-test/t/type_timestamp.test:
old timestamp(X) is no longer supported
mysys/my_getsystime.c:
functions to get the time with microsecond precision
mysys/my_init.c:
move the my_getsystime.c initialization code to my_getsystime.c
mysys/my_static.c:
no need to make these variables extern
mysys/my_static.h:
no need to make these variables extern
scripts/mysql_system_tables.sql:
old timestamp(X) is no longer supported
scripts/mysql_system_tables_fix.sql:
old timestamp(X) is no longer supported
scripts/mysqlhotcopy.sh:
old timestamp(X) is no longer supported
sql-common/my_time.c:
* call str_to_time from str_to_datetime, as appropriate
* date/time to string conversions take precision as an argument
* number_to_time()
* TIME_to_double()
* pack_time() and unpack_time()
sql/event_data_objects.cc:
cast is not needed
my_datetime_to_str() takes precision as an argument
sql/event_db_repository.cc:
avoid dangerous downcast (because the pointer is
not always Field_timestamp, see events_1.test)
sql/event_queue.cc:
avoid silly double-work for cond_wait
(having an endpoint of wait, subtract the current time to get the timeout,
and use set_timespec() macro to fill in struct timespec, by adding the current
time to the timeout)
sql/field.cc:
* remove virtual Field::get_time(), everyone should use only Field::get_date()
* remove lots of #ifdef WORDS_BIGENDIAN
* unified the warnings from Field_timestamp/datetime/time/date/newdate store methods
* Field_timestamp_hires, Field_datetime_hires, Field_time_hires
* Field_temporal
* make_truncated_value_warning and Field::set_datetime_warning use Lazy_string as an argument, removed char*/int/double variants
sql/field.h:
* remove virtual Field::get_time(), everyone should use only Field::get_date()
* remove lots of #ifdef WORDS_BIGENDIAN
* unified the warnings from Field_timestamp/datetime/time/date/newdate store methods
* Field_timestamp_hires, Field_datetime_hires, Field_time_hires
* Field_temporal
* make_truncated_value_warning and Field::set_datetime_warning use Lazy_string as an argument, removed char*/int/double variants
* removed Field::can_be_compared_as_longlong(). Use Field::cmp_type() == INT_RESULT instead
sql/filesort.cc:
TIME_RESULT, cmp_time()
sql/item.cc:
* numbers aren't quoted in EXPLAIN EXTENDED
* Item::cmp_result() instead of Item::is_datetime() and Item::result_as_longlong()
* virtual Item::get_time() is gone
* Item_param::field_type() is set correctly
* Item_datetime, for a datetime constant
* time to anything is compared as a time
* Item_cache::print() prints the value is available
* bug fixed in Item_cache_int::val_str()
sql/item.h:
* Item::print_value(), to be used from Item_xxx::print() when needed
* Item::cmp_result() instead of Item::is_datetime() and Item::result_as_longlong()
* virtual Item::get_time() is gone
* Item_datetime, for a datetime constant
* better default for cast_to_int_type()
* Item_cache objects now *always* have the field_type() set
sql/item_cmpfunc.cc:
* get_year_value, get_time_value are gone. get_datetime_value does it all
* get_value_a_func, get_value_b_func are gone
* can_compare_as_dates() is gone too, TIME_RESULT is used instead
* cmp_type() instead or result_type() when doing a comparison
* compare_datetime and compate_e_datetime in the comparator_matrix, is_nulls_eq is gone
* Item::cmp_result() instead of Item::is_datetime() and Item::result_as_longlong()
sql/item_cmpfunc.h:
greatly simplified Arg_comparator
sql/item_create.cc:
* fix a bug in error messages in CAST
sql/item_func.cc:
Item::cmp_result() instead of Item::is_datetime() and Item::result_as_longlong()
mention all possibitiles in switch over Item_result values, or use default:
sql/item_row.h:
overwrite the default cmp_type() for Item_row,
as no MYSQL_TYPE_xxx value corresponds to ROW_RESULT
sql/item_timefunc.cc:
rewrite make_datetime to support precision argument
SEC_TO_TIME is real function, not integer.
many functions that returned temporal values had duplicate code in val_* methods,
some of them did not have get_date() which resulted in unnecessary date->str->date conversions.
Now they all are derived from Item_temporal_func and *only* provide get_date, not val* methods.
many fixes to set decimals (datetime precision) correctly.
sql/item_timefunc.h:
SEC_TO_TIME is real function, not integer.
many functions that returned temporal values had duplicate code in val_* methods,
some of them did not have get_date() which resulted in unnecessary date->str->date conversions.
Now they all are derived from Item_temporal_func and *only* provide get_date, not val* methods.
many fixes to set decimals (datetime precision) correctly.
sql/log_event.cc:
replication of NOW(6)
sql/log_event.h:
replication of NOW(6)
sql/mysql_priv.h:
Lazy_string class to pass a value (string, number, time) polymorphically down the stack.
make_truncated_value_warning() that uses it.
sql/mysqld.cc:
datetime in Arg_comparator::comparator_matrix
sql/opt_range.cc:
cleanup: don't disable warnings before calling save_in_field_no_warnings()
sql/protocol.cc:
Protocol::store(time) now takes the precision as an argument
sql/protocol.h:
Protocol::store(time) now takes the precision as an argument
sql/rpl_rli.cc:
small cleanup
sql/set_var.cc:
SET TIMESTAMP=double
sql/set_var.h:
@@TIMESTAMP is a double
sql/share/errmsg.txt:
precision and scale are unsigned
sql/slave.cc:
replication of NOW(6)
sql/sp_head.cc:
cleanup
sql/sql_class.cc:
support for NOW(6)
sql/sql_class.h:
support for NOW(6)
sql/sql_insert.cc:
support for NOW(6)
sql/sql_select.cc:
use item->cmp_type().
move a comment where it belongs
sql/sql_show.cc:
new column I_S.COLUMNS.DATETIME_PRECISION
sql/sql_yacc.yy:
TIME(X), DATETIME(X), cast, NOW(X), CURTIME(X), etc
sql/time.cc:
fix date_add_interval() to support MYSQL_TIMESTAMP_TIME argument
storage/myisam/ha_myisam.cc:
TIMESTAMP no longer carries ZEROFIELD flag, still we keep MYI file compatible.
strings/my_vsnprintf.c:
warnings
tests/mysql_client_test.c:
old timestamp(X) does not work anymore
datetime is no longer equal to time
2011-03-01 13:24:36 +01:00
drop table t1;
2011-03-08 19:41:58 +01:00
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 ;
time(f1)
00:00:00.000100
00:00:00.000200
00:00:00.000300
select distinct time(f1) from t1 ;
time(f1)
00:00:00.000100
00:00:00.000200
00:00:00.000300
drop table t1;
2013-01-26 22:33:18 +01:00
create table t1(i int, g int);
insert into t1 values (null, 1), (0, 2);
select distinct i from t1 group by g;
i
NULL
0
drop table t1;
create table t1(i int, g blob);
insert into t1 values (null, 1), (0, 2);
select distinct i from t1 group by g;
i
NULL
0
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;
field1
NULL
0
7
select distinct (select max(a) from t1 where alias.b = a) as field1 from t2 as alias group by field1;
field1
NULL
0
7
drop view v;
drop table t1, t2;
2013-10-03 02:59:56 +02:00
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;
COUNT(DISTINCT t1.id)
18
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;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 96 100.00 Using where; Using temporary
1 PRIMARY <derived2> ref key0 key0 5 test.t1.i1 9 100.00 Using where; Distinct
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where; Distinct; Using join buffer (flat, BNL join)
2 DERIVED t1 ALL NULL NULL NULL NULL 96 100.00
Warnings:
Note 1003 select straight_join distinct `test`.`t1`.`id` AS `id` from `test`.`t1` join `test`.`v1` join `test`.`t2` where ((`test`.`t2`.`i` = `v1`.`id`) and (`v1`.`i1` = `test`.`t1`.`i1`) and (`v1`.`id` <> 3))
set join_buffer_size=1024;
SELECT STRAIGHT_JOIN DISTINCT t1.id FROM
t1, v1, t2 WHERE v1.id = t2.i AND t1.i1 = v1.i1 AND t2.i != 3;
id
7
8
9
18
20
24
43
45
50
51
61
64
71
74
77
78
93
94
set join_buffer_size=1024*16;
SELECT STRAIGHT_JOIN DISTINCT t1.id FROM
t1, v1, t2 WHERE v1.id = t2.i AND t1.i1 = v1.i1 AND t2.i != 3;
id
7
9
18
20
24
8
50
51
61
43
45
71
64
74
77
78
94
93
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;
id
7
9
18
20
24
50
51
61
71
94
8
43
45
64
74
77
78
93
DROP VIEW v1;
DROP TABLE t1,t2;
2011-05-11 13:11:57 +02:00
#
# Bug #11744875: 4082: integer lengths cause truncation with distinct concat and innodb
#
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;
c
11112222
33334444
DROP TABLE t1;
Bug#16539979 - BASIC SELECT COUNT(DISTINCT ID) IS BROKEN
Bug#17867117 - ERROR RESULT WHEN "COUNT + DISTINCT + CASE WHEN" NEED MERGE_WALK
Problem:
COUNT DISTINCT gives incorrect result when it uses a Unique
Tree and its last inserted record has null value.
Here is how COUNT DISTINCT is processed, given that this query is not
using loose index scan.
When a row is produced as a result of joining tables (there is only
one table here), we store the SELECTed value in a Unique tree. This
allows elimination of any duplicates, and thus implements DISTINCT.
When we have processed all rows like this, we walk the Unique tree,
counting its elements, in Aggregator_distinct::endup() (tree->walk());
for each element we call Item_sum_count::add(). Such function wants to
ignore any NULL value, for that it checks item_sum -> args[0] ->
null_value. It is a mistake: when walking the Unique tree, the value
to be aggregated is not item_sum ->args[0] but rather table ->
field[0].
Solution:
instead of item_sum -> args[0] -> null_value, use arg_is_null(), which
knows where to look (like in fix for bug 57932).
As a consequence of this solution, we have to make arg_is_null() a
little more general:
1) Because it was so far only used for AVG() (which always has a
single argument), this function was looking at a single argument; now
that it has to work with COUNT(DISTINCT expression1,expression2), it
must look at all arguments.
2) Because we start using arg_is_null () for COUNT(DISTINCT), i.e. in
Item_sum_count::add (), it implies that we are also using it for
COUNT(no DISTINCT) (same add ()). For COUNT(no DISTINCT), the
nullness to check is that of item_sum -> args[0]. But the null_value
of such item is reliable only if val_*() has been called on it. So far
arg_is_null() was always used after a call to arg_val*(), so could
rely on null_value; but for COUNT, there is no call to arg_val*(), so
arg_is_null() has to call is_null() instead.
Testcase for 16539979 by Neeraj. Testcase for 17867117 contributed by
Xiaobin Lin from Taobao.
2013-12-04 12:32:42 +01:00
#
# Bug#16539979 BASIC SELECT COUNT(DISTINCT ID) IS BROKEN.
# Bug#17867117 ERROR RESULT WHEN "COUNT + DISTINCT + CASE WHEN" NEED MERGE_WALK
#
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;
COUNT(DISTINCT a)
128
SELECT COUNT(DISTINCT (a+0)) FROM t1;
COUNT(DISTINCT (a+0))
128
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;
count(distinct case when id<=64 then id end)
64
select count(distinct case when id<=63 then id end) from tb;
count(distinct case when id<=63 then id end)
63
drop table tb;
SET @@tmp_table_size= @tmp_table_size_save;
2011-05-11 13:11:57 +02:00
End of 5.5 tests