~stewart/drizzle/embedded-innodb-create-select-transaction-arrgh

« back to all changes in this revision

Viewing changes to mysql-test/r/distinct.result

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
drop table if exists t1,t2,t3;
 
2
CREATE TABLE t1 (id int,facility char(20));
 
3
CREATE TABLE t2 (facility char(20));
 
4
INSERT INTO t1 VALUES (NULL,NULL);
 
5
INSERT INTO t1 VALUES (-1,'');
 
6
INSERT INTO t1 VALUES (0,'');
 
7
INSERT INTO t1 VALUES (1,'/L');
 
8
INSERT INTO t1 VALUES (2,'A01');
 
9
INSERT INTO t1 VALUES (3,'ANC');
 
10
INSERT INTO t1 VALUES (4,'F01');
 
11
INSERT INTO t1 VALUES (5,'FBX');
 
12
INSERT INTO t1 VALUES (6,'MT');
 
13
INSERT INTO t1 VALUES (7,'P');
 
14
INSERT INTO t1 VALUES (8,'RV');
 
15
INSERT INTO t1 VALUES (9,'SRV');
 
16
INSERT INTO t1 VALUES (10,'VMT');
 
17
INSERT INTO t2 SELECT DISTINCT FACILITY FROM t1;
 
18
select id from t1 group by id;
 
19
id
 
20
NULL
 
21
-1
 
22
0
 
23
1
 
24
2
 
25
3
 
26
4
 
27
5
 
28
6
 
29
7
 
30
8
 
31
9
 
32
10
 
33
select * from t1 order by id;
 
34
id      facility
 
35
NULL    NULL
 
36
-1      
 
37
0       
 
38
1       /L
 
39
2       A01
 
40
3       ANC
 
41
4       F01
 
42
5       FBX
 
43
6       MT
 
44
7       P
 
45
8       RV
 
46
9       SRV
 
47
10      VMT
 
48
select id-5,facility from t1 order by "id-5";
 
49
id-5    facility
 
50
NULL    NULL
 
51
-6      
 
52
-5      
 
53
-4      /L
 
54
-3      A01
 
55
-2      ANC
 
56
-1      F01
 
57
0       FBX
 
58
1       MT
 
59
2       P
 
60
3       RV
 
61
4       SRV
 
62
5       VMT
 
63
select id,concat(facility) from t1 group by id ;
 
64
id      concat(facility)
 
65
NULL    NULL
 
66
-1      
 
67
0       
 
68
1       /L
 
69
2       A01
 
70
3       ANC
 
71
4       F01
 
72
5       FBX
 
73
6       MT
 
74
7       P
 
75
8       RV
 
76
9       SRV
 
77
10      VMT
 
78
select id+0 as a,max(id),concat(facility) as b from t1 group by a order by b desc,a;
 
79
a       max(id) b
 
80
10      10      VMT
 
81
9       9       SRV
 
82
8       8       RV
 
83
7       7       P
 
84
6       6       MT
 
85
5       5       FBX
 
86
4       4       F01
 
87
3       3       ANC
 
88
2       2       A01
 
89
1       1       /L
 
90
-1      -1      
 
91
0       0       
 
92
NULL    NULL    NULL
 
93
select id >= 0 and id <= 5 as grp,count(*) from t1 group by grp;
 
94
grp     count(*)
 
95
NULL    1
 
96
0       6
 
97
1       6
 
98
SELECT DISTINCT FACILITY FROM t1;
 
99
FACILITY
 
100
NULL
 
101
 
 
102
/L
 
103
A01
 
104
ANC
 
105
F01
 
106
FBX
 
107
MT
 
108
P
 
109
RV
 
110
SRV
 
111
VMT
 
112
SELECT FACILITY FROM t2;
 
113
FACILITY
 
114
NULL
 
115
 
 
116
/L
 
117
A01
 
118
ANC
 
119
F01
 
120
FBX
 
121
MT
 
122
P
 
123
RV
 
124
SRV
 
125
VMT
 
126
SELECT count(*) from t1,t2 where t1.facility=t2.facility;
 
127
count(*)
 
128
12
 
129
select count(facility) from t1;
 
130
count(facility)
 
131
12
 
132
select count(*) from t1;
 
133
count(*)
 
134
13
 
135
select count(*) from t1 where facility IS NULL;
 
136
count(*)
 
137
1
 
