summaryrefslogtreecommitdiff
path: root/mysql-test/r/distinct.result
blob: 21f87a11a53d81a1342914ae447cf9cb67b7a072 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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;
id
NULL
-1
0
1
2
3
4
5
6
7
8
9
10
select * from t1 order by id;
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
select id-5,facility from t1 order by "id-5";
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
select id,concat(facility) from t1 group by id ;
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
select id+0 as a,max(id),concat(facility) as b from t1 group by a order by b desc,a;
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	
NULL	NULL	NULL
select id >= 0 and id <= 5 as grp,count(*) from t1 group by grp;
grp	count(*)
0	7
1	6
SELECT DISTINCT FACILITY FROM t1;
FACILITY
NULL

/L
A01
ANC
F01
FBX
MT
P
RV
SRV
VMT
SELECT FACILITY FROM t2;
FACILITY
NULL

/L
A01
ANC
F01
FBX
MT
P
RV
SRV
VMT
SELECT count(*) from t1,t2 where t1.facility=t2.facility;
count(*)
12
select count(facility) from t1;
count(facility)
12
select count(*) from t1;
count(*)
13
select count(*) from t1 where facility IS NULL;
count(*)
1
select count(*) from t1 where facility = NULL;
count(*)
0
select count(*) from t1 where facility IS NOT NULL;
count(*)
12
select count(*) from t1 where id IS NULL;
count(*)
1
select count(*) from t1 where id IS NOT NULL;
count(*)
12
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;
UserId
SELECT UserId FROM t1 WHERE UserId=22 group by Userid;
UserId
SELECT DISTINCT UserId FROM t1 WHERE UserId=22 group by Userid;
UserId
SELECT DISTINCT UserId FROM t1 WHERE UserId=22;
UserId
drop table t1;
CREATE TABLE t1 (a int(10) unsigned not null primary key,b int(10) unsigned);
INSERT INTO t1 VALUES (1,1),(2,1);
CREATE TABLE t2 (a int(10) unsigned not null, key (A));
INSERT INTO t2 VALUES (1),(2);
CREATE TABLE t3 (a int(10) unsigned, key(A), b text);
INSERT INTO t3 VALUES (1,'1'),(2,'2');
SELECT DISTINCT t3.b FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a;
b
1
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;
table	type	possible_keys	key	key_len	ref	rows	Extra
t3	index	a	a	5	NULL	6	Using index; Using temporary
t2	index	a	a	4	NULL	5	Using index; Distinct
t1	eq_ref	PRIMARY	PRIMARY	4	t2.a	1	where used; Distinct
SELECT distinct t3.a FROM t3,t2,t1 WHERE t3.a=t1.b AND t1.a=t2.a;
a
1
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;
table	type	possible_keys	key	key_len	ref	rows	Extra
t1	index	PRIMARY	PRIMARY	4	NULL	2	Using index; Using temporary
t3	ref	a	a	5	t1.a	10	Using index; Distinct
select distinct t1.a from t1,t3 where t1.a=t3.a;
a
1
2
select distinct 1 from t1,t3 where t1.a=t3.a;
1
1
drop table t1,t2,t3,t4;
CREATE TABLE t1 (name varchar(255));
INSERT INTO t1 VALUES ('aa'),('ab'),('ac'),('ad'),('ae');
SELECT DISTINCT * FROM t1 LIMIT 2;
name
aa
ab
SELECT DISTINCT name FROM t1 LIMIT 2;
name
aa
ab
SELECT DISTINCT 1 FROM t1 LIMIT 2;
1
1
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;
key_link_id	link
NULL	NULL
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));
table	type	possible_keys	key	key_len	ref	rows	Extra
t1	index	id	id	4	NULL	2	Using index; Using temporary
t2	index	id	id	8	NULL	1	Using index; Distinct
t3	index	id	id	8	NULL	1	Using index; Distinct
j_lj_t2	index	id	id	4	NULL	2	where used; Using index; Distinct
t2_lj	index	id	id	8	NULL	1	where used; Using index; Distinct
j_lj_t3	index	id	id	4	NULL	2	where used; Using index; Distinct
t3_lj	index	id	id	8	NULL	1	where used; Using index; Distinct
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));
id
2
drop table t1,t2,t3;
drop table if exists t1;
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;
a	sec_to_time(sum(time_to_sec(t)))
1	00:06:15
1	00:36:30
1	00:36:30
select distinct a,sec_to_time(sum(time_to_sec(t))) from t1 group by a,b;
a	sec_to_time(sum(time_to_sec(t)))
1	00:06:15
1	00:36:30
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;
a	sec_to_time(sum(time_to_sec(t)))
1	00:06:15
1	00:36:30
1	00:36:30
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;
a	sec_to_time(sum(time_to_sec(t)))
1	00:06:15
1	00:36:30
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;
a
4
3
select distinct a,c from t1 group by b,c,a having a > 2 order by a desc;
a	c
4	NULL
3	NULL
drop table t1;