~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to tests/r/distinct.result

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

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 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 not null primary key,b int);
 
164
INSERT INTO t1 VALUES (1,1),(2,1),(3,1),(4,1);
 
165
CREATE TABLE t2 (a int not null, key (A));
 
166
INSERT INTO t2 VALUES (1),(2);
 
167
CREATE TABLE t3 (a int, 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  t2      ref     a       a       4       test.t1.a       1       Using index
 
178
1       SIMPLE  t3      ref     a       a       5       test.t1.b       1       Using index
 
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       1       Using index; Distinct
 
194
select distinct t1.a from t1,t3 where t1.a=t3.a;
 
195
a
 
196
1
 
197
2
 
198
show status like 'Handler%';
 
199
Variable_name   Value
 
200
Handler_commit  #
 
201
Handler_delete  #
 
202
Handler_prepare #
 
203
Handler_read_first      #
 
204
Handler_read_key        #
 
205
Handler_read_next       #
 
206
Handler_read_prev       #
 
207
Handler_read_rnd        #
 
208
Handler_read_rnd_next   #
 
209
Handler_rollback        #
 
210
Handler_savepoint       #
 
211
Handler_savepoint_rollback      #
 
212
Handler_update  #
 
213
Handler_write   #
 
214
flush status;
 
215
select distinct 1 from t1,t3 where t1.a=t3.a;
 
216
1
 
217
1
 
218
show status like 'Handler%';
 
219
Variable_name   Value
 
220
Handler_commit  #
 
221
Handler_delete  #
 
222
Handler_prepare #
 
223
Handler_read_first      #
 
224
Handler_read_key        #
 
225
Handler_read_next       #
 
226
Handler_read_prev       #
 
227
Handler_read_rnd        #
 
228
Handler_read_rnd_next   #
 
229
Handler_rollback        #
 
230
Handler_savepoint       #
 
231
Handler_savepoint_rollback      #
 
232
Handler_update  #
 
233
Handler_write   #
 
234
explain SELECT distinct t1.a from t1;
 
235
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
236
1       SIMPLE  t1      index   NULL    PRIMARY 4       NULL    4       Using index
 
237
explain SELECT distinct t1.a from t1 order by a desc;
 
238
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
239
1       SIMPLE  t1      index   NULL    PRIMARY 4       NULL    4       Using index
 
240
explain SELECT t1.a from t1 group by a order by a desc;
 
241
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
242
1       SIMPLE  t1      index   NULL    PRIMARY 4       NULL    4       Using index
 
243
explain SELECT distinct t1.a from t1 order by a desc limit 1;
 
244
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
245
1       SIMPLE  t1      index   NULL    PRIMARY 4       NULL    1       Using index
 
246
explain SELECT distinct a from t3 order by a desc limit 2;
 
247
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
248
1       SIMPLE  t3      index   NULL    a       5       NULL    2       Using index
 
249
explain SELECT distinct a,b from t3 order by a+1;
 
250
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
251
1       SIMPLE  t3      ALL     NULL    NULL    NULL    NULL    204     Using temporary; Using filesort
 
252
explain SELECT distinct a,b from t3 order by a limit 2;
 
253
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
254
1       SIMPLE  t3      ALL     NULL    NULL    NULL    NULL    204     Using temporary; Using filesort
 
255
explain SELECT a,b from t3 group by a,b order by a+1;
 
256
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
257
1       SIMPLE  t3      ALL     NULL    NULL    NULL    NULL    204     Using temporary; Using filesort
 
258
drop table t1,t2,t3,t4;
 
259
CREATE TABLE t1 (name varchar(255));
 
260
INSERT INTO t1 VALUES ('aa'),('ab'),('ac'),('ad'),('ae');
 
261
SELECT DISTINCT * FROM t1 LIMIT 2;
 
262
name
 
263
aa
 
264
ab
 
265
SELECT DISTINCT name FROM t1 LIMIT 2;
 
266
name
 
267
aa
 
268
ab
 
269
SELECT DISTINCT 1 FROM t1 LIMIT 2;
 
270
1
 
271
1
 
272
drop table t1;
 
273
CREATE TABLE t1 (
 
274
ID int NOT NULL auto_increment,
 
275
NAME varchar(75) DEFAULT '' NOT NULL,
 
276
LINK_ID int DEFAULT '0' NOT NULL,
 
277
PRIMARY KEY (ID),
 
278
KEY NAME (NAME),
 
279
KEY LINK_ID (LINK_ID)
 
280
);
 
281
INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (1,'Mike',0),(2,'Jack',0),(3,'Bill',0);
 
282
CREATE TABLE t2 (
 
283
ID int NOT NULL auto_increment,
 
284
NAME varchar(150) DEFAULT '' NOT NULL,
 
285
PRIMARY KEY (ID),
 
286
KEY NAME (NAME)
 
287
);
 
288
SELECT DISTINCT
 
289
t2.id AS key_link_id,
 
290
t2.name AS link
 
291
FROM t1
 
292
LEFT JOIN t2 ON t1.link_id=t2.id
 
293
GROUP BY t1.id
 
294
ORDER BY link;
 
295
key_link_id     link
 
296
NULL    NULL
 
297
drop table t1,t2;
 
298
create table t1 (
 
299
id              int not null,
 
300
name    tinytext not null,
 
301
unique  (id)
 
302
);
 
303
create table t2 (
 
304
id              int not null,
 
305
idx             int not null,
 
306
unique  (id, idx)
 
307
);
 
308
create table t3 (
 
309
id              int not null,
 
310
idx             int not null,
 
311
unique  (id, idx)
 
312
);
 
313
insert into t1 values (1,'yes'), (2,'no');
 
314
insert into t2 values (1,1);
 
315
insert into t3 values (1,1);
 
316
EXPLAIN
 
317
SELECT DISTINCT
 
318
t1.id
 
319
from
 
320
t1
 
321
straight_join
 
322
t2
 
323
straight_join
 
324
t3
 
325
straight_join
 
326
t1 as j_lj_t2 left join t2 as t2_lj
 
327
on j_lj_t2.id=t2_lj.id
 
328
straight_join
 
329
t1 as j_lj_t3 left join t3 as t3_lj
 
330
on j_lj_t3.id=t3_lj.id
 
331
WHERE
 
332
((t1.id=j_lj_t2.id AND t2_lj.id IS NULL) OR (t1.id=t2.id AND t2.idx=2))
 
333
AND ((t1.id=j_lj_t3.id AND t3_lj.id IS NULL) OR (t1.id=t3.id AND t3.idx=2));
 
334
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
335
1       SIMPLE  t1      index   id      id      4       NULL    2       Using index; Using temporary
 
336
1       SIMPLE  t2      index   id      id      8       NULL    1       Using index; Distinct; Using join buffer
 
337
1       SIMPLE  t3      index   id      id      8       NULL    1       Using index; Distinct; Using join buffer
 
338
1       SIMPLE  j_lj_t2 index   id      id      4       NULL    2       Using where; Using index; Distinct; Using join buffer
 
339
1       SIMPLE  t2_lj   ref     id      id      4       test.j_lj_t2.id 1       Using where; Using index; Distinct
 
340
1       SIMPLE  j_lj_t3 index   id      id      4       NULL    2       Using where; Using index; Distinct; Using join buffer
 
341
1       SIMPLE  t3_lj   ref     id      id      4       test.j_lj_t3.id 1       Using where; Using index; Distinct
 
342
SELECT DISTINCT
 
343
t1.id
 
344
from
 
345
t1
 
346
straight_join
 
347
t2
 
348
straight_join
 
349
t3
 
350
straight_join
 
351
t1 as j_lj_t2 left join t2 as t2_lj
 
352
on j_lj_t2.id=t2_lj.id
 
353
straight_join
 
354
t1 as j_lj_t3 left join t3 as t3_lj
 
355
on j_lj_t3.id=t3_lj.id
 
356
WHERE
 
357
((t1.id=j_lj_t2.id AND t2_lj.id IS NULL) OR (t1.id=t2.id AND t2.idx=2))
 
358
AND ((t1.id=j_lj_t3.id AND t3_lj.id IS NULL) OR (t1.id=t3.id AND t3.idx=2));
 
359
id
 
360
2
 
361
drop table t1,t2,t3;
 
362
create table t1 (a int not null,b char(5), c text);
 
363
insert into t1 (a) values (1),(2),(3),(4),(1),(2),(3),(4);
 
364
select distinct a from t1 group by b,a having a > 2 order by a desc;
 
365
a
 
366
4
 
367
3
 
368
select distinct a,c from t1 group by b,c,a having a > 2 order by a desc;
 
369
a       c
 
370
4       NULL
 
371
3       NULL
 
372
drop table t1;
 
373
create table t1 (a char(1), key(a));
 
374
insert into t1 values('1'),('1');
 
375
select * from t1 where a >= '1';
 
376
a
 
377
1
 
378
1
 
379
select distinct a from t1 order by a desc;
 
380
a
 
381
1
 
382
select distinct a from t1 where a >= '1' order by a desc;
 
383
a
 
384
1
 
385
drop table t1;
 
386
CREATE TABLE t1 (email varchar(50), infoID BIGINT, dateentered DATETIME);
 
387
CREATE TABLE t2 (infoID BIGINT, shipcode varchar(10));
 
388
INSERT INTO t1 (email, infoID, dateentered) VALUES
 
389
('test1@testdomain.com', 1, '2002-07-30 22:56:38'),
 
390
('test1@testdomain.com', 1, '2002-07-27 22:58:16'),
 
391
('test2@testdomain.com', 1, '2002-06-19 15:22:19'),
 
392
('test2@testdomain.com', 2, '2002-06-18 14:23:47'),
 
393
('test3@testdomain.com', 1, '2002-05-19 22:17:32');
 
394
INSERT INTO t2(infoID, shipcode) VALUES
 
395
(1, 'Z001'),
 
396
(2, 'R002');
 
397
SELECT DISTINCTROW email, shipcode FROM t1, t2 WHERE t1.infoID=t2.infoID;
 
398
email   shipcode
 
399
test1@testdomain.com    Z001
 
400
test2@testdomain.com    Z001
 
401
test2@testdomain.com    R002
 
402
test3@testdomain.com    Z001
 
403
SELECT DISTINCTROW email FROM t1 ORDER BY dateentered DESC;
 
404
email
 
405
test1@testdomain.com
 
406
test2@testdomain.com
 
407
test3@testdomain.com
 
408
SELECT DISTINCTROW email, shipcode FROM t1, t2 WHERE t1.infoID=t2.infoID ORDER BY dateentered DESC;
 
409
email   shipcode
 
410
test1@testdomain.com    Z001
 
411
test2@testdomain.com    Z001
 
412
test2@testdomain.com    R002
 
413
test3@testdomain.com    Z001
 
414
drop table t1,t2;
 
415
CREATE TABLE t1 (privatemessageid int NOT NULL auto_increment,  folderid int NOT NULL default '0',  userid int NOT NULL default '0',  touserid int NOT NULL default '0',  fromuserid int NOT NULL default '0',  title varchar(250) NOT NULL default '',  message mediumtext NOT NULL,  dateline int NOT NULL default '0',  showsignature int NOT NULL default '0',  iconid int NOT NULL default '0',  messageread int NOT NULL default '0',  readtime int NOT NULL default '0',  receipt int NOT NULL default '0',  deleteprompt int NOT NULL default '0',  multiplerecipients int NOT NULL default '0',  PRIMARY KEY  (privatemessageid),  KEY userid (userid));
 
416
INSERT INTO t1 VALUES (128,0,33,33,8,':D','',996121863,1,0,2,996122850,2,0,0);
 
417
CREATE TABLE t2 (userid int NOT NULL auto_increment,  usergroupid int NOT NULL default '0',  username varchar(50) NOT NULL default '',  password varchar(50) NOT NULL default '',  email varchar(50) NOT NULL default '',  styleid int NOT NULL default '0',  parentemail varchar(50) NOT NULL default '',  coppauser int 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 int NOT NULL default '0',  showemail int NOT NULL default '0',  invisible int NOT NULL default '0',  usertitle varchar(250) NOT NULL default '',  customtitle int NOT NULL default '0',  joindate int NOT NULL default '0',  cookieuser int NOT NULL default '0',  daysprune int NOT NULL default '0',  lastvisit int NOT NULL default '0',  lastactivity int NOT NULL default '0',  lastpost int NOT NULL default '0',  posts int NOT NULL default '0',  timezoneoffset varchar(4) NOT NULL default '',  emailnotification int NOT NULL default '0',  buddylist mediumtext NOT NULL,  ignorelist mediumtext NOT NULL,  pmfolders mediumtext NOT NULL,  receivepm int NOT NULL default '0',  emailonpm int NOT NULL default '0',  pmpopup int NOT NULL default '0',  avatarid int NOT NULL default '0',  avatarrevision int NOT NULL default '0',  options int NOT NULL default '15',  birthday date NULL,  maxposts int NOT NULL default '-1',  startofweek int NOT NULL default '1',  ipaddress varchar(20) NOT NULL default '',  referrerid int NOT NULL default '0',  nosessionhash int NOT NULL default '0',  autorefresh int NOT NULL default '-1',  messagepopup int NOT NULL default '0',  inforum int NOT NULL default '0',  ratenum int NOT NULL default '0',  ratetotal int NOT NULL default '0',  allowrate int NOT NULL default '1',  PRIMARY KEY  (userid),  KEY usergroupid (usergroupid),  KEY username (username),  KEY inforum (inforum));
 
418
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,NULL,-1,1,'64.0.0.0',0,1,-1,0,0,4,19,1);
 