138
select count(*) from t1 where facility = NULL;
 
139
count(*)
 
140
0
 
141
select count(*) from t1 where facility IS NOT NULL;
 
142
count(*)
 
143
12
 
144
select count(*) from t1 where id IS NULL;
 
145
count(*)
 
146
1
 
147
select count(*) from t1 where id IS NOT NULL;
 
148
count(*)
 
149
12
 
150
drop table t1,t2;
 
151
CREATE TABLE t1 (UserId int(11) DEFAULT '0' NOT NULL);
 
152
INSERT INTO t1 VALUES (20);
 
153
INSERT INTO t1 VALUES (27);
 
154
SELECT UserId FROM t1 WHERE Userid=22;
 
155
UserId
 
156
SELECT UserId FROM t1 WHERE UserId=22 group by Userid;
 
157
UserId
 
158
SELECT DISTINCT UserId FROM t1 WHERE UserId=22 group by Userid;
 
159
UserId
 
160
SELECT DISTINCT UserId FROM t1 WHERE UserId=22;
 
161
UserId
 
162
drop table t1;
 
163
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);
 
165
CREATE TABLE t2 (a int(10) unsigned not null, key (A));
 
166
INSERT INTO t2 VALUES (1),(2);
 
167
CREATE TABLE t3 (a int(10) unsigned, key(A), b text);
 
168
INSERT INTO t3 VALUES (1,'1'),(2,'2');
 
169
SELECT DISTINCT t3.b FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a;
 
170
b
 
171
1
 
172
INSERT INTO t2 values (1),(2),(3);
 
173
INSERT INTO t3 VALUES (1,'1'),(2,'2'),(1,'1'),(2,'2');
 
174
explain SELECT distinct t3.a FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a;
 
175
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
176
1       SIMPLE  t1      ALL     PRIMARY NULL    NULL    NULL    4       Using temporary
 
177
1       SIMPLE  t3      ref     a       a       5       test.t1.b       2       Using index
 
178
1       SIMPLE  t2      index   a       a       4       NULL    5       Using where; Using index; Distinct; Using join buffer
 
179
SELECT distinct t3.a FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a;
 
180
a
 
181
1
 
182
create temporary table t4 select * from t3;
 
183
insert into t3 select * from t4;
 
184
insert into t4 select * from t3;
 
185
insert into t3 select * from t4;
 
186
insert into t4 select * from t3;
 
187
insert into t3 select * from t4;
 
188
insert into t4 select * from t3;
 
189
insert into t3 select * from t4;
 
190
explain select distinct t1.a from t1,t3 where t1.a=t3.a;
 
191
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
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 index; Distinct
 
194
select distinct t1.a from t1,t3 where t1.a=t3.a;
 
195
a
 
196
1
 
197
2
 
198
select distinct 1 from t1,t3 where t1.a=t3.a;
 
199
1
 
200
1
 
201
explain SELECT distinct t1.a from t1;
 
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;
 
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;
 
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;
 
211
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
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;
 
214
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
215
1       SIMPLE  t3      index   NULL    a       5       NULL    40      Using index
 
216
explain SELECT distinct a,b from t3 order by a+1;
 
217
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
218
1       SIMPLE  t3      ALL     NULL    NULL    NULL    NULL    204     Using temporary; Using filesort
 
219
explain SELECT distinct a,b from t3 order by a limit 2;
 
220
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
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;
 
223
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
224
1       SIMPLE  t3      ALL     NULL    NULL    NULL    NULL    204     Using temporary; Using filesort
 
225
drop table t1,t2,t3,t4;
 
226
CREATE TABLE t1 (name varchar(255));
 
227
INSERT INTO t1 VALUES ('aa'),('ab'),('ac'),('ad'),('ae');
 
228
SELECT DISTINCT * FROM t1 LIMIT 2;
 
229
name
 
230
aa
 
231
ab
 
232
SELECT DISTINCT name FROM t1 LIMIT 2;
 
233
name
 
234
aa
 
235
ab
 
236
SELECT DISTINCT 1 FROM t1 LIMIT 2;
 
237
1
 
238
1
 
239
drop table t1;
 
240
CREATE TABLE t1 (
 
241
ID int(11) NOT NULL auto_increment,
 
242
NAME varchar(75) DEFAULT '' NOT NULL,
 
243
LINK_ID int(11) DEFAULT '0' NOT NULL,
 
244
PRIMARY KEY (ID),
 
245
KEY NAME (NAME),
 
246
KEY LINK_ID (LINK_ID)
 
247
);
 
