distinct.result 22.4 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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;
19 20 21 22 23 24 25 26 27 28 29 30 31 32
id
NULL
-1
0
1
2
3
4
5
6
7
8
9
10
unknown's avatar
unknown committed
33
select * from t1 order by id;
34 35 36 37 38 39 40 41 42 43 44 45 46 47
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
unknown's avatar
unknown committed
48
select id-5,facility from t1 order by "id-5";
49 50 51 52 53 54 55 56 57 58 59 60 61 62
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
unknown's avatar
unknown committed
63
select id,concat(facility) from t1 group by id ;
64 65 66 67 68 69 70 71 72 73 74 75 76 77
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
unknown's avatar
unknown committed
78
select id+0 as a,max(id),concat(facility) as b from t1 group by a order by b desc,a;
79 80 81 82 83 84 85 86 87 88 89 90 91
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	
unknown's avatar
unknown committed
92
NULL	NULL	NULL
unknown's avatar
unknown committed
93
select id >= 0 and id <= 5 as grp,count(*) from t1 group by grp;
94
grp	count(*)
unknown's avatar
unknown committed
95 96
NULL	1
0	6
97
1	6
unknown's avatar
unknown committed
98
SELECT DISTINCT FACILITY FROM t1;
99 100 101 102 103 104 105 106 107 108 109 110 111
FACILITY
NULL

/L
A01
ANC
F01
FBX
MT
P
RV
SRV
VMT
unknown's avatar
unknown committed
112
SELECT FACILITY FROM t2;
113 114 115 116 117 118 119 120 121 122 123 124 125
FACILITY
NULL