419
SELECT DISTINCT t1.*, t2.* FROM t1 LEFT JOIN t2 ON (t2.userid = t1.touserid);
 
420
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
 
421
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      NULL    -1      1       64.0.0.0        0       1       -1      0       0       4       19      1
 
422
DROP TABLE t1,t2;
 
423
CREATE TABLE t1 (a int primary key, b int, c int);
 
424
INSERT t1 VALUES (1,2,3);
 
425
CREATE TABLE t2 (a int primary key, b int, c int);
 
426
INSERT t2 VALUES (3,4,5);
 
427
SELECT DISTINCT t1.a, t2.b FROM t1, t2 WHERE t1.a=1 ORDER BY t2.c;
 
428
a       b
 
429
1       4
 
430
DROP TABLE t1,t2;
 
431
CREATE table t1 (  `id` int NOT NULL auto_increment,  `name` varchar(50) NOT NULL default '',  PRIMARY KEY  (`id`)) AUTO_INCREMENT=3 ;
 
432
INSERT INTO t1 VALUES (1, 'aaaaa');
 
433
INSERT INTO t1 VALUES (3, 'aaaaa');
 
434
INSERT INTO t1 VALUES (2, 'eeeeeee');
 
435
select distinct left(name,1) as name from t1;
 
436
name
 