248
INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (1,'Mike',0),(2,'Jack',0),(3,'Bill',0);
 
249
CREATE TABLE t2 (
 
250
ID int(11) NOT NULL auto_increment,
 
251
NAME varchar(150) DEFAULT '' NOT NULL,
 
252
PRIMARY KEY (ID),
 
253
KEY NAME (NAME)
 
254
);
 
255
SELECT DISTINCT
 
256
t2.id AS key_link_id,
 
257
t2.name AS link
 
258
FROM t1
 
259
LEFT JOIN t2 ON t1.link_id=t2.id
 
260
GROUP BY t1.id
 
261
ORDER BY link;
 
262
key_link_id     link
 
263
NULL    NULL
 
264
drop table t1,t2;
 
265
create table t1 (
 
266
id              int not null,
 
267
name    tinytext not null,
 
268
unique  (id)
 
269
);
 
270
create table t2 (
 
271
id              int not null,
 
272
idx             int not null,
 
273
unique  (id, idx)
 
274
);
 
275
create table t3 (
 
276
id              int not null,
 
277
idx             int not null,
 
278
unique  (id, idx)
 
279
);
 
280
insert into t1 values (1,'yes'), (2,'no');
 
281
insert into t2 values (1,1);
 
282
insert into t3 values (1,1);
 
283
EXPLAIN
 
284
SELECT DISTINCT
 
285
t1.id
 
286
from
 
287
t1
 
288
straight_join
 
289
t2
 
290
straight_join
 
291
t3
 
292
straight_join
 
293
t1 as j_lj_t2 left join t2 as t2_lj
 
294
on j_lj_t2.id=t2_lj.id
 
295
straight_join
 
296
t1 as j_lj_t3 left join t3 as t3_lj
 
297
on j_lj_t3.id=t3_lj.id
 
298
WHERE
 
299
((t1.id=j_lj_t2.id AND t2_lj.id IS NULL) OR (t1.id=t2.id AND t2.idx=2))
 
300
AND ((t1.id=j_lj_t3.id AND t3_lj.id IS NULL) OR (t1.id=t3.id AND t3.idx=2));
 
301
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
302
1       SIMPLE  t1      index   id      id      4       NULL    2       Using index; Using temporary
 
303
1       SIMPLE  t2      index   id      id      8       NULL    1       Using index; Distinct; Using join buffer
 
304
1       SIMPLE  t3      index   id      id      8       NULL    1       Using index; Distinct; Using join buffer
 
305
1       SIMPLE  j_lj_t2 index   id      id      4       NULL    2       Using where; Using index; Distinct; Using join buffer
 
306
1       SIMPLE  t2_lj   ref     id      id      4       test.j_lj_t2.id 1       Using where; Using index; Distinct
 
307
1       SIMPLE  j_lj_t3 index   id      id      4       NULL    2       Using where; Using index; Distinct; Using join buffer
 
308
1       SIMPLE  t3_lj   ref     id      id      4       test.j_lj_t3.id 1       Using where; Using index; Distinct
 
309
SELECT DISTINCT
 
310
t1.id
 
311
from
 
312
t1
 
313
straight_join
 
314
t2
 
315
straight_join
 
316
t3
 
317
straight_join
 
318
t1 as j_lj_t2 left join t2 as t2_lj
 
319
on j_lj_t2.id=t2_lj.id
 
320
straight_join
 
321
t1 as j_lj_t3 left join t3 as t3_lj
 
322
on j_lj_t3.id=t3_lj.id
 
323
WHERE
 
324
((t1.id=j_lj_t2.id AND t2_lj.id IS NULL) OR (t1.id=t2.id AND t2.idx=2))
 
325
AND ((t1.id=j_lj_t3.id AND t3_lj.id IS NULL) OR (t1.id=t3.id AND t3.idx=2));
 
326
id
 
327
2
 
328
drop table t1,t2,t3;
 
329
create table t1 (a int not null, b int not null, t time);
 
330
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");
 
331
select a,sec_to_time(sum(time_to_sec(t))) from t1 group by a,b;
 
332
a       sec_to_time(sum(time_to_sec(t)))
 
333
1       00:06:15
 
334
1       00:36:30
 
335
1       00:36:30
 