/L
A01
ANC
F01
FBX
MT
P
RV
SRV
VMT
unknown's avatar
unknown committed
126
SELECT count(*) from t1,t2 where t1.facility=t2.facility;
127 128
count(*)
12
unknown's avatar
unknown committed
129
select count(facility) from t1;
130 131
count(facility)
12
unknown's avatar
unknown committed
132
select count(*) from t1;
133 134
count(*)
13
unknown's avatar
unknown committed
135
select count(*) from t1 where facility IS NULL;
136 137
count(*)
1
unknown's avatar
unknown committed
138
select count(*) from t1 where facility = NULL;
139 140
count(*)
0
unknown's avatar
unknown committed
141
select count(*) from t1 where facility IS NOT NULL;
142 143
count(*)
12
unknown's avatar
unknown committed
144
select count(*) from t1 where id IS NULL;
145 146
count(*)
1
unknown's avatar
unknown committed
147
select count(*) from t1 where id IS NOT NULL;
148 149
count(*)
12
unknown's avatar
unknown committed
150 151 152 153 154
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;
155
UserId
unknown's avatar
unknown committed
156
SELECT UserId FROM t1 WHERE UserId=22 group by Userid;
157
UserId
unknown's avatar
unknown committed
158
SELECT DISTINCT UserId FROM t1 WHERE UserId=22 group by Userid;
159
UserId
unknown's avatar
unknown committed
160
SELECT DISTINCT UserId FROM t1 WHERE UserId=22;
161
UserId
unknown's avatar
unknown committed
162 163
drop table t1;
CREATE TABLE t1 (a int(10) unsigned not null primary key,b int(10) unsigned);
164
INSERT INTO t1 VALUES (1,1),(2,1),(3,1),(4,1);
unknown's avatar
unknown committed
165 166 167 168 169
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;
170 171
b
1
unknown's avatar
unknown committed
172 173 174
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;
unknown's avatar
unknown committed
175
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
unknown's avatar
unknown committed
176
1	SIMPLE	t1	ALL	PRIMARY	NULL	NULL	NULL	4	Using temporary
unknown's avatar
unknown committed
177 178
1	SIMPLE	t3	ref	a	a	5	test.t1.b	2	Using where; Using index
1	SIMPLE	t2	index	a	a	4	NULL	5	Using where; Using index; Distinct; Using join buffer
unknown's avatar
unknown committed
179
SELECT distinct t3.a FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a;
180 181
a
1
unknown's avatar
unknown committed
182 183 184 185 186 187 188 189 190
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;
unknown's avatar
unknown committed
191
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
unknown's avatar
unknown committed
192
1	SIMPLE	t1	index	PRIMARY	PRIMARY	4	NULL	4	Using index; Using temporary
193
1	SIMPLE	t3	ref	a	a	5	test.t1.a	11	Using where; Using index; Distinct
unknown's avatar
unknown committed
194
select distinct t1.a from t1,t3 where t1.a=t3.a;
195 196 197
a
1
2
unknown's avatar
unknown committed
198
select distinct 1 from t1,t3 where t1.a=t3.a;
199 200
1
1
201
explain SELECT distinct t1.a from t1;
unknown's avatar
unknown committed
202
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
203
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	4	Using index
204
explain SELECT distinct t1.a from t1 order by a desc;
unknown's avatar
unknown committed
205
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
206
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	4	Using index
207
explain SELECT t1.a from t1 group by a order by a desc;
unknown's avatar
unknown committed
208
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
209
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	4	Using index
210
explain SELECT distinct t1.a from t1 order by a desc limit 1;
unknown's avatar
unknown committed
211
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
unknown's avatar
unknown committed
212
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	1	Using index
213
explain SELECT distinct a from t3 order by a desc limit 2;
unknown's avatar
unknown committed
214
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
unknown's avatar
unknown committed
215
1	SIMPLE	t3	index	NULL	a	5	NULL	40	Using index
216
explain SELECT distinct a,b from t3 order by a+1;
unknown's avatar
unknown committed
217 218
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
unknown's avatar
unknown committed
219
explain SELECT distinct a,b from t3 order by a limit 2;
unknown's avatar
unknown committed
220
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
unknown's avatar
unknown committed
221
1	SIMPLE	t3	index	NULL	a	5	NULL	2	Using temporary
222
explain SELECT a,b from t3 group by a,b order by a+1;
unknown's avatar
unknown committed
223 224
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
unknown's avatar
unknown committed
225 226 227 228
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;
229 230 231
name
aa
ab
unknown's avatar
unknown committed
232
SELECT DISTINCT name FROM t1 LIMIT 2;
233 234 235
name
aa
ab
unknown's avatar
unknown committed
236
SELECT DISTINCT 1 FROM t1 LIMIT 2;
237 238
1
1
unknown's avatar
unknown committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
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;
262 263
key_link_id	link
NULL	NULL
unknown's avatar
unknown committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
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);
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));
unknown's avatar
unknown committed
301
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
unknown's avatar
unknown committed
302 303 304 305 306 307
1	SIMPLE	t1	index	id	id	4	NULL	2	Using index; Using temporary
1	SIMPLE	t2	index	id	id	8	NULL	1	Using index; Distinct; Using join buffer
1	SIMPLE	t3	index	id	id	8	NULL	1	Using index; Distinct; Using join buffer
1	SIMPLE	j_lj_t2	index	id	id	4	NULL	2	Using where; Using index; Distinct; Using join buffer
1	SIMPLE	t2_lj	ref	id	id	4	test.j_lj_t2.id	1	Using where; Using index; Distinct
1	SIMPLE	j_lj_t3	index	id	id	4	NULL	2	Using where; Using index; Distinct; Using join buffer
unknown's avatar
unknown committed
308
1	SIMPLE	t3_lj	ref	id	id	4	test.j_lj_t3.id	1	Using where; Using index; Distinct
unknown's avatar
unknown committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
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));
326 327
id
2
unknown's avatar
unknown committed
328 329 330 331
drop table t1,t2,t3;
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;
unknown's avatar
unknown committed
332 333 334 335
a	sec_to_time(sum(time_to_sec(t)))
1	00:06:15
1	00:36:30
1	00:36:30
unknown's avatar
unknown committed
336
select distinct a,sec_to_time(sum(time_to_sec(t))) from t1 group by a,b;
unknown's avatar
unknown committed
337 338 339
a	sec_to_time(sum(time_to_sec(t)))
1	00:06:15
1	00:36:30
unknown's avatar
unknown committed
340 341 342
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;
unknown's avatar
unknown committed
343 344 345 346
a	sec_to_time(sum(time_to_sec(t)))
1	00:06:15
1	00:36:30
1	00:36:30
unknown's avatar
unknown committed
347
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;
unknown's avatar
unknown committed
348 349 350
a	sec_to_time(sum(time_to_sec(t)))
1	00:06:15
1	00:36:30
unknown's avatar
unknown committed
351 352 353 354
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;
355 356 357
a
4
3
unknown's avatar
unknown committed
358
select distinct a,c from t1 group by b,c,a having a > 2 order by a desc;
359 360 361
a	c
4	NULL
3	NULL
unknown's avatar
unknown committed
362
drop table t1;
unknown's avatar
unknown committed
363
create table t1 (a char(1), key(a)) engine=myisam;
364 365 366 367 368 369 370 371 372 373 374 375
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;
376
CREATE TABLE t1 (email varchar(50), infoID BIGINT, dateentered DATETIME);
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
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
392
test3@testdomain.com	Z001
393 394 395 396 397 398 399 400 401 402 403 404
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;
unknown's avatar
unknown committed
405
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;
unknown's avatar
unknown committed
406
INSERT INTO t1 VALUES (128,0,33,33,8,':D','',996121863,1,0,2,996122850,2,0,0);
unknown's avatar
unknown committed
407
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;
unknown's avatar
unknown committed
408 409
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);
410 411
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
412
DROP TABLE t1,t2;
413 414 415 416 417 418 419
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
420
DROP TABLE t1,t2;
unknown's avatar
unknown committed
421
CREATE table t1 (  `id` int(11) NOT NULL auto_increment,  `name` varchar(50) NOT NULL default '',  PRIMARY KEY  (`id`)) ENGINE=MyISAM AUTO_INCREMENT=3 ;
422 423 424 425 426 427 428 429
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;
unknown's avatar
unknown committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
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',
unknown's avatar
unknown committed
460
rout int(11) default '0'
unknown's avatar
unknown committed
461
) ENGINE=MyISAM;
unknown's avatar
unknown committed
462
INSERT INTO t1 VALUES ('1',1,0);
unknown's avatar
unknown committed
463
SELECT DISTINCT html,SUM(rout)/(SUM(rin)+1) as 'prod' FROM t1 GROUP BY rin;
unknown's avatar
unknown committed
464
html	prod
465
1	0.0000
unknown's avatar
unknown committed
466
drop table t1;
unknown's avatar
unknown committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
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;
507 508 509 510 511 512 513 514 515 516
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
unknown's avatar
unknown committed
517 518
1	SIMPLE	t1_1	ALL	NULL	NULL	NULL	NULL	3	Using temporary
1	SIMPLE	t1_2	index	NULL	PRIMARY	4	NULL	3	Using index; Distinct; Using join buffer
519 520 521 522 523 524 525 526 527 528 529 530 531 532
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
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	
EXPLAIN SELECT DISTINCT a,b FROM t1 GROUP BY a,b;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	
533 534
CREATE TABLE t2(a INT, b INT NOT NULL, c INT NOT NULL, d INT, 
PRIMARY KEY (a,b));
535 536 537 538 539 540
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
unknown's avatar
unknown committed
541
1	SIMPLE	t2	index	NULL	PRIMARY	8	NULL	3	Using index
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
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
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	3	
DROP TABLE t1,t2;
559 560 561 562 563 564 565 566
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;
567 568 569 570 571 572 573 574 575 576 577
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;
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
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;
599 600 601 602 603 604 605 606 607 608 609 610
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;
unknown's avatar
unknown committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
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
636
2	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	2	Using where
unknown's avatar
unknown committed
637 638 639 640 641 642 643 644 645 646 647
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;
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
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;
unknown's avatar
unknown committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684
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