437
a
 
438
e
 
439
drop  table t1;
 
440
CREATE TABLE t1 (
 
441
ID int NOT NULL auto_increment,
 
442
NAME varchar(75) DEFAULT '' NOT NULL,
 
443
LINK_ID int DEFAULT '0' NOT NULL,
 
444
PRIMARY KEY (ID),
 
445
KEY NAME (NAME),
 
446
KEY LINK_ID (LINK_ID)
 
447
);
 
448
INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (1,'Mike',0);
 
449
INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (2,'Jack',0);
 
450
INSERT INTO t1 (ID, NAME, LINK_ID) VALUES (3,'Bill',0);
 
451
CREATE TABLE t2 (
 
452
ID int NOT NULL auto_increment,
 
453
NAME varchar(150) DEFAULT '' NOT NULL,
 
454
PRIMARY KEY (ID),
 
455
KEY NAME (NAME)
 
456
);
 
457
SELECT DISTINCT
 
458
t2.id AS key_link_id,
 
459
t2.name AS link
 
460
FROM t1
 
461
LEFT JOIN t2 ON t1.link_id=t2.id
 
462
GROUP BY t1.id
 
463
ORDER BY link;
 
464
key_link_id     link
 
465
NULL    NULL
 
466
drop table t1,t2;
 