336
select distinct a,sec_to_time(sum(time_to_sec(t))) from t1 group by a,b;
 
337
a       sec_to_time(sum(time_to_sec(t)))
 
338
1       00:06:15
 
339
1       00:36:30
 
340
create table t2 (a int not null primary key, b int);
 
341
insert into t2 values (1,1),(2,2),(3,3);
 
342
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;
 
343
a       sec_to_time(sum(time_to_sec(t)))
 
344
1       00:06:15
 
345
1       00:36:30
 
346
1       00:36:30
 
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;
 
348
a       sec_to_time(sum(time_to_sec(t)))
 
349
1       00:06:15
 
350
1       00:36:30
 
351
drop table t1,t2;
 
352
create table t1 (a int not null,b char(5), c text);
 
353
insert into t1 (a) values (1),(2),(3),(4),(1),(2),(3),(4);
 
354
select distinct a from t1 group by b,a having a > 2 order by a desc;
 
355
a
 
356
4
 
357
3
 
358
select distinct a,c from t1 group by b,c,a having a > 2 order by a desc;
 
359
a       c
 
360
4       NULL
 
361
3       NULL
 
362
drop table t1;
 
363
create table t1 (a char(1), key(a)) engine=myisam;
 
364
insert into t1 values('1'),('1');
 
365
select * from t1 where a >= '1';
 
366
a
 
367
1
 
368
1
 
369
select distinct a from t1 order by a desc;
 
370
a
 
371
1
 
372
select distinct a from t1 where a >= '1' order by a desc;
 
373
a
 
374
1
 
375
drop table t1;
 
376
CREATE TABLE t1 (email varchar(50), infoID BIGINT, dateentered DATETIME);
 
377
CREATE TABLE t2 (infoID BIGINT, shipcode varchar(10));
 
378
INSERT INTO t1 (email, infoID, dateentered) VALUES
 
379
('test1@testdomain.com', 1, '2002-07-30 22:56:38'),
 
380
('test1@testdomain.com', 1, '2002-07-27 22:58:16'),
 
381
('test2@testdomain.com', 1, '2002-06-19 15:22:19'),
 
382
('test2@testdomain.com', 2, '2002-06-18 14:23:47'),
 
383
('test3@testdomain.com', 1, '2002-05-19 22:17:32');
 
384
INSERT INTO t2(infoID, shipcode) VALUES
 
385
(1, 'Z001'),
 
386
(2, 'R002');
 
387
SELECT DISTINCTROW email, shipcode FROM t1, t2 WHERE t1.infoID=t2.infoID;
 
388
email   shipcode
 
389
test1@testdomain.com    Z001
 
390
test2@testdomain.com    Z001
 
391
test2@testdomain.com    R002
 
392
test3@testdomain.com    Z001
 
393
SELECT DISTINCTROW email FROM t1 ORDER BY dateentered DESC;
 
394
email
 
395
test1@testdomain.com
 
396
test2@testdomain.com
 
397
test3@testdomain.com
 
398
SELECT DISTINCTROW email, shipcode FROM t1, t2 WHERE t1.infoID=t2.infoID ORDER BY dateentered DESC;
 
399
email   shipcode
 
400
test1@testdomain.com    Z001
 
401
test2@testdomain.com    Z001
 
402
test2@testdomain.com    R002
 
403
test3@testdomain.com    Z001
 
404
drop table t1,t2;
 
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;
 
406
INSERT INTO t1 VALUES (128,0,33,33,8,':D','',996121863,1,0,2,996122850,2,0,0);
 
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;
 
408
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);
 
409
SELECT DISTINCT t1.*, t2.* FROM t1 LEFT JOIN t2 ON (t2.userid = t1.touserid);
 
410
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
 
411
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
CREATE TABLE t1 (a int primary key, b int, c int);
 
414
INSERT t1 VALUES (1,2,3);
 
415
CREATE TABLE t2 (a int primary key, b int, c int);
 
416
INSERT t2 VALUES (3,4,5);
 
417
SELECT DISTINCT t1.a, t2.b FROM t1, t2 WHERE t1.a=1 ORDER BY t2.c;
 
418
a       b
 
419
1       4
 
420
DROP TABLE t1,t2;
 
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
INSERT INTO t1 VALUES (1, 'aaaaa');
 
423
INSERT INTO t1 VALUES (3, 'aaaaa');
 
424
INSERT INTO t1 VALUES (2, 'eeeeeee');
 
425
select distinct left(name,1) as name from t1;
 
426
name
 
427
a
 
428
e
 
429
drop  table t1;
 
430
CREATE TABLE t1 (
 
431
ID int(11) NOT NULL auto_increment,
 
432
NAME varchar(75) DEFAULT '' NOT NULL,
 
433
LINK_ID int(11) DEFAULT '0' NOT NULL,
 
434
PRIMARY KEY (ID),
 
435
KEY NAME (NAME),
 
436
KEY LINK_ID (LINK_ID)
 
437
);
 
438
INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (1,'Mike',0);
 
439
INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (2,'Jack',0);
 
440
INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (3,'Bill',0);
 
441
CREATE TABLE t2 (
 
442
ID int(11) NOT NULL auto_increment,
 
443
NAME varchar(150) DEFAULT '' NOT NULL,
 
444
PRIMARY KEY (ID),
 
445
KEY NAME (NAME)
 
446
);
 
447
SELECT DISTINCT
 
448
t2.id AS key_link_id,
 
449
t2.name AS link
 
450
FROM t1
 
451
LEFT JOIN t2 ON t1.link_id=t2.id
 
452
GROUP BY t1.id
 
453
ORDER BY link;
 
454
key_link_id     link
 
455
NULL    NULL
 
456
drop table t1,t2;
 
457
CREATE TABLE t1 (
 
458
html varchar(5) default NULL,
 
459
rin int(11) default '0',
 
460
rout int(11) default '0'
 
461
) ENGINE=MyISAM;
 
462
INSERT INTO t1 VALUES ('1',1,0);
 
463
SELECT DISTINCT html,SUM(rout)/(SUM(rin)+1) as 'prod' FROM t1 GROUP BY rin;
 
464
html    prod
 
465
1       0.0000
 
466
drop table t1;
 
467
CREATE TABLE t1 (a int);
 
468
INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
 
469
SELECT DISTINCT a, 1 FROM t1;
 
470
a       1
 
471
1       1
 
472
2       1
 
473
3       1
 
474
4       1
 
475
5       1
 
476
SELECT DISTINCT 1, a FROM t1;
 
477
1       a
 
478
1       1
 
479
1       2
 
480
1       3
 
481
1       4
 
482
1       5
 
483
CREATE TABLE t2 (a int, b int);
 
484
INSERT INTO t2 VALUES (1,1),(2,2),(2,3),(2,4),(3,5);
 
485
SELECT DISTINCT a, b, 2 FROM t2;
 
486
a       b       2
 
487
1       1       2
 
488
2       2       2
 
489
2       3       2
 
490
2       4       2
 
491
3       5       2
 
492
SELECT DISTINCT 2, a, b FROM t2;
 
493
2       a       b
 
494
2       1       1
 
495
2       2       2
 
496
2       2       3
 
497
2       2       4
 
498
2       3       5
 
499
SELECT DISTINCT a, 2, b FROM t2;
 
500
a       2       b
 
501
1       2       1
 
502
2       2       2
 
503
2       2       3
 
504
2       2       4
 
505
3       2       5
 
506
DROP TABLE t1,t2;
 
507
CREATE TABLE t1(a INT PRIMARY KEY, b INT);
 
508
INSERT INTO t1 VALUES (1,1), (2,1), (3,1);
 
509
EXPLAIN SELECT DISTINCT a FROM t1;
 
510
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
511
1       SIMPLE  t1      index   NULL    PRIMARY 4       NULL    3       Using index
 
512
EXPLAIN SELECT DISTINCT a,b FROM t1;
 
513
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
514
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       
 
515
EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2;
 
516
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
517
1       SIMPLE  t1_1    ALL     NULL    NULL    NULL    NULL    3       Using temporary
 
518
1       SIMPLE  t1_2    index   NULL    PRIMARY 4       NULL    3       Using index; Distinct; Using join buffer
 
519
EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2
 
520
WHERE t1_1.a = t1_2.a;
 
521
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
522
1       SIMPLE  t1_1    ALL     PRIMARY NULL    NULL    NULL    3       Using temporary
 
523
1       SIMPLE  t1_2    eq_ref  PRIMARY PRIMARY 4       test.t1_1.a     1       Using index; Distinct
 
524
EXPLAIN SELECT a FROM t1 GROUP BY a;
 