467
CREATE TABLE t1 (
 
468
html varchar(5) default NULL,
 
469
rin int default '0',
 
470
rout int default '0'
 
471
);
 
472
INSERT INTO t1 VALUES ('1',1,0);
 
473
SELECT DISTINCT html,SUM(rout)/(SUM(rin)+1) as 'prod' FROM t1 GROUP BY rin;
 
474
html    prod
 
475
1       0.0000
 
476
drop table t1;
 
477
CREATE TABLE t1 (a int);
 
478
INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
 
479
SELECT DISTINCT a, 1 FROM t1;
 
480
a       1
 
481
1       1
 
482
2       1
 
483
3       1
 
484
4       1
 
485
5       1
 
486
SELECT DISTINCT 1, a FROM t1;
 
487
1       a
 
488
1       1
 
489
1       2
 
490
1       3
 
491
1       4
 
492
1       5
 
493
CREATE TABLE t2 (a int, b int);
 
494
INSERT INTO t2 VALUES (1,1),(2,2),(2,3),(2,4),(3,5);
 
495
SELECT DISTINCT a, b, 2 FROM t2;
 
496
a       b       2
 
497
1       1       2
 
498
2       2       2
 
499
2       3       2
 
500
2       4       2
 
501
3       5       2
 
502
SELECT DISTINCT 2, a, b FROM t2;
 