525
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
526
1       SIMPLE  t1      index   NULL    PRIMARY 4       NULL    3       Using index
 
527
EXPLAIN SELECT a,b FROM t1 GROUP BY a,b;
 
528
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
529
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       Using filesort
 
530
EXPLAIN SELECT DISTINCT a,b FROM t1 GROUP BY a,b;
 
531
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
532
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       Using filesort
 
533
CREATE TABLE t2(a INT, b INT NOT NULL, c INT NOT NULL, d INT, 
 
534
PRIMARY KEY (a,b));
 
535
INSERT INTO t2 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4);
 
536
EXPLAIN SELECT DISTINCT a FROM t2;
 
537
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
538
1       SIMPLE  t2      index   NULL    PRIMARY 8       NULL    3       Using index
 
539
EXPLAIN SELECT DISTINCT a,a FROM t2;
 
540
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
541
1       SIMPLE  t2      index   NULL    PRIMARY 8       NULL    3       Using index
 
542
EXPLAIN SELECT DISTINCT b,a FROM t2;
 
543
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
544
1       SIMPLE  t2      index   NULL    PRIMARY 8       NULL    3       Using index
 
545
EXPLAIN SELECT DISTINCT a,c FROM t2;
 
546
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
547
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    3       Using temporary
 
548
EXPLAIN SELECT DISTINCT c,a,b FROM t2;
 
549
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
550
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    3       
 
551
EXPLAIN SELECT DISTINCT a,b,d FROM t2 GROUP BY c,b,d;
 
552
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
553
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    3       Using temporary; Using filesort
 
554
CREATE UNIQUE INDEX c_b_unq ON t2 (c,b);
 
555
EXPLAIN SELECT DISTINCT a,b,d FROM t2 GROUP BY c,b,d;
 
556
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
557
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    3       Using filesort
 
558
DROP TABLE t1,t2;
 
559
create table t1 (id int, dsc varchar(50));
 
560
insert into t1 values (1, "line number one"), (2, "line number two"), (3, "line number three");
 
561
select distinct id, IFNULL(dsc, '-') from t1;
 
562
id      IFNULL(dsc, '-')
 
563
1       line number one
 
564
2       line number two
 
565
3       line number three
 
566
drop table t1;
 
567
CREATE TABLE t1 (a int primary key, b int);
 
568
INSERT INTO t1 (a,b) values (1,1), (2,3), (3,2);
 
569
explain SELECT DISTINCT a, b FROM t1 ORDER BY b;
 
570
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
571
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       Using filesort
 
572
SELECT DISTINCT a, b FROM t1 ORDER BY b;
 
573
a       b
 
574
1       1
 
575
3       2
 
576
2       3
 
577
DROP TABLE t1;
 
578
CREATE TABLE t1 (
 
579
ID int(11) NOT NULL auto_increment,
 
580
x varchar(20) default NULL,
 
581
y decimal(10,0) default NULL,
 
582
PRIMARY KEY  (ID),
 
583
KEY (y)
 
584
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
585
INSERT INTO t1 VALUES
 
586
(1,'ba','-1'),
 
587
(2,'ba','1150'),
 
588
(306,'ba','-1'),
 
589
(307,'ba','1150'),
 
590
(611,'ba','-1'),
 
591
(612,'ba','1150');
 
592
select count(distinct x,y) from t1;
 
593
count(distinct x,y)
 
594
2
 
595
select count(distinct concat(x,y)) from t1;
 
596
count(distinct concat(x,y))
 
597
2
 
598
drop table t1;
 
599
CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a,b));
 
600
INSERT INTO t1 VALUES (1, 101);
 
601
INSERT INTO t1 SELECT a + 1, a + 101 FROM t1;
 
602
INSERT INTO t1 SELECT a + 2, a + 102 FROM t1;
 
603
INSERT INTO t1 SELECT a + 4, a + 104 FROM t1;
 
604
INSERT INTO t1 SELECT a + 8, a + 108 FROM t1;
 
605
EXPLAIN SELECT DISTINCT a,a FROM t1 WHERE b < 12 ORDER BY a;
 
606
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
607
1       SIMPLE  t1      index   NULL    PRIMARY 8       NULL    16      Using where; Using index
 
608
SELECT DISTINCT a,a FROM t1 WHERE b < 12 ORDER BY a;
 
609
a       a
 
610
DROP TABLE t1;
 