503
2       a       b
 
504
2       1       1
 
505
2       2       2
 
506
2       2       3
 
507
2       2       4
 
508
2       3       5
 
509
SELECT DISTINCT a, 2, b FROM t2;
 
510
a       2       b
 
511
1       2       1
 
512
2       2       2
 
513
2       2       3
 
514
2       2       4
 
515
3       2       5
 
516
DROP TABLE t1,t2;
 
517
CREATE TABLE t1(a INT PRIMARY KEY, b INT);
 
518
INSERT INTO t1 VALUES (1,1), (2,1), (3,1);
 
519
EXPLAIN SELECT DISTINCT a FROM t1;
 
520
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
521
1       SIMPLE  t1      index   NULL    PRIMARY 4       NULL    3       Using index
 
522
EXPLAIN SELECT DISTINCT a,b FROM t1;
 
523
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
524
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       
 
525
EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2;
 
526
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
527
1       SIMPLE  t1_1    ALL     NULL    NULL    NULL    NULL    3       Using temporary
 
528
1       SIMPLE  t1_2    index   NULL    PRIMARY 4       NULL    3       Using index; Distinct; Using join buffer
 
529
EXPLAIN SELECT DISTINCT t1_1.a, t1_1.b FROM t1 t1_1, t1 t1_2
 
530
WHERE t1_1.a = t1_2.a;
 
531
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
532
1       SIMPLE  t1_2    index   PRIMARY PRIMARY 4       NULL    3       Using index; Using temporary
 
533
1       SIMPLE  t1_1    ALL     PRIMARY NULL    NULL    NULL    3       Using where; Using join buffer
 
534
EXPLAIN SELECT a FROM t1 GROUP BY a;
 
535
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
536
1       SIMPLE  t1      index   NULL    PRIMARY 4       NULL    3       Using index
 
537
EXPLAIN SELECT a,b FROM t1 GROUP BY a,b;
 
538
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
539
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       Using filesort
 
540
EXPLAIN SELECT DISTINCT a,b FROM t1 GROUP BY a,b;
 
541
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
542
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       Using filesort
 
543
CREATE TABLE t2(a INT, b INT NOT NULL, c INT NOT NULL, d INT, 
 
544
PRIMARY KEY (a,b));
 
545
INSERT INTO t2 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4);
 
546
EXPLAIN SELECT DISTINCT a FROM t2;
 
547
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
548
1       SIMPLE  t2      index   NULL    PRIMARY 8       NULL    3       Using index
 
549
EXPLAIN SELECT DISTINCT a,a FROM t2;
 
550
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
551
1       SIMPLE  t2      index   NULL    PRIMARY 8       NULL    3       Using index
 
552
EXPLAIN SELECT DISTINCT b,a FROM t2;
 
553
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
554
1       SIMPLE  t2      index   NULL    PRIMARY 8       NULL    3       Using index
 
555
EXPLAIN SELECT DISTINCT a,c FROM t2;
 
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 temporary
 
558
EXPLAIN SELECT DISTINCT c,a,b FROM t2;
 
559
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
560
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    3       
 
561
EXPLAIN SELECT DISTINCT a,b,d FROM t2 GROUP BY c,b,d;
 
562
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
563
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    3       Using temporary; Using filesort
 
564
CREATE UNIQUE INDEX c_b_unq ON t2 (c,b);
 
565
EXPLAIN SELECT DISTINCT a,b,d FROM t2 GROUP BY c,b,d;
 
566
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
567
1       SIMPLE  t2      ALL     NULL    NULL    NULL    NULL    3       Using filesort
 
568
DROP TABLE t1,t2;
 
569
create table t1 (id int, dsc varchar(50));
 
570
insert into t1 values (1, "line number one"), (2, "line number two"), (3, "line number three");
 
571
select distinct id, IFNULL(dsc, '-') from t1;
 
572
id      IFNULL(dsc, '-')
 
573
1       line number one
 
574
2       line number two
 
575
3       line number three
 
576
drop table t1;
 
577
CREATE TABLE t1 (a int primary key, b int);
 
578
INSERT INTO t1 (a,b) values (1,1), (2,3), (3,2);
 
579
explain SELECT DISTINCT a, b FROM t1 ORDER BY b;
 
580
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
581
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       Using filesort
 
582
SELECT DISTINCT a, b FROM t1 ORDER BY b;
 
583
a       b
 
584
1       1
 
585
3       2
 
586
2       3
 
587
DROP TABLE t1;
 
588
CREATE TABLE t1 (
 
589
ID int NOT NULL auto_increment,
 
590
x varchar(20) default NULL,
 
591
y decimal(10,0) default NULL,
 
592
PRIMARY KEY  (ID),
 
593
KEY (y)
 
594
);
 
595
INSERT INTO t1 VALUES
 
596
(1,'ba','-1'),
 
597
(2,'ba','1150'),
 
598
(306,'ba','-1'),
 
599
(307,'ba','1150'),
 
600
(611,'ba','-1'),
 
601
(612,'ba','1150');
 
602
select count(distinct x,y) from t1;
 
603
count(distinct x,y)
 
604
2
 
605
select count(distinct concat(x,y)) from t1;
 
606
count(distinct concat(x,y))
 
607
2
 
608
drop table t1;
 
609
CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a,b));
 
610
INSERT INTO t1 VALUES (1, 101);
 
611
INSERT INTO t1 SELECT a + 1, a + 101 FROM t1;
 
612
INSERT INTO t1 SELECT a + 2, a + 102 FROM t1;
 
613
INSERT INTO t1 SELECT a + 4, a + 104 FROM t1;
 
614
INSERT INTO t1 SELECT a + 8, a + 108 FROM t1;
 
615
EXPLAIN SELECT DISTINCT a,a FROM t1 WHERE b < 12 ORDER BY a;
 
616
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
617
1       SIMPLE  t1      index   NULL    PRIMARY 8       NULL    16      Using where; Using index
 
618
SELECT DISTINCT a,a FROM t1 WHERE b < 12 ORDER BY a;
 
619
a       a
 
620
DROP TABLE t1;
 
621
CREATE TABLE t1 (a INT, UNIQUE (a));
 
622
INSERT INTO t1 VALUES (4),(null),(2),(1),(null),(3);
 
623
EXPLAIN SELECT DISTINCT a FROM t1;
 
624
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
625
1       SIMPLE  t1      index   NULL    a       5       NULL    6       Using index
 
626
SELECT DISTINCT a FROM t1;
 
627
a
 
628
NULL
 
629
1
 
630
2
 
631
3
 
632
4
 
633
EXPLAIN SELECT a FROM t1 GROUP BY a;
 
634
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
635
1       SIMPLE  t1      index   NULL    a       5       NULL    6       Using index
 
636
SELECT a FROM t1 GROUP BY a;
 
637
a
 
638
NULL
 
639
1
 
640
2
 
641
3
 
642
4
 
643
DROP TABLE t1;
 
644
CREATE TABLE t1 (a INT, b INT);
 
645
INSERT INTO t1 VALUES(1,1),(1,2),(1,3);
 
646
SELECT DISTINCT a, b FROM t1;
 
647
a       b
 
648
1       1
 
649
1       2
 
650
1       3
 
651
SELECT DISTINCT a, a, b FROM t1;
 
652
a       a       b
 
653
1       1       1
 
654
1       1       2
 
655
1       1       3
 
656
DROP TABLE t1;
 
657
End of 5.0 tests
 
658
CREATE TABLE t1(a INT, b INT, c INT, d INT DEFAULT 0, e INT DEFAULT 0,
 
659
PRIMARY KEY(a,b,c,d,e),
 
660
KEY(a,b,d,c)
 
661
);
 
662
INSERT INTO t1(a, b, c) VALUES (1, 1, 1),
 
663
(1, 1, 2),
 
664
(1, 1, 3),
 
665
(1, 2, 1),
 
666
(1, 2, 2),
 
667
(1, 2, 3);
 
668
EXPLAIN SELECT DISTINCT a, b, d, c FROM t1;
 
669
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
670
1       SIMPLE  t1      index   NULL    a       16      NULL    6       Using index
 
671
SELECT DISTINCT a, b, d, c FROM t1;
 
672
a       b       d       c
 
673
1       1       0       1
 
674
1       1       0       2
 
675
1       1       0       3
 
676
1       2       0       1
 
677
1       2       0       2
 
678
1       2       0       3
 
679
DROP TABLE t1;
 
680
End of 5.1 tests