611
CREATE TABLE t1 (a CHAR(1));
 
612
INSERT INTO t1 VALUES('A'), (0);
 
613
SELECT a FROM t1 WHERE a=0;
 
614
a
 
615
A
 
616
0
 
617
Warnings:
 
618
Warning 1292    Truncated incorrect DOUBLE value: 'A'
 
619
SELECT DISTINCT a FROM t1 WHERE a=0;
 
620
a
 
621
A
 
622
0
 
623
Warnings:
 
624
Warning 1292    Truncated incorrect DOUBLE value: 'A'
 
625
DROP TABLE t1;
 
626
CREATE TABLE t1 (a DATE);
 
627
INSERT INTO t1 VALUES ('1972-07-29'), ('1972-02-06');
 
628
EXPLAIN SELECT (SELECT DISTINCT a FROM t1 WHERE a = '2002-08-03');
 
629
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
630
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
631
2       SUBQUERY        t1      ALL     NULL    NULL    NULL    NULL    2       Using where
 
632
EXPLAIN SELECT (SELECT DISTINCT ADDDATE(a,1) FROM t1
 
633
WHERE ADDDATE(a,1) = '2002-08-03');
 
634
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
635
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
636
2       SUBQUERY        t1      ALL     NULL    NULL    NULL    NULL    2       Using where
 
637
CREATE TABLE t2 (a CHAR(5) CHARACTER SET latin1 COLLATE latin1_general_ci);
 
638
INSERT INTO t2 VALUES (0xf6);
 
639
INSERT INTO t2 VALUES ('oe');
 
640
SELECT COUNT(*) FROM (SELECT DISTINCT a FROM t2) dt;
 
641
COUNT(*)
 
642
2
 
643
SELECT COUNT(*) FROM 
 
644
(SELECT DISTINCT a FROM t2 WHERE a='oe' COLLATE latin1_german2_ci) dt;
 
645
COUNT(*)
 
646
2
 
647
DROP TABLE t1, t2;
 
648
CREATE TABLE t1 (a INT, UNIQUE (a));
 
649
INSERT INTO t1 VALUES (4),(null),(2),(1),(null),(3);
 
650
EXPLAIN SELECT DISTINCT a FROM t1;
 
651
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
652
1       SIMPLE  t1      index   NULL    a       5       NULL    6       Using index
 
653
SELECT DISTINCT a FROM t1;
 
654
a
 
655
NULL
 
656
1
 
657
2
 
658
3
 
659
4
 
660
EXPLAIN SELECT a FROM t1 GROUP BY a;
 
661
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
662
1       SIMPLE  t1      index   NULL    a       5       NULL    6       Using index
 
663
SELECT a FROM t1 GROUP BY a;
 
664
a
 
665
NULL
 
666
1
 
667
2
 
668
3
 
669
4
 
670
DROP TABLE t1;
 
671
CREATE TABLE t1 (a INT, b INT);
 
672
INSERT INTO t1 VALUES(1,1),(1,2),(1,3);
 
673
SELECT DISTINCT a, b FROM t1;
 
674
a       b
 
675
1       1
 
676
1       2
 
677
1       3
 
678
SELECT DISTINCT a, a, b FROM t1;
 
679
a       a       b
 
680
1       1       1
 
681
1       1       2
 
682
1       1       3
 
683
DROP TABLE t1;
 
684
End of 5.0 tests
 
685
CREATE TABLE t1(a INT, b INT, c INT, d INT, e INT,
 
686
PRIMARY KEY(a,b,c,d,e),
 
687
KEY(a,b,d,c)
 
688
);
 
689
INSERT INTO t1(a, b, c) VALUES (1, 1, 1),
 
690
(1, 1, 2),
 
691
(1, 1, 3),
 
692
(1, 2, 1),
 
693
(1, 2, 2),
 
694
(1, 2, 3);
 
695
EXPLAIN SELECT DISTINCT a, b, d, c FROM t1;
 
696
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
697
1       SIMPLE  t1      index   NULL    a       16      NULL    6       Using index
 
698
SELECT DISTINCT a, b, d, c FROM t1;
 
699
a       b       d       c
 
700
1       1       0       1
 
701
1       1       0       2
 
702
1       1       0       3
 
703
1       2       0       1
 
704
1       2       0       2
 
705
1       2       0       3
 
706
DROP TABLE t1;
 
707
End of 5.1 tests