~ubuntu-branches/ubuntu/trusty/mariadb-5.5/trusty-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Otto Kekäläinen
  • Date: 2013-12-22 10:27:05 UTC
  • Revision ID: package-import@ubuntu.com-20131222102705-mndw7s12mz0szrcn
Tags: upstream-5.5.32
Import upstream version 5.5.32

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12;
 
2
drop view if exists v2;
 
3
set @subselect_tmp=@@optimizer_switch;
 
4
set @@optimizer_switch=ifnull(@optimizer_switch_for_subselect_test,
 
5
"semijoin=on,firstmatch=on,loosescan=on,semijoin_with_cache=on,partial_match_rowid_merge=off,partial_match_table_scan=off");
 
6
set join_cache_level=1;
 
7
set optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on';
 
8
select (select 2);
 
9
(select 2)
 
10
2
 
11
explain extended select (select 2);
 
12
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
13
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
14
Warnings:
 
15
Note    1249    Select 2 was reduced during optimization
 
16
Note    1003    select 2 AS `(select 2)`
 
17
SELECT (SELECT 1) UNION SELECT (SELECT 2);
 
18
(SELECT 1)
 
19
1
 
20
2
 
21
explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2);
 
22
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
23
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
24
3       UNION   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
25
NULL    UNION RESULT    <union1,3>      ALL     NULL    NULL    NULL    NULL    NULL    NULL    
 
26
Warnings:
 
27
Note    1249    Select 2 was reduced during optimization
 
28
Note    1249    Select 4 was reduced during optimization
 
29
Note    1003    select 1 AS `(SELECT 1)` union select 2 AS `(SELECT 2)`
 
30
SELECT (SELECT (SELECT 0 UNION SELECT 0));
 
31
(SELECT (SELECT 0 UNION SELECT 0))
 
32
0
 
33
explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0));
 
34
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
35
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
36
3       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
37
4       UNION   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
38
NULL    UNION RESULT    <union3,4>      ALL     NULL    NULL    NULL    NULL    NULL    NULL    
 
39
Warnings:
 
40
Note    1249    Select 2 was reduced during optimization
 
41
Note    1003    select (select 0 union select 0) AS `(SELECT (SELECT 0 UNION SELECT 0))`
 
42
SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
 
43
ERROR 42S22: Reference 'a' not supported (forward reference in item list)
 
44
SELECT (SELECT 1 FROM (SELECT 1) as b HAVING b=1) as a,(SELECT 1 FROM (SELECT 1) as c HAVING a=1) as b;
 
45
ERROR 42S22: Reference 'b' not supported (forward reference in item list)
 
46
SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
 
47
(SELECT 1)      MAX(1)
 
48
1       1
 
49
SELECT (SELECT a) as a;
 
50
ERROR 42S22: Reference 'a' not supported (forward reference in item list)
 
51
EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b  HAVING (SELECT a)=1;
 
52
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
53
1       PRIMARY <derived2>      system  NULL    NULL    NULL    NULL    1       100.00  
 
54
3       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
55
2       DERIVED NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
56
Warnings:
 
57
Note    1276    Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
 
58
Note    1276    Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
 
59
Note    1003    select 1 AS `1` from dual having ((select 1) = 1)
 
60
SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
 
61
1
 
62
1
 
63
SELECT (SELECT 1), a;
 
64
ERROR 42S22: Unknown column 'a' in 'field list'
 
65
SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1;
 
66
a
 
67
1
 
68
SELECT 1 FROM (SELECT (SELECT a) b) c;
 
69
ERROR 42S22: Unknown column 'a' in 'field list'
 
70
SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id);
 
71
id
 
72
1
 
73
SELECT * FROM (SELECT 1) a  WHERE 1 IN (SELECT 1,1);
 
74
ERROR 21000: Operand should contain 1 column(s)
 
75
SELECT 1 IN (SELECT 1);
 
76
1 IN (SELECT 1)
 
77
1
 
78
SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a));
 
79
1
 
80
1
 
81
select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1));
 
82
ERROR HY000: Incorrect usage of PROCEDURE and subquery
 
83
SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1));
 
84
ERROR HY000: Incorrect parameters to procedure 'ANALYSE'
 
85
SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL;
 
86
ERROR 42S22: Unknown column 'a' in 'field list'
 
87
SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL;
 
88
ERROR 42S22: Unknown column 'a' in 'field list'
 
89
SELECT (SELECT 1,2,3) = ROW(1,2,3);
 
90
(SELECT 1,2,3) = ROW(1,2,3)
 
91
1
 
92
SELECT (SELECT 1,2,3) = ROW(1,2,1);
 
93
(SELECT 1,2,3) = ROW(1,2,1)
 
94
0
 
95
SELECT (SELECT 1,2,3) < ROW(1,2,1);
 
96
(SELECT 1,2,3) < ROW(1,2,1)
 
97
0
 
98
SELECT (SELECT 1,2,3) > ROW(1,2,1);
 
99
(SELECT 1,2,3) > ROW(1,2,1)
 
100
1
 
101
SELECT (SELECT 1,2,3) = ROW(1,2,NULL);
 
102
(SELECT 1,2,3) = ROW(1,2,NULL)
 
103
NULL
 
104
SELECT ROW(1,2,3) = (SELECT 1,2,3);
 
105
ROW(1,2,3) = (SELECT 1,2,3)
 
106
1
 
107
SELECT ROW(1,2,3) = (SELECT 1,2,1);
 
108
ROW(1,2,3) = (SELECT 1,2,1)
 
109
0
 
110
SELECT ROW(1,2,3) < (SELECT 1,2,1);
 
111
ROW(1,2,3) < (SELECT 1,2,1)
 
112
0
 
113
SELECT ROW(1,2,3) > (SELECT 1,2,1);
 
114
ROW(1,2,3) > (SELECT 1,2,1)
 
115
1
 
116
SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
 
117
ROW(1,2,3) = (SELECT 1,2,NULL)
 
118
NULL
 
119
SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
 
120
(SELECT 1.5,2,'a') = ROW(1.5,2,'a')
 
121
1
 
122
SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
 
123
(SELECT 1.5,2,'a') = ROW(1.5,2,'b')
 
124
0
 
125
SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
 
126
(SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
 
127
0
 
128
Warnings:
 
129
Warning 1292    Truncated incorrect DOUBLE value: '1.5b'
 
130
SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
 
131
(SELECT 'b',2,'a') = ROW(1.5,2,'a')
 
132
0
 
133
SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
 
134
(SELECT 1.5,2,'a') = ROW(1.5,'2','a')
 
135
1
 
136
SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
 
137
(SELECT 1.5,'c','a') = ROW(1.5,2,'a')
 
138
0
 
139
SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
 
140
ERROR 21000: Operand should contain 1 column(s)
 
141
SELECT 1 as a,(SELECT a+a) b,(SELECT b);
 
142
a       b       (SELECT b)
 
143
1       2       2
 
144
create table t1 (a int);
 
145
create table t2 (a int, b int);
 
146
create table t3 (a int);
 
147
create table t4 (a int not null, b int not null);
 
148
insert into t1 values (2);
 
149
insert into t2 values (1,7),(2,7);
 
150
insert into t4 values (4,8),(3,8),(5,9);
 
151
select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
 
152
ERROR 42S22: Reference 'a1' not supported (forward reference in item list)
 
153
select (select a from t1 where t1.a=t2.a), a from t2;
 
154
(select a from t1 where t1.a=t2.a)      a
 
155
NULL    1
 
156
2       2
 
157
select (select a from t1 where t1.a=t2.b), a from t2;
 
158
(select a from t1 where t1.a=t2.b)      a
 
159
NULL    1
 
160
NULL    2
 
161
select (select a from t1), a, (select 1 union select 2 limit 1) from t2;
 
162
(select a from t1)      a       (select 1 union select 2 limit 1)
 
163
2       1       1
 
164
2       2       1
 
165
select (select a from t3), a from t2;
 
166
(select a from t3)      a
 
167
NULL    1
 
168
NULL    2
 
169
select * from t2 where t2.a=(select a from t1);
 
170
a       b
 
171
2       7
 
172
insert into t3 values (6),(7),(3);
 
173
select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1);
 
174
a       b
 
175
1       7
 
176
2       7
 
177
(select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 order by a limit 2) limit 3;
 
178
a       b
 
179
1       7
 
180
2       7
 
181
3       8
 
182
(select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a);
 
183
a       b
 
184
1       7
 
185
2       7
 
186
4       8
 
187
3       8
 
188
explain extended (select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a);
 
189
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
190
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where
 
191
2       SUBQUERY        t3      ALL     NULL    NULL    NULL    NULL    3       100.00  Using filesort
 
192
3       UNION   t4      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
193
4       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       100.00  
 
194
NULL    UNION RESULT    <union1,3>      ALL     NULL    NULL    NULL    NULL    NULL    NULL    
 
195
Warnings:
 
196
Note    1003    (select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where (`test`.`t2`.`b` = (select `test`.`t3`.`a` from `test`.`t3` order by 1 desc limit 1))) union (select `test`.`t4`.`a` AS `a`,`test`.`t4`.`b` AS `b` from `test`.`t4` where (`test`.`t4`.`b` = (select (max(`test`.`t2`.`a`) * 4) from `test`.`t2`)) order by `a`)
 
197
select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
 
198
(select a from t3 where a<t2.a*4 order by 1 desc limit 1)       a
 
199
3       1
 
200
7       2
 
201
select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
 
202
(select * from t2 where a>1) as tt;
 
203
(select t3.a from t3 where a<8 order by 1 desc limit 1) a
 
204
7       2
 
205
set @tmp_optimizer_switch=@@optimizer_switch;
 
206
set optimizer_switch='derived_merge=off,derived_with_keys=off';
 
207
explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
 
208
(select * from t2 where a>1) as tt;
 
209
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
210
1       PRIMARY <derived3>      ALL     NULL    NULL    NULL    NULL    2       100.00  
 
211
3       DERIVED t2      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where
 
212
2       SUBQUERY        t3      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where; Using filesort
 
213
Warnings:
 
214
Note    1003    select (select `test`.`t3`.`a` from `test`.`t3` where (`test`.`t3`.`a` < 8) order by 1 desc limit 1) AS `(select t3.a from t3 where a<8 order by 1 desc limit 1)`,`tt`.`a` AS `a` from (select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where (`test`.`t2`.`a` > 1)) `tt`
 
215
set optimizer_switch=@tmp_optimizer_switch;
 
216
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3) order by 1 desc limit 1);
 
217
a
 
218
2
 
219
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a > t1.a) order by 1 desc limit 1);
 
220
a
 
221
2
 
222
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1);
 
223
a
 
224
select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
 
225
b       (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
 
226
8       7.5000
 
227
8       4.5000
 
228
9       7.5000
 
229
explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
 
230
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
231
1       PRIMARY t4      ALL     NULL    NULL    NULL    NULL    3       100.00  
 
232
2       DEPENDENT SUBQUERY      t2      ALL     NULL    NULL    NULL    NULL    2       100.00  
 
233
3       DEPENDENT SUBQUERY      t3      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
234
Warnings:
 
235
Note    1276    Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
 
236
Note    1003    select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((select avg((`test`.`t2`.`a` + (select min(`test`.`t3`.`a`) from `test`.`t3` where (`test`.`t3`.`a` >= `test`.`t4`.`a`)))) from `test`.`t2`)) AS `(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)` from `test`.`t4`
 
237
select * from t3 where exists (select * from t2 where t2.b=t3.a);
 
238
a
 
239
7
 
240
select * from t3 where not exists (select * from t2 where t2.b=t3.a);
 
241
a
 
242
6
 
243
3
 
244
select * from t3 where a in (select b from t2);
 
245
a
 
246
7
 
247
select * from t3 where a not in (select b from t2);
 
248
a
 
249
6
 
250
3
 
251
select * from t3 where a = some (select b from t2);
 
252
a
 
253
7
 
254
select * from t3 where a <> any (select b from t2);
 
255
a
 
256
6
 
257
3
 
258
select * from t3 where a = all (select b from t2);
 
259
a
 
260
7
 
261
select * from t3 where a <> all (select b from t2);
 
262
a
 
263
6
 
264
3
 
265
insert into t2 values (100, 5);
 
266
select * from t3 where a < any (select b from t2);
 
267
a
 
268
6
 
269
3
 
270
select * from t3 where a < all (select b from t2);
 
271
a
 
272
3
 
273
select * from t3 where a >= any (select b from t2);
 
274
a
 
275
6
 
276
7
 
277
explain extended select * from t3 where a >= any (select b from t2);
 
278
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
279
1       PRIMARY t3      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
280
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    3       100.00  
 
281
Warnings:
 
282
Note    1003    select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(`test`.`t2`.`b`) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`))))
 
283
select * from t3 where a >= all (select b from t2);
 
284
a
 
285
7
 
286
delete from t2 where a=100;
 
287
select * from t3 where a in (select a,b from t2);
 
288
ERROR 21000: Operand should contain 1 column(s)
 
289
select * from t3 where a in (select * from t2);
 
290
ERROR 21000: Operand should contain 1 column(s)
 
291
insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10);
 
292
select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b);
 
293
b       ma
 
294
insert into t2 values (2,10);
 
295
select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b);
 
296
b       ma
 
297
10      1
 
298
delete from t2 where a=2 and b=10;
 
299
select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
 
300
b       ma
 
301
7       12
 
302
create table t5 (a int);
 
303
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
 
304
(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)       a
 
305
NULL    1
 
306
2       2
 
307
insert into t5 values (5);
 
308
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
 
309
(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)       a
 
310
NULL    1
 
311
2       2
 
312
insert into t5 values (2);
 
313
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
 
314
(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)       a
 
315
NULL    1
 
316
2       2
 
317
explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
 
318
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
319
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    2       100.00  
 
320
2       DEPENDENT SUBQUERY      t1      system  NULL    NULL    NULL    NULL    1       100.00  
 
321
3       DEPENDENT UNION t5      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where
 
322
NULL    UNION RESULT    <union2,3>      ALL     NULL    NULL    NULL    NULL    NULL    NULL    
 
323
Warnings:
 
324
Note    1276    Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
 
325
Note    1276    Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
 
326
Note    1003    select <expr_cache><`test`.`t2`.`a`>((select 2 from dual where (2 = `test`.`t2`.`a`) union select `test`.`t5`.`a` from `test`.`t5` where (`test`.`t5`.`a` = `test`.`t2`.`a`))) AS `(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)`,`test`.`t2`.`a` AS `a` from `test`.`t2`
 
327
select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
 
328
ERROR 21000: Subquery returns more than 1 row
 
329
create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
 
330
create table t7( uq int primary key, name char(25));
 
331
insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
 
332
insert into t6 values (1,1),(1,2),(2,2),(1,3);
 
333
select * from t6 where exists (select * from t7 where uq = clinic_uq);
 
334
patient_uq      clinic_uq
 
335
1       1
 
336
1       2
 
337
2       2
 
338
explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq);
 
339
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
340
1       PRIMARY t6      ALL     NULL    NULL    NULL    NULL    4       100.00  Using where
 
341
2       DEPENDENT SUBQUERY      t7      eq_ref  PRIMARY PRIMARY 4       test.t6.clinic_uq       1       100.00  Using index
 
342
Warnings:
 
343
Note    1276    Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1
 
344
Note    1003    select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <expr_cache><`test`.`t6`.`clinic_uq`>(exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`)))
 
345
select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
 
346
ERROR 23000: Column 'a' in field list is ambiguous
 
347
drop table t1,t2,t3;
 
348
CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0');
 
349
INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b');
 
350
CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0');
 
351
INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2');
 
352
CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00');
 
353
INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13');
 
354
SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1);
 
355
a       b
 
356
W       1732-02-22
 
357
SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2);
 
358
a       b
 
359
W       1
 
360
SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3);
 
361
a       b
 
362
W       a
 
363
CREATE TABLE `t8` (
 
364
`pseudo` varchar(35) character set latin1 NOT NULL default '',
 
365
`email` varchar(60) character set latin1 NOT NULL default '',
 
366
PRIMARY KEY  (`pseudo`),
 
367
UNIQUE KEY `email` (`email`)
 
368
) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
 
369
INSERT INTO t8 (pseudo,email) VALUES ('joce','test');
 
370
INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1');
 
371
INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1');
 
372
EXPLAIN EXTENDED SELECT pseudo,(SELECT email FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce')) FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
 
373
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
374
1       PRIMARY t8      const   PRIMARY PRIMARY 37      const   1       100.00  Using index
 
375
4       SUBQUERY        t8      const   PRIMARY PRIMARY 37      const   1       100.00  Using index
 
376
2       SUBQUERY        t8      const   PRIMARY PRIMARY 37      const   1       100.00  
 
377
3       SUBQUERY        t8      const   PRIMARY PRIMARY 37      const   1       100.00  Using index
 
378
Warnings:
 
379
Note    1003    select 'joce' AS `pseudo`,(select 'test' from `test`.`t8` where 1) AS `(SELECT email FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce'))` from `test`.`t8` where 1
 
380
SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM
 
381
t8 WHERE pseudo='joce');
 
382
ERROR 21000: Operand should contain 1 column(s)
 
383
SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE
 
384
pseudo='joce');
 
385
ERROR 21000: Operand should contain 1 column(s)
 
386
SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
 
387
pseudo
 
388
joce
 
389
SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%');
 
390
ERROR 21000: Subquery returns more than 1 row
 
391
drop table if exists t1,t2,t3,t4,t5,t6,t7,t8;
 
392
CREATE TABLE `t1` (
 
393
`topic` mediumint(8) unsigned NOT NULL default '0',
 
394
`date` date NOT NULL default '0000-00-00',
 
395
`pseudo` varchar(35) character set latin1 NOT NULL default '',
 
396
PRIMARY KEY  (`pseudo`,`date`,`topic`),
 
397
KEY `topic` (`topic`)
 
398
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
 
399
INSERT INTO t1 (topic,date,pseudo) VALUES
 
400
('43506','2002-10-02','joce'),('40143','2002-08-03','joce');
 
401
EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
 
402
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
403
1       SIMPLE  t1      index   NULL    PRIMARY 43      NULL    2       100.00  Using where; Using index
 
404
Warnings:
 
405
Note    1003    select distinct `test`.`t1`.`date` AS `date` from `test`.`t1` where (`test`.`t1`.`date` = '2002-08-03')
 
406
EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
 
407
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
408
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
409
2       SUBQUERY        t1      index   NULL    PRIMARY 43      NULL    2       100.00  Using where; Using index
 
410
Warnings:
 
411
Note    1003    select (select distinct `test`.`t1`.`date` from `test`.`t1` where (`test`.`t1`.`date` = '2002-08-03')) AS `(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')`
 
412
SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
 
413
date
 
414
2002-08-03
 
415
SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
 
416
(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')
 
417
2002-08-03
 
418
SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1;
 
419
1
 
420
1
 
421
1
 
422
1
 
423
SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1;
 
424
ERROR 21000: Subquery returns more than 1 row
 
425
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1);
 
426
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
427
1       PRIMARY t1      index   NULL    topic   3       NULL    2       100.00  Using index
 
428
2       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
429
3       UNION   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
430
NULL    UNION RESULT    <union2,3>      ALL     NULL    NULL    NULL    NULL    NULL    NULL    
 
431
Warnings:
 
432
Note    1003    select 1 AS `1` from `test`.`t1` where 1
 
433
drop table t1;
 
434
CREATE TABLE `t1` (
 
435
`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
 
436
`maxnumrep` int(10) unsigned NOT NULL default '0',
 
437
PRIMARY KEY  (`numeropost`),
 
438
UNIQUE KEY `maxnumrep` (`maxnumrep`)
 
439
) ENGINE=MyISAM ROW_FORMAT=FIXED;
 
440
INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2);
 
441
CREATE TABLE `t2` (
 
442
`mot` varchar(30) NOT NULL default '',
 
443
`topic` mediumint(8) unsigned NOT NULL default '0',
 
444
`date` date NOT NULL default '0000-00-00',
 
445
`pseudo` varchar(35) NOT NULL default '',
 
446
PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`)
 
447
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
 
448
INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce');
 
449
select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
 
450
a
 
451
40143
 
452
SELECT numeropost,maxnumrep FROM t1 WHERE exists (SELECT 1 FROM t2 WHERE (mot='joce') AND date >= '2002-10-21' AND t1.numeropost = t2.topic) ORDER BY maxnumrep DESC LIMIT 0, 20;
 
453
numeropost      maxnumrep
 
454
43506   2
 
455
40143   1
 
456
SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b;
 
457
ERROR 42S22: Unknown column 'a' in 'having clause'
 
458
SELECT 1 IN (SELECT 1 FROM t2 HAVING a);
 
459
ERROR 42S22: Unknown column 'a' in 'having clause'
 
460
SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic);
 
461
mot     topic   date    pseudo
 
462
joce    40143   2002-10-22      joce
 
463
joce    43506   2002-10-22      joce
 
464
SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
 
465
mot     topic   date    pseudo
 
466
SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1);
 
467
mot     topic   date    pseudo
 
468
SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic);
 
469
mot     topic   date    pseudo
 
470
joce    40143   2002-10-22      joce
 
471
joce    43506   2002-10-22      joce
 
472
SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
 
473
mot     topic   date    pseudo
 
474
SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1);
 
475
mot     topic   date    pseudo
 
476
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic);
 
477
mot     topic   date    pseudo
 
478
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
 
479
mot     topic   date    pseudo
 
480
joce    40143   2002-10-22      joce
 
481
joce    43506   2002-10-22      joce
 
482
SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
 
483
mot     topic   date    pseudo  topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
 
484
joce    40143   2002-10-22      joce    1
 
485
joce    43506   2002-10-22      joce    1
 
486
SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
 
487
mot     topic   date    pseudo
 
488
SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2);
 
489
mot     topic   date    pseudo
 
490
joce    40143   2002-10-22      joce
 
491
joce    43506   2002-10-22      joce
 
492
SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
 
493
mot     topic   date    pseudo
 
494
joce    40143   2002-10-22      joce
 
495
SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
 
496
mot     topic   date    pseudo
 
497
joce    40143   2002-10-22      joce
 
498
SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
 
499
mot     topic   date    pseudo
 
500
joce    40143   2002-10-22      joce
 
501
SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
 
502
mot     topic   date    pseudo  topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
 
503
joce    40143   2002-10-22      joce    1
 
504
joce    43506   2002-10-22      joce    0
 
505
drop table t1,t2;
 
506
CREATE TABLE `t1` (
 
507
`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
 
508
`maxnumrep` int(10) unsigned NOT NULL default '0',
 
509
PRIMARY KEY  (`numeropost`),
 
510
UNIQUE KEY `maxnumrep` (`maxnumrep`)
 
511
) ENGINE=MyISAM ROW_FORMAT=FIXED;
 
512
INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1);
 
513
select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
 
514
ERROR 21000: Subquery returns more than 1 row
 
515
select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1);
 
516
ERROR 21000: Subquery returns more than 1 row
 
517
show warnings;
 
518
Level   Code    Message
 
519
Error   1242    Subquery returns more than 1 row
 
520
Error   1028    Sort aborted: Subquery returns more than 1 row
 
521
drop table t1;
 
522
create table t1 (a int);
 
523
insert into t1 values (1),(2),(3);
 
524
(select * from t1) union (select * from t1) order by (select a from t1 limit 1);
 
525
a
 
526
1
 
527
2
 
528
3
 
529
drop table t1;
 
530
CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b');
 
531
INSERT INTO t1 VALUES ();
 
532
SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b');
 
533
ERROR 21000: Subquery returns more than 1 row
 
534
drop table t1;
 
535
CREATE TABLE `t1` (
 
536
`numeropost` mediumint(8) unsigned NOT NULL default '0',
 
537
`numreponse` int(10) unsigned NOT NULL auto_increment,
 
538
`pseudo` varchar(35) NOT NULL default '',
 
539
PRIMARY KEY  (`numeropost`,`numreponse`),
 
540
UNIQUE KEY `numreponse` (`numreponse`),
 
541
KEY `pseudo` (`pseudo`,`numeropost`)
 
542
) ENGINE=MyISAM;
 
543
SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
 
544
ERROR 42S22: Reference 'numreponse' not supported (forward reference in item list)
 
545
SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
 
546
ERROR 42S22: Unknown column 'a' in 'having clause'
 
547
SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
 
548
numreponse      (SELECT numeropost FROM t1 HAVING numreponse=1)
 
549
INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test');
 
550
EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
 
551
ERROR 21000: Subquery returns more than 1 row
 
552
SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
 
553
ERROR 21000: Subquery returns more than 1 row
 
554
EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1';
 
555
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
556
1       SIMPLE  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    Select tables optimized away
 
557
Warnings:
 
558
Note    1003    select max(`test`.`t1`.`numreponse`) AS `MAX(numreponse)` from `test`.`t1` where (`test`.`t1`.`numeropost` = '1')
 
559
EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1');
 
560
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
561
1       PRIMARY t1      const   PRIMARY,numreponse      PRIMARY 7       const,const     1       100.00  Using index
 
562
2       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    Select tables optimized away
 
563
Warnings:
 
564
Note    1003    select 3 AS `numreponse` from `test`.`t1` where ((1 = '1'))
 
565
drop table t1;
 
566
CREATE TABLE t1 (a int(1));
 
567
INSERT INTO t1 VALUES (1);
 
568
SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1;
 
569
1
 
570
1
 
571
drop table t1;
 
572
create table t1 (a int NOT NULL, b int, primary key (a));
 
573
create table t2 (a int NOT NULL, b int, primary key (a));
 
574
insert into t1 values (0, 10),(1, 11),(2, 12);
 
575
insert into t2 values (1, 21),(2, 22),(3, 23);
 
576
select * from t1;
 
577
a       b
 
578
0       10
 
579
1       11
 
580
2       12
 
581
update t1 set b= (select b from t1);
 
582
ERROR HY000: You can't specify target table 't1' for update in FROM clause
 
583
update t1 set b= (select b from t2);
 
584
ERROR 21000: Subquery returns more than 1 row
 
585
update t1 set b= (select b from t2 where t1.a = t2.a);
 
586
select * from t1;
 
587
a       b
 
588
0       NULL
 
589
1       21
 
590
2       22
 
591
drop table t1, t2;
 
592
create table t1 (a int NOT NULL, b int, primary key (a));
 
593
create table t2 (a int NOT NULL, b int, primary key (a));
 
594
insert into t1 values (0, 10),(1, 11),(2, 12);
 
595
insert into t2 values (1, 21),(2, 12),(3, 23);
 
596
select * from t1;
 
597
a       b
 
598
0       10
 
599
1       11
 
600
2       12
 
601
select * from t1 where b = (select b from t2 where t1.a = t2.a);
 
602
a       b
 
603
2       12
 
604
delete from t1 where b in (select b from t1);
 
605
ERROR HY000: You can't specify target table 't1' for update in FROM clause
 
606
delete from t1 where b = (select b from t2);
 
607
ERROR 21000: Subquery returns more than 1 row
 
608
delete from t1 where b = (select b from t2 where t1.a = t2.a);
 
609
select * from t1;
 
610
a       b
 
611
0       10
 
612
1       11
 
613
drop table t1, t2;
 
614
create table t11 (a int NOT NULL, b int, primary key (a));
 
615
create table t12 (a int NOT NULL, b int, primary key (a));
 
616
create table t2 (a int NOT NULL, b int, primary key (a));
 
617
insert into t11 values (0, 10),(1, 11),(2, 12);
 
618
insert into t12 values (33, 10),(22, 11),(2, 12);
 
619
insert into t2 values (1, 21),(2, 12),(3, 23);
 
620
select * from t11;
 
621
a       b
 
622
0       10
 
623
1       11
 
624
2       12
 
625
select * from t12;
 
626
a       b
 
627
33      10
 
628
22      11
 
629
2       12
 
630
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
 
631
ERROR HY000: You can't specify target table 't12' for update in FROM clause
 
632
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
 
633
ERROR 21000: Subquery returns more than 1 row
 
634
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
 
635
select * from t11;
 
636
a       b
 
637
0       10
 
638
1       11
 
639
select * from t12;
 
640
a       b
 
641
33      10
 
642
22      11
 
643
drop table t11, t12, t2;
 
644
CREATE TABLE t1 (x int) ENGINE=MyISAM;
 
645
create table t2 (a int) ENGINE=MyISAM;
 
646
create table t3 (b int);
 
647
insert into t2 values (1);
 
648
insert into t3 values (1),(2);
 
649
INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));
 
650
ERROR HY000: You can't specify target table 't1' for update in FROM clause
 
651
INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));
 
652
ERROR 21000: Subquery returns more than 1 row
 
653
INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));
 
654
select * from t1;
 
655
x
 
656
1
 
657
insert into t2 values (1);
 
658
INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
 
659
select * from t1;
 
660
x
 
661
1
 
662
2
 
663
INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;
 
664
select * from t1;
 
665
x
 
666
1
 
667
2
 
668
3
 
669
3
 
670
INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;
 
671
select * from t1;
 
672
x
 
673
1
 
674
2
 
675
3
 
676
3
 
677
11
 
678
11
 
679
INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
 
680
ERROR 42S22: Unknown column 'x' in 'field list'
 
681
INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
 
682
select * from t1;
 
683
x
 
684
1
 
685
2
 
686
3
 
687
3
 
688
11
 
689
11
 
690
2
 
691
drop table t1, t2, t3;
 
692
CREATE TABLE t1 (x int not null, y int, primary key (x)) ENGINE=MyISAM;
 
693
create table t2 (a int);
 
694
create table t3 (a int);
 
695
insert into t2 values (1);
 
696
insert into t3 values (1),(2);
 
697
select * from t1;
 
698
x       y
 
699
replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));
 
700
ERROR HY000: You can't specify target table 't1' for update in FROM clause
 
701
replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));
 
702
ERROR 21000: Subquery returns more than 1 row
 
703
replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));
 
704
select * from t1;
 
705
x       y
 
706
1       2
 
707
replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));
 
708
select * from t1;
 
709
x       y
 
710
1       3
 
711
replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
 
712
select * from t1;
 
713
x       y
 
714
1       3
 
715
4       1
 
716
replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
 
717
select * from t1;
 
718
x       y
 
719
1       3
 
720
4       2
 
721
replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
 
722
select * from t1;
 
723
x       y
 
724
1       3
 
725
4       2
 
726
2       1
 
727
drop table t1, t2, t3;
 
728
SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);
 
729
ERROR HY000: No tables used
 
730
CREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
 
731
INSERT INTO t2 VALUES (1),(2);
 
732
SELECT * FROM t2 WHERE id IN (SELECT 1);
 
733
id
 
734
1
 
735
EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);
 
736
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
737
1       PRIMARY t2      ref     id      id      5       const   1       100.00  Using index
 
738
Warnings:
 
739
Note    1249    Select 2 was reduced during optimization
 
740
Note    1003    select `test`.`t2`.`id` AS `id` from `test`.`t2` where (`test`.`t2`.`id` = 1)
 
741
SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
 
742
id
 
743
1
 
744
SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
 
745
id
 
746
2
 
747
EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
 
748
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
749
1       PRIMARY t2      ref     id      id      5       const   1       100.00  Using where; Using index
 
750
Warnings:
 
751
Note    1249    Select 3 was reduced during optimization
 
752
Note    1249    Select 2 was reduced during optimization
 
753
Note    1003    select `test`.`t2`.`id` AS `id` from `test`.`t2` where (`test`.`t2`.`id` = <cache>((1 + 1)))
 
754
EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
 
755
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
756
1       PRIMARY t2      index   NULL    id      5       NULL    2       100.00  Using where; Using index
 
757
2       DEPENDENT SUBQUERY      NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
758
3       DEPENDENT UNION NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
759
NULL    UNION RESULT    <union2,3>      ALL     NULL    NULL    NULL    NULL    NULL    NULL    
 
760
Warnings:
 
761
Note    1003    select `test`.`t2`.`id` AS `id` from `test`.`t2` where <expr_cache><`test`.`t2`.`id`>(<in_optimizer>(`test`.`t2`.`id`,<exists>(select 1 having (<cache>(`test`.`t2`.`id`) = <ref_null_helper>(1)) union select 3 having (<cache>(`test`.`t2`.`id`) = <ref_null_helper>(3)))))
 
762
SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);
 
763
id
 
764
SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);
 
765
id
 
766
2
 
767
INSERT INTO t2 VALUES ((SELECT * FROM t2));
 
768
ERROR HY000: You can't specify target table 't2' for update in FROM clause
 
769
INSERT INTO t2 VALUES ((SELECT id FROM t2));
 
770
ERROR HY000: You can't specify target table 't2' for update in FROM clause
 
771
SELECT * FROM t2;
 
772
id
 
773
1
 
774
2
 
775
CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
 
776
INSERT INTO t1 values (1),(1);
 
777
UPDATE t2 SET id=(SELECT * FROM t1);
 
778
ERROR 21000: Subquery returns more than 1 row
 
779
drop table t2, t1;
 
780
create table t1 (a int);
 
781
insert into t1 values (1),(2),(3);
 
782
select 1 IN (SELECT * from t1);
 
783
1 IN (SELECT * from t1)
 
784
1
 
785
select 10 IN (SELECT * from t1);
 
786
10 IN (SELECT * from t1)
 
787
0
 
788
select NULL IN (SELECT * from t1);
 
789
NULL IN (SELECT * from t1)
 
790
NULL
 
791
update t1 set a=NULL where a=2;
 
792
select 1 IN (SELECT * from t1);
 
793
1 IN (SELECT * from t1)
 
794
1
 
795
select 3 IN (SELECT * from t1);
 
796
3 IN (SELECT * from t1)
 
797
1
 
798
select 10 IN (SELECT * from t1);
 
799
10 IN (SELECT * from t1)
 
800
NULL
 
801
select 1 > ALL (SELECT * from t1);
 
802
1 > ALL (SELECT * from t1)
 
803
0
 
804
select 10 > ALL (SELECT * from t1);
 
805
10 > ALL (SELECT * from t1)
 
806
NULL
 
807
select 1 > ANY (SELECT * from t1);
 
808
1 > ANY (SELECT * from t1)
 
809
NULL
 
810
select 10 > ANY (SELECT * from t1);
 
811
10 > ANY (SELECT * from t1)
 
812
1
 
813
drop table t1;
 
814
create table t1 (a varchar(20));
 
815
insert into t1 values ('A'),('BC'),('DEF');
 
816
select 'A' IN (SELECT * from t1);
 
817
'A' IN (SELECT * from t1)
 
818
1
 
819
select 'XYZS' IN (SELECT * from t1);
 
820
'XYZS' IN (SELECT * from t1)
 
821
0
 
822
select NULL IN (SELECT * from t1);
 
823
NULL IN (SELECT * from t1)
 
824
NULL
 
825
update t1 set a=NULL where a='BC';
 
826
select 'A' IN (SELECT * from t1);
 
827
'A' IN (SELECT * from t1)
 
828
1
 
829
select 'DEF' IN (SELECT * from t1);
 
830
'DEF' IN (SELECT * from t1)
 
831
1
 
832
select 'XYZS' IN (SELECT * from t1);
 
833
'XYZS' IN (SELECT * from t1)
 
834
NULL
 
835
select 'A' > ALL (SELECT * from t1);
 
836
'A' > ALL (SELECT * from t1)
 
837
0
 
838
select 'XYZS' > ALL (SELECT * from t1);
 
839
'XYZS' > ALL (SELECT * from t1)
 
840
NULL
 
841
select 'A' > ANY (SELECT * from t1);
 
842
'A' > ANY (SELECT * from t1)
 
843
NULL
 
844
select 'XYZS' > ANY (SELECT * from t1);
 
845
'XYZS' > ANY (SELECT * from t1)
 
846
1
 
847
drop table t1;
 
848
create table t1 (a float);
 
849
insert into t1 values (1.5),(2.5),(3.5);
 
850
select 1.5 IN (SELECT * from t1);
 
851
1.5 IN (SELECT * from t1)
 
852
1
 
853
select 10.5 IN (SELECT * from t1);
 
854
10.5 IN (SELECT * from t1)
 
855
0
 
856
select NULL IN (SELECT * from t1);
 
857
NULL IN (SELECT * from t1)
 
858
NULL
 
859
update t1 set a=NULL where a=2.5;
 
860
select 1.5 IN (SELECT * from t1);
 
861
1.5 IN (SELECT * from t1)
 
862
1
 
863
select 3.5 IN (SELECT * from t1);
 
864
3.5 IN (SELECT * from t1)
 
865
1
 
866
select 10.5 IN (SELECT * from t1);
 
867
10.5 IN (SELECT * from t1)
 
868
NULL
 
869
select 1.5 > ALL (SELECT * from t1);
 
870
1.5 > ALL (SELECT * from t1)
 
871
0
 
872
select 10.5 > ALL (SELECT * from t1);
 
873
10.5 > ALL (SELECT * from t1)
 
874
NULL
 
875
select 1.5 > ANY (SELECT * from t1);
 
876
1.5 > ANY (SELECT * from t1)
 
877
NULL
 
878
select 10.5 > ANY (SELECT * from t1);
 
879
10.5 > ANY (SELECT * from t1)
 
880
1
 
881
explain extended select (select a+1) from t1;
 
882
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
883
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  
 
884
Warnings:
 
885
Note    1276    Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
 
886
Note    1249    Select 2 was reduced during optimization
 
887
Note    1003    select (`test`.`t1`.`a` + 1) AS `(select a+1)` from `test`.`t1`
 
888
select (select a+1) from t1;
 
889
(select a+1)
 
890
2.5
 
891
NULL
 
892
4.5
 
893
drop table t1;
 
894
CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY  (a));
 
895
CREATE TABLE t2 (a int(11) default '0', INDEX (a));
 
896
INSERT INTO t1 VALUES (1),(2),(3),(4);
 
897
INSERT INTO t2 VALUES (1),(2),(3);
 
898
SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
 
899
a       t1.a in (select t2.a from t2)
 
900
1       1
 
901
2       1
 
902
3       1
 
903
4       0
 
904
explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
 
905
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
906
1       PRIMARY t1      index   NULL    PRIMARY 4       NULL    4       100.00  Using index
 
907
2       SUBQUERY        t2      index_subquery  a       a       5       func    2       100.00  Using index
 
908
Warnings:
 
909
Note    1003    select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in t2 on a checking NULL having <is_not_null_test>(`test`.`t2`.`a`))))) AS `t1.a in (select t2.a from t2)` from `test`.`t1`
 
910
CREATE TABLE t3 (a int(11) default '0');
 
911
INSERT INTO t3 VALUES (1),(2),(3);
 
912
SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
 
913
a       t1.a in (select t2.a from t2,t3 where t3.a=t2.a)
 
914
1       1
 
915
2       1
 
916
3       1
 
917
4       0
 
918
explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
 
919
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
920
1       PRIMARY t1      index   NULL    PRIMARY 4       NULL    4       100.00  Using index
 
921
2       DEPENDENT SUBQUERY      t2      ref_or_null     a       a       5       func    2       100.00  Using where; Using index
 
922
2       DEPENDENT SUBQUERY      t3      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where; Using join buffer (flat, BNL join)
 
923
Warnings:
 
924
Note    1003    select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(select `test`.`t2`.`a` from `test`.`t2` join `test`.`t3` where ((`test`.`t3`.`a` = `test`.`t2`.`a`) and ((<cache>(`test`.`t1`.`a`) = `test`.`t2`.`a`) or isnull(`test`.`t2`.`a`))) having <is_not_null_test>(`test`.`t2`.`a`)))) AS `t1.a in (select t2.a from t2,t3 where t3.a=t2.a)` from `test`.`t1`
 
925
drop table t1,t2,t3;
 
926
# check correct NULL Processing for normal IN/ALL/ANY
 
927
# and 2 ways of max/min optimization
 
928
create table t1 (a int);
 
929
insert into t1 values (1), (100), (NULL), (1000);
 
930
create table t2 (a int not null);
 
931
# subselect returns empty set (for NULL and non-NULL left part)
 
932
select a, a in (select * from t2) from t1;
 
933
a       a in (select * from t2)
 
934
1       0
 
935
100     0
 
936
NULL    0
 
937
1000    0
 
938
select a, a > any (select * from t2) from t1;
 
939
a       a > any (select * from t2)
 
940
1       0
 
941
100     0
 
942
NULL    0
 
943
1000    0
 
944
select a, a > all (select * from t2) from t1;
 
945
a       a > all (select * from t2)
 
946
1       1
 
947
100     1
 
948
NULL    1
 
949
1000    1
 
950
select a from t1 where a in (select * from t2);
 
951
a
 
952
select a from t1 where a > any (select * from t2);
 
953
a
 
954
select a from t1 where a > all (select * from t2);
 
955
a
 
956
1
 
957
100
 
958
NULL
 
959
1000
 
960
select a from t1 where a in (select * from t2 group by a);
 
961
a
 
962
select a from t1 where a > any (select * from t2 group by a);
 
963
a
 
964
select a from t1 where a > all (select * from t2 group by a);
 
965
a
 
966
1
 
967
100
 
968
NULL
 
969
1000
 
970
insert into t2 values (1),(200);
 
971
# sebselect returns non-empty set without NULLs
 
972
select a, a in (select * from t2) from t1;
 
973
a       a in (select * from t2)
 
974
1       1
 
975
100     0
 
976
NULL    NULL
 
977
1000    0
 
978
select a, a > any (select * from t2) from t1;
 
979
a       a > any (select * from t2)
 
980
1       0
 
981
100     1
 
982
NULL    NULL
 
983
1000    1
 
984
select a, a > all (select * from t2) from t1;
 
985
a       a > all (select * from t2)
 
986
1       0
 
987
100     0
 
988
NULL    NULL
 
989
1000    1
 
990
select a from t1 where a in (select * from t2);
 
991
a
 
992
1
 
993
select a from t1 where a > any (select * from t2);
 
994
a
 
995
100
 
996
1000
 
997
select a from t1 where a > all (select * from t2);
 
998
a
 
999
1000
 
1000
select a from t1 where a in (select * from t2 group by a);
 
1001
a
 
1002
1
 
1003
select a from t1 where a > any (select * from t2 group by a);
 
1004
a
 
1005
100
 
1006
1000
 
1007
select a from t1 where a > all (select * from t2 group by a);
 
1008
a
 
1009
1000
 
1010
drop table t2;
 
1011
create table t2 (a int);
 
1012
insert into t2 values (1),(NULL),(200);
 
1013
# sebselect returns non-empty set with NULLs
 
1014
select a, a in (select * from t2) from t1;
 
1015
a       a in (select * from t2)
 
1016
1       1
 
1017
100     NULL
 
1018
NULL    NULL
 
1019
1000    NULL
 
1020
select a, a > any (select * from t2) from t1;
 
1021
a       a > any (select * from t2)
 
1022
1       NULL
 
1023
100     1
 
1024
NULL    NULL
 
1025
1000    1
 
1026
select a, a > all (select * from t2) from t1;
 
1027
a       a > all (select * from t2)
 
1028
1       0
 
1029
100     0
 
1030
NULL    NULL
 
1031
1000    NULL
 
1032
select a from t1 where a in (select * from t2);
 
1033
a
 
1034
1
 
1035
select a from t1 where a > any (select * from t2);
 
1036
a
 
1037
100
 
1038
1000
 
1039
select a from t1 where a > all (select * from t2);
 
1040
a
 
1041
select a from t1 where a in (select * from t2 group by a);
 
1042
a
 
1043
1
 
1044
select a from t1 where a > any (select * from t2 group by a);
 
1045
a
 
1046
100
 
1047
1000
 
1048
select a from t1 where a > all (select * from t2 group by a);
 
1049
a
 
1050
drop table t1, t2;
 
1051
create table t1 (a float);
 
1052
select 10.5 IN (SELECT * from t1 LIMIT 1);
 
1053
ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
 
1054
select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
 
1055
ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
 
1056
drop table t1;
 
1057
create table t1 (a int, b int, c varchar(10));
 
1058
create table t2 (a int);
 
1059
insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
 
1060
insert into t2 values (1),(2),(NULL);
 
1061
select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),(select c from t1 where a=t2.a)  from t2;
 
1062
a       (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a')   (select c from t1 where a=t2.a)
 
1063
1       1       a
 
1064
2       0       b
 
1065
NULL    NULL    NULL
 
1066
select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b'),(select c from t1 where a=t2.a) from t2;
 
1067
a       (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b')   (select c from t1 where a=t2.a)
 
1068
1       0       a
 
1069
2       1       b
 
1070
NULL    NULL    NULL
 
1071
select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c'),(select c from t1 where a=t2.a) from t2;
 
1072
a       (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c')   (select c from t1 where a=t2.a)
 
1073
1       0       a
 
1074
2       0       b
 
1075
NULL    NULL    NULL
 
1076
drop table t1,t2;
 
1077
create table t1 (a int, b real, c varchar(10));
 
1078
insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
 
1079
select ROW(1, 1, 'a') IN (select a,b,c from t1);
 
1080
ROW(1, 1, 'a') IN (select a,b,c from t1)
 
1081
1
 
1082
select ROW(1, 2, 'a') IN (select a,b,c from t1);
 
1083
ROW(1, 2, 'a') IN (select a,b,c from t1)
 
1084
0
 
1085
select ROW(1, 1, 'a') IN (select b,a,c from t1);
 
1086
ROW(1, 1, 'a') IN (select b,a,c from t1)
 
1087
1
 
1088
select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
 
1089
ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null)
 
1090
1
 
1091
select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
 
1092
ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null)
 
1093
0
 
1094
select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
 
1095
ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null)
 
1096
1
 
1097
select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
 
1098
ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a')
 
1099
1
 
1100
select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
 
1101
ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a')
 
1102
0
 
1103
select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
 
1104
ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a')
 
1105
1
 
1106
select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
 
1107
ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
 
1108
drop table t1;
 
1109
create table t1 (a int);
 
1110
insert into t1 values (1);
 
1111
do @a:=(SELECT a from t1);
 
1112
select @a;
 
1113
@a
 
1114
1
 
1115
set @a:=2;
 
1116
set @a:=(SELECT a from t1);
 
1117
select @a;
 
1118
@a
 
1119
1
 
1120
drop table t1;
 
1121
do (SELECT a from t1);
 
1122
ERROR 42S02: Table 'test.t1' doesn't exist
 
1123
set @a:=(SELECT a from t1);
 
1124
ERROR 42S02: Table 'test.t1' doesn't exist
 
1125
CREATE TABLE t1 (a int, KEY(a));
 
1126
HANDLER t1 OPEN;
 
1127
HANDLER t1 READ a=((SELECT 1));
 
1128
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1))' at line 1
 
1129
HANDLER t1 CLOSE;
 
1130
drop table t1;
 
1131
create table t1 (a int);
 
1132
create table t2 (b int);
 
1133
insert into t1 values (1),(2);
 
1134
insert into t2 values (1);
 
1135
select a from t1 where a in (select a from t1 where a in (select b from t2));
 
1136
a
 
1137
1
 
1138
drop table t1, t2;
 
1139
create table t1 (a int, b int);
 
1140
create table t2 like t1;
 
1141
insert into t1 values (1,2),(1,3),(1,4),(1,5);
 
1142
insert into t2 values (1,2),(1,3);
 
1143
select * from t1 where row(a,b) in (select a,b from t2);
 
1144
a       b
 
1145
1       2
 
1146
1       3
 
1147
drop table t1, t2;
 
1148
CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY  (`i`)) ENGINE=MyISAM CHARSET=latin1;
 
1149
INSERT INTO t1 VALUES (1);
 
1150
UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
 
1151
select * from t1;
 
1152
i
 
1153
2
 
1154
drop table t1;
 
1155
CREATE TABLE t1 (a int(1));
 
1156
EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
 
1157
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1158
1       PRIMARY t1      system  NULL    NULL    NULL    NULL    0       0.00    const row not found
 
1159
2       UNCACHEABLE SUBQUERY    t1      system  NULL    NULL    NULL    NULL    0       0.00    const row not found
 
1160
Warnings:
 
1161
Note    1003    select (select rand() from `test`.`t1`) AS `(SELECT RAND() FROM t1)` from `test`.`t1`
 
1162
EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1;
 
1163
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1164
1       PRIMARY t1      system  NULL    NULL    NULL    NULL    0       0.00    const row not found
 
1165
2       UNCACHEABLE SUBQUERY    t1      system  NULL    NULL    NULL    NULL    0       0.00    const row not found
 
1166
Warnings:
 
1167
Note    1003    select (select encrypt('test') from `test`.`t1`) AS `(SELECT ENCRYPT('test') FROM t1)` from `test`.`t1`
 
1168
EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
 
1169
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1170
1       PRIMARY t1      system  NULL    NULL    NULL    NULL    0       0.00    const row not found
 
1171
2       UNCACHEABLE SUBQUERY    t1      system  NULL    NULL    NULL    NULL    0       0.00    const row not found
 
1172
Warnings:
 
1173
Note    1003    select (select benchmark(1,1) from `test`.`t1`) AS `(SELECT BENCHMARK(1,1) FROM t1)` from `test`.`t1`
 
1174
drop table t1;
 
1175
CREATE TABLE `t1` (
 
1176
`mot` varchar(30) character set latin1 NOT NULL default '',
 
1177
`topic` mediumint(8) unsigned NOT NULL default '0',
 
1178
`date` date NOT NULL default '0000-00-00',
 
1179
`pseudo` varchar(35) character set latin1 NOT NULL default '',
 
1180
PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),
 
1181
KEY `pseudo` (`pseudo`,`date`,`topic`),
 
1182
KEY `topic` (`topic`)
 
1183
) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
 
1184
CREATE TABLE `t2` (
 
1185
`mot` varchar(30) character set latin1 NOT NULL default '',
 
1186
`topic` mediumint(8) unsigned NOT NULL default '0',
 
1187
`date` date NOT NULL default '0000-00-00',
 
1188
`pseudo` varchar(35) character set latin1 NOT NULL default '',
 
1189
PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),
 
1190
KEY `pseudo` (`pseudo`,`date`,`topic`),
 
1191
KEY `topic` (`topic`)
 
1192
) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
 
1193
CREATE TABLE `t3` (
 
1194
`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
 
1195
`maxnumrep` int(10) unsigned NOT NULL default '0',
 
1196
PRIMARY KEY  (`numeropost`),
 
1197
UNIQUE KEY `maxnumrep` (`maxnumrep`)
 
1198
) ENGINE=MyISAM CHARSET=latin1;
 
1199
INSERT INTO t1 VALUES ('joce','1','','joce'),('test','2','','test');
 
1200
Warnings:
 
1201
Warning 1265    Data truncated for column 'date' at row 1
 
1202
Warning 1265    Data truncated for column 'date' at row 2
 
1203
INSERT INTO t2 VALUES ('joce','1','','joce'),('test','2','','test');
 
1204
Warnings:
 
1205
Warning 1265    Data truncated for column 'date' at row 1
 
1206
Warning 1265    Data truncated for column 'date' at row 2
 
1207
INSERT INTO t3 VALUES (1,1);
 
1208
SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE
 
1209
numeropost=topic);
 
1210
topic
 
1211
2
 
1212
select * from t1;
 
1213
mot     topic   date    pseudo
 
1214
joce    1       0000-00-00      joce
 
1215
test    2       0000-00-00      test
 
1216
DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT
 
1217
EXISTS(SELECT * FROM t3 WHERE numeropost=topic));
 
1218
select * from t1;
 
1219
mot     topic   date    pseudo
 
1220
joce    1       0000-00-00      joce
 
1221
drop table t1, t2, t3;
 
1222
SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
 
1223
a       (SELECT a)
 
1224
1       1
 
1225
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
 
1226
SHOW CREATE TABLE t1;
 
1227
Table   Create Table
 
1228
t1      CREATE TABLE `t1` (
 
1229
  `a` int(1) NOT NULL DEFAULT '0',
 
1230
  `(SELECT 1)` int(1) NOT NULL DEFAULT '0'
 
1231
) ENGINE=MyISAM DEFAULT CHARSET=latin1
 
1232
drop table t1;
 
1233
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
 
1234
SHOW CREATE TABLE t1;
 
1235
Table   Create Table
 
1236
t1      CREATE TABLE `t1` (
 
1237
  `a` int(1) NOT NULL DEFAULT '0',
 
1238
  `(SELECT a)` int(1) NOT NULL DEFAULT '0'
 
1239
) ENGINE=MyISAM DEFAULT CHARSET=latin1
 
1240
drop table t1;
 
1241
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
 
1242
SHOW CREATE TABLE t1;
 
1243
Table   Create Table
 
1244
t1      CREATE TABLE `t1` (
 
1245
  `a` int(1) NOT NULL DEFAULT '0',
 
1246
  `(SELECT a+0)` int(3) NOT NULL DEFAULT '0'
 
1247
) ENGINE=MyISAM DEFAULT CHARSET=latin1
 
1248
drop table t1;
 
1249
CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
 
1250
select * from t1;
 
1251
a
 
1252
2
 
1253
SHOW CREATE TABLE t1;
 
1254
Table   Create Table
 
1255
t1      CREATE TABLE `t1` (
 
1256
  `a` bigint(20) NOT NULL DEFAULT '0'
 
1257
) ENGINE=MyISAM DEFAULT CHARSET=latin1
 
1258
drop table t1;
 
1259
create table t1 (a int);
 
1260
insert into t1 values (1), (2), (3);
 
1261
explain extended select a,(select (select rand() from t1 limit 1)  from t1 limit 1)
 
1262
from t1;
 
1263
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1264
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    3       100.00  
 
1265
2       UNCACHEABLE SUBQUERY    t1      ALL     NULL    NULL    NULL    NULL    3       100.00  
 
1266
3       UNCACHEABLE SUBQUERY    t1      ALL     NULL    NULL    NULL    NULL    3       100.00  
 
1267
Warnings:
 
1268
Note    1003    select `test`.`t1`.`a` AS `a`,(select (select rand() from `test`.`t1` limit 1) from `test`.`t1` limit 1) AS `(select (select rand() from t1 limit 1)  from t1 limit 1)` from `test`.`t1`
 
1269
drop table t1;
 
1270
select t1.Continent, t2.Name, t2.Population from t1 LEFT JOIN t2 ON t1.Code = t2.Country  where t2.Population IN (select max(t2.Population) AS Population from t2, t1 where t2.Country = t1.Code group by Continent);
 
1271
ERROR 42S02: Table 'test.t1' doesn't exist
 
1272
CREATE TABLE t1 (
 
1273
ID int(11) NOT NULL auto_increment,
 
1274
name char(35) NOT NULL default '',
 
1275
t2 char(3) NOT NULL default '',
 
1276
District char(20) NOT NULL default '',
 
1277
Population int(11) NOT NULL default '0',
 
1278
PRIMARY KEY  (ID)
 
1279
) ENGINE=MyISAM;
 
1280
INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);
 
1281
INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);
 
1282
INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);
 
1283
CREATE TABLE t2 (
 
1284
Code char(3) NOT NULL default '',
 
1285
Name char(52) NOT NULL default '',
 
1286
Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
 
1287
Region char(26) NOT NULL default '',
 
1288
SurfaceArea float(10,2) NOT NULL default '0.00',
 
1289
IndepYear smallint(6) default NULL,
 
1290
Population int(11) NOT NULL default '0',
 
1291
LifeExpectancy float(3,1) default NULL,
 
1292
GNP float(10,2) default NULL,
 
1293
GNPOld float(10,2) default NULL,
 
1294
LocalName char(45) NOT NULL default '',
 
1295
GovernmentForm char(45) NOT NULL default '',
 
1296
HeadOfState char(60) default NULL,
 
1297
Capital int(11) default NULL,
 
1298
Code2 char(2) NOT NULL default '',
 
1299
PRIMARY KEY  (Code)
 
1300
) ENGINE=MyISAM;
 
1301
INSERT INTO t2 VALUES ('AUS','Australia','Oceania','Australia and New Zealand',7741220.00,1901,18886000,79.8,351182.00,392911.00,'Australia','Constitutional Monarchy, Federation','Elisabeth II',135,'AU');
 
1302
INSERT INTO t2 VALUES ('AZE','Azerbaijan','Asia','Middle East',86600.00,1991,7734000,62.9,4127.00,4100.00,'Azärbaycan','Federal Republic','Heydär Äliyev',144,'AZ');
 
1303
select t2.Continent, t1.Name, t1.Population from t2 LEFT JOIN t1 ON t2.Code = t1.t2  where t1.Population IN (select max(t1.Population) AS Population from t1, t2 where t1.t2 = t2.Code group by Continent);
 
1304
Continent       Name    Population
 
1305
Oceania Sydney  3276207
 
1306
drop table t1, t2;
 
1307
CREATE TABLE `t1` (
 
1308
`id` mediumint(8) unsigned NOT NULL auto_increment,
 
1309
`pseudo` varchar(35) character set latin1 NOT NULL default '',
 
1310
PRIMARY KEY  (`id`),
 
1311
UNIQUE KEY `pseudo` (`pseudo`)
 
1312
) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
 
1313
INSERT INTO t1 (pseudo) VALUES ('test');
 
1314
SELECT 0 IN (SELECT 1 FROM t1 a);
 
1315
0 IN (SELECT 1 FROM t1 a)
 
1316
0
 
1317
EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
 
1318
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1319
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
1320
2       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
1321
Warnings:
 
1322
Note    1003    select <in_optimizer>(0,<exists>(select 1 from dual where (0 = 1))) AS `0 IN (SELECT 1 FROM t1 a)`
 
1323
INSERT INTO t1 (pseudo) VALUES ('test1');
 
1324
SELECT 0 IN (SELECT 1 FROM t1 a);
 
1325
0 IN (SELECT 1 FROM t1 a)
 
1326
0
 
1327
EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
 
1328
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1329
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
1330
2       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
1331
Warnings:
 
1332
Note    1003    select <in_optimizer>(0,<exists>(select 1 from `test`.`t1` `a` where (0 = 1))) AS `0 IN (SELECT 1 FROM t1 a)`
 
1333
drop table t1;
 
1334
CREATE TABLE `t1` (
 
1335
`i` int(11) NOT NULL default '0',
 
1336
PRIMARY KEY  (`i`)
 
1337
) ENGINE=MyISAM CHARSET=latin1;
 
1338
INSERT INTO t1 VALUES (1);
 
1339
UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));
 
1340
UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
 
1341
UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);
 
1342
ERROR 42S22: Unknown column 't.i' in 'field list'
 
1343
select * from t1;
 
1344
i
 
1345
3
 
1346
drop table t1;
 
1347
CREATE TABLE t1 (
 
1348
id int(11) default NULL
 
1349
) ENGINE=MyISAM CHARSET=latin1;
 
1350
INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);
 
1351
CREATE TABLE t2 (
 
1352
id int(11) default NULL,
 
1353
name varchar(15) default NULL
 
1354
) ENGINE=MyISAM CHARSET=latin1;
 
1355
INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita');
 
1356
update t1, t2 set t2.name='lenka' where t2.id in (select id from t1);
 
1357
select * from t2;
 
1358
id      name
 
1359
4       vita
 
1360
1       lenka
 
1361
2       lenka
 
1362
1       lenka
 
1363
drop table t1,t2;
 
1364
create table t1 (a int, unique index indexa (a));
 
1365
insert into t1 values (-1), (-4), (-2), (NULL);
 
1366
select -10 IN (select a from t1 FORCE INDEX (indexa));
 
1367
-10 IN (select a from t1 FORCE INDEX (indexa))
 
1368
NULL
 
1369
drop table t1;
 
1370
create table t1 (id int not null auto_increment primary key, salary int, key(salary));
 
1371
insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);
 
1372
explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1);
 
1373
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1374
1       PRIMARY t1      ref     salary  salary  5       const   1       100.00  Using where
 
1375
2       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    Select tables optimized away
 
1376
Warnings:
 
1377
Note    1003    select `test`.`t1`.`id` AS `id` from `test`.`t1` where (`test`.`t1`.`salary` = (select max(`test`.`t1`.`salary`) from `test`.`t1`))
 
1378
drop table t1;
 
1379
CREATE TABLE t1 (
 
1380
ID int(10) unsigned NOT NULL auto_increment,
 
1381
SUB_ID int(3) unsigned NOT NULL default '0',
 
1382
REF_ID int(10) unsigned default NULL,
 
1383
REF_SUB int(3) unsigned default '0',
 
1384
PRIMARY KEY (ID,SUB_ID),
 
1385
UNIQUE KEY t1_PK (ID,SUB_ID),
 
1386
KEY t1_FK (REF_ID,REF_SUB),
 
1387
KEY t1_REFID (REF_ID)
 
1388
) ENGINE=MyISAM CHARSET=cp1251;
 
1389
INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);
 
1390
SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);
 
1391
REF_ID
 
1392
DROP TABLE t1;
 
1393
create table t1 (a int, b int);
 
1394
create table t2 (a int, b int);
 
1395
insert into t1 values (1,0), (2,0), (3,0);
 
1396
insert into t2 values (1,1), (2,1), (3,1), (2,2);
 
1397
update ignore t1 set b=(select b from t2 where t1.a=t2.a);
 
1398
Warnings:
 
1399
Warning 1242    Subquery returns more than 1 row
 
1400
select * from t1;
 
1401
a       b
 
1402
1       1
 
1403
2       NULL
 
1404
3       1
 
1405
drop table t1, t2;
 
1406
CREATE TABLE `t1` (
 
1407
`id` mediumint(8) unsigned NOT NULL auto_increment,
 
1408
`pseudo` varchar(35) NOT NULL default '',
 
1409
`email` varchar(60) NOT NULL default '',
 
1410
PRIMARY KEY  (`id`),
 
1411
UNIQUE KEY `email` (`email`),
 
1412
UNIQUE KEY `pseudo` (`pseudo`)
 
1413
) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
 
1414
INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');
 
1415
SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1);
 
1416
a       b
 
1417
test    test
 
1418
test1   test1
 
1419
drop table if exists t1;
 
1420
(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);
 
1421
a
 
1422
1
 
1423
create table t1 (a int not null, b int, primary key (a));
 
1424
create table t2 (a int not null, primary key (a));
 
1425
create table t3 (a int not null, b int, primary key (a));
 
1426
insert into t1 values (1,10), (2,20), (3,30),  (4,40);
 
1427
insert into t2 values (2), (3), (4), (5);
 
1428
insert into t3 values (10,3), (20,4), (30,5);
 
1429
select * from t2 where t2.a in (select a from t1);
 
1430
a
 
1431
2
 
1432
3
 
1433
4
 
1434
explain extended select * from t2 where t2.a in (select a from t1);
 
1435
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1436
1       PRIMARY t2      index   PRIMARY PRIMARY 4       NULL    4       100.00  Using index
 
1437
1       PRIMARY t1      index   PRIMARY PRIMARY 4       NULL    4       75.00   Using where; Using index; Using join buffer (flat, BNL join)
 
1438
Warnings:
 
1439
Note    1003    select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)
 
1440
select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 
1441
a
 
1442
2
 
1443
4
 
1444
explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 
1445
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1446
1       PRIMARY t2      index   PRIMARY PRIMARY 4       NULL    4       100.00  Using index
 
1447
1       PRIMARY t1      ALL     PRIMARY NULL    NULL    NULL    4       75.00   Using where; Using join buffer (flat, BNL join)
 
1448
Warnings:
 
1449
Note    1003    select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
 
1450
select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
 
1451
a
 
1452
2
 
1453
3
 
1454
explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
 
1455
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1456
1       PRIMARY t2      index   PRIMARY PRIMARY 4       NULL    4       100.00  Using index
 
1457
1       PRIMARY t1      ALL     PRIMARY NULL    NULL    NULL    4       75.00   Using where; Using join buffer (flat, BNL join)
 
1458
1       PRIMARY t3      eq_ref  PRIMARY PRIMARY 4       test.t1.b       1       100.00  Using index
 
1459
Warnings:
 
1460
Note    1003    select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t3` join `test`.`t2` where ((`test`.`t3`.`a` = `test`.`t1`.`b`) and (`test`.`t1`.`a` = `test`.`t2`.`a`))
 
1461
drop table t1, t2, t3;
 
1462
create table t1 (a int, b int, index a (a,b));
 
1463
create table t2 (a int, index a (a));
 
1464
create table t3 (a int, b int, index a (a));
 
1465
insert into t1 values (1,10), (2,20), (3,30), (4,40);
 
1466
create table t0(a int);
 
1467
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
 
1468
insert into t1
 
1469
select rand()*100000+200,rand()*100000 from t0 A, t0 B, t0 C, t0 D;
 
1470
insert into t2 values (2), (3), (4), (5);
 
1471
insert into t3 values (10,3), (20,4), (30,5);
 
1472
select * from t2 where t2.a in (select a from t1);
 
1473
a
 
1474
2
 
1475
3
 
1476
4
 
1477
explain extended select * from t2 where t2.a in (select a from t1);
 
1478
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1479
1       PRIMARY t2      index   a       a       5       NULL    4       100.00  Using where; Using index
 
1480
1       PRIMARY t1      ref     a       a       5       test.t2.a       101     100.00  Using index; FirstMatch(t2)
 
1481
Warnings:
 
1482
Note    1003    select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where (`test`.`t1`.`a` = `test`.`t2`.`a`)
 
1483
select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 
1484
a
 
1485
2
 
1486
4
 
1487
explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 
1488
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1489
1       PRIMARY t2      index   a       a       5       NULL    4       100.00  Using where; Using index
 
1490
1       PRIMARY t1      ref     a       a       5       test.t2.a       101     100.00  Using where; Using index; FirstMatch(t2)
 
1491
Warnings:
 
1492
Note    1003    select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
 
1493
select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
 
1494
a
 
1495
2
 
1496
3
 
1497
explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
 
1498
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1499
1       PRIMARY t2      index   a       a       5       NULL    4       100.00  Using where; Using index
 
1500
1       PRIMARY t3      index   a       a       5       NULL    3       100.00  Using where; Using index
 
1501
1       PRIMARY t1      ref     a       a       10      test.t2.a,test.t3.a     116     100.00  Using index; FirstMatch(t2)
 
1502
Warnings:
 
1503
Note    1003    select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1` join `test`.`t3`) where ((`test`.`t1`.`b` = `test`.`t3`.`a`) and (`test`.`t1`.`a` = `test`.`t2`.`a`))
 
1504
insert into t1 values (3,31);
 
1505
select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 
1506
a
 
1507
2
 
1508
3
 
1509
4
 
1510
select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31);
 
1511
a
 
1512
2
 
1513
4
 
1514
explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
 
1515
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1516
1       PRIMARY t2      index   a       a       5       NULL    4       100.00  Using where; Using index
 
1517
1       PRIMARY t1      ref     a       a       5       test.t2.a       101     100.00  Using where; Using index; FirstMatch(t2)
 
1518
Warnings:
 
1519
Note    1003    select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
 
1520
drop table t0, t1, t2, t3;
 
1521
create table t1 (a int, b int);
 
1522
create table t2 (a int, b int);
 
1523
create table t3 (a int, b int);
 
1524
insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
 
1525
insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
 
1526
insert into t3 values (3,3), (2,2), (1,1);
 
1527
select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3;
 
1528
a       (select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1)
 
1529
3       1
 
1530
2       2
 
1531
1       2
 
1532
drop table t1,t2,t3;
 
1533
create table t1 (s1 int);
 
1534
create table t2 (s1 int);
 
1535
insert into t1 values (1);
 
1536
insert into t2 values (1);
 
1537
select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
 
1538
s1
 
1539
1
 
1540
drop table t1,t2;
 
1541
create table t1 (s1 int);
 
1542
create table t2 (s1 int);
 
1543
insert into t1 values (1);
 
1544
insert into t2 values (1);
 
1545
update t1 set  s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
 
1546
ERROR 42S22: Unknown column 'x.s1' in 'field list'
 
1547
DROP TABLE t1, t2;
 
1548
CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
 
1549
s2 CHAR(5) COLLATE latin1_swedish_ci);
 
1550
INSERT INTO t1 VALUES ('z','?');
 
1551
select * from t1 where s1 > (select max(s2) from t1);
 
1552
ERROR HY000: Illegal mix of collations (latin1_german1_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '>'
 
1553
select * from t1 where s1 > any (select max(s2) from t1);
 
1554
ERROR HY000: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin1_german1_ci,IMPLICIT) for operation '<'
 
1555
drop table t1;
 
1556
create table t1(toid int,rd int);
 
1557
create table t2(userid int,pmnew int,pmtotal int);
 
1558
insert into t2 values(1,0,0),(2,0,0);
 
1559
insert into t1 values(1,0),(1,0),(1,0),(1,12),(1,15),(1,123),(1,12312),(1,12312),(1,123),(2,0),(2,0),(2,1),(2,2);
 
1560
select userid,pmtotal,pmnew, (select count(rd) from t1 where toid=t2.userid) calc_total, (select count(rd) from t1 where rd=0 and toid=t2.userid) calc_new from t2 where userid in (select distinct toid from t1);
 
1561
userid  pmtotal pmnew   calc_total      calc_new
 
1562
1       0       0       9       3
 
1563
2       0       0       4       2
 
1564
drop table t1, t2;
 
1565
create table t1 (s1 char(5));
 
1566
select (select 'a','b' from t1 union select 'a','b' from t1) from t1;
 
1567
ERROR 21000: Operand should contain 1 column(s)
 
1568
insert into t1 values ('tttt');
 
1569
select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1);
 
1570
s1
 
1571
tttt
 
1572
explain extended (select * from t1);
 
1573
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1574
1       SIMPLE  t1      system  NULL    NULL    NULL    NULL    1       100.00  
 
1575
Warnings:
 
1576
Note    1003    (select 'tttt' AS `s1` from dual)
 
1577
(select * from t1);
 
1578
s1
 
1579
tttt
 
1580
drop table t1;
 
1581
create table t1 (s1 char(5), index s1(s1));
 
1582
create table t2 (s1 char(5), index s1(s1));
 
1583
insert into t1 values ('a1'),('a2'),('a3');
 
1584
insert into t2 values ('a1'),('a2');
 
1585
select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
 
1586
s1      s1 NOT IN (SELECT s1 FROM t2)
 
1587
a1      0
 
1588
a2      0
 
1589
a3      1
 
1590
select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
 
1591
s1      s1 = ANY (SELECT s1 FROM t2)
 
1592
a1      1
 
1593
a2      1
 
1594
a3      0
 
1595
select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
 
1596
s1      s1 <> ALL (SELECT s1 FROM t2)
 
1597
a1      0
 
1598
a2      0
 
1599
a3      1
 
1600
select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
 
1601
s1      s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')
 
1602
a1      0
 
1603
a2      1
 
1604
a3      1
 
1605
explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
 
1606
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1607
1       PRIMARY t1      index   NULL    s1      6       NULL    3       100.00  Using index
 
1608
2       SUBQUERY        t2      index_subquery  s1      s1      6       func    2       100.00  Using index; Full scan on NULL key
 
1609
Warnings:
 
1610
Note    1003    select `test`.`t1`.`s1` AS `s1`,(not(<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))))) AS `s1 NOT IN (SELECT s1 FROM t2)` from `test`.`t1`
 
1611
explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
 
1612
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1613
1       PRIMARY t1      index   NULL    s1      6       NULL    3       100.00  Using index
 
1614
2       SUBQUERY        t2      index_subquery  s1      s1      6       func    2       100.00  Using index; Full scan on NULL key
 
1615
Warnings:
 
1616
Note    1003    select `test`.`t1`.`s1` AS `s1`,<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))) AS `s1 = ANY (SELECT s1 FROM t2)` from `test`.`t1`
 
1617
explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
 
1618
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1619
1       PRIMARY t1      index   NULL    s1      6       NULL    3       100.00  Using index
 
1620
2       SUBQUERY        t2      index_subquery  s1      s1      6       func    2       100.00  Using index; Full scan on NULL key
 
1621
Warnings:
 
1622
Note    1003    select `test`.`t1`.`s1` AS `s1`,(not(<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))))) AS `s1 <> ALL (SELECT s1 FROM t2)` from `test`.`t1`
 
1623
explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
 
1624
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1625
1       PRIMARY t1      index   NULL    s1      6       NULL    3       100.00  Using index
 
1626
2       SUBQUERY        t2      index_subquery  s1      s1      6       func    2       100.00  Using index; Using where; Full scan on NULL key
 
1627
Warnings:
 
1628
Note    1003    select `test`.`t1`.`s1` AS `s1`,(not(<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL where (`test`.`t2`.`s1` < 'a2') having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))))) AS `s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')` from `test`.`t1`
 
1629
drop table t1,t2;
 
1630
create table t2 (a int, b int not null);
 
1631
create table t3 (a int);
 
1632
insert into t3 values (6),(7),(3);
 
1633
select * from t3 where a >= all (select b from t2);
 
1634
a
 
1635
6
 
1636
7
 
1637
3
 
1638
explain extended select * from t3 where a >= all (select b from t2);
 
1639
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1640
1       PRIMARY t3      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
1641
2       SUBQUERY        t2      system  NULL    NULL    NULL    NULL    0       0.00    const row not found
 
1642
Warnings:
 
1643
Note    1003    select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,((select max(NULL) from `test`.`t2`) > <cache>(`test`.`t3`.`a`))))
 
1644
select * from t3 where a >= some (select b from t2);
 
1645
a
 
1646
explain extended select * from t3 where a >= some (select b from t2);
 
1647
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1648
1       PRIMARY t3      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
1649
2       SUBQUERY        t2      system  NULL    NULL    NULL    NULL    0       0.00    const row not found
 
1650
Warnings:
 
1651
Note    1003    select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(NULL) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`))))
 
1652
select * from t3 where a >= all (select b from t2 group by 1);
 
1653
a
 
1654
6
 
1655
7
 
1656
3
 
1657
explain extended select * from t3 where a >= all (select b from t2 group by 1);
 
1658
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1659
1       PRIMARY t3      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
1660
2       SUBQUERY        t2      system  NULL    NULL    NULL    NULL    0       0.00    const row not found
 
1661
Warnings:
 
1662
Note    1003    select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,((select max(NULL) from `test`.`t2`) > <cache>(`test`.`t3`.`a`))))
 
1663
select * from t3 where a >= some (select b from t2 group by 1);
 
1664
a
 
1665
explain extended select * from t3 where a >= some (select b from t2 group by 1);
 
1666
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1667
1       PRIMARY t3      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
1668
2       SUBQUERY        t2      system  NULL    NULL    NULL    NULL    0       0.00    const row not found
 
1669
Warnings:
 
1670
Note    1003    select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(NULL) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`))))
 
1671
select * from t3 where NULL >= any (select b from t2);
 
1672
a
 
1673
explain extended select * from t3 where NULL >= any (select b from t2);
 
1674
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1675
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE
 
1676
2       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    no matching row in const table
 
1677
Warnings:
 
1678
Note    1003    select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
 
1679
select * from t3 where NULL >= any (select b from t2 group by 1);
 
1680
a
 
1681
explain extended select * from t3 where NULL >= any (select b from t2 group by 1);
 
1682
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1683
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE
 
1684
2       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    no matching row in const table
 
1685
Warnings:
 
1686
Note    1003    select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
 
1687
select * from t3 where NULL >= some (select b from t2);
 
1688
a
 
1689
explain extended select * from t3 where NULL >= some (select b from t2);
 
1690
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1691
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE
 
1692
2       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    no matching row in const table
 
1693
Warnings:
 
1694
Note    1003    select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
 
1695
select * from t3 where NULL >= some (select b from t2 group by 1);
 
1696
a
 
1697
explain extended select * from t3 where NULL >= some (select b from t2 group by 1);
 
1698
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1699
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE
 
1700
2       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    no matching row in const table
 
1701
Warnings:
 
1702
Note    1003    select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
 
1703
insert into t2 values (2,2), (2,1), (3,3), (3,1);
 
1704
select * from t3 where a > all (select max(b) from t2 group by a);
 
1705
a
 
1706
6
 
1707
7
 
1708
explain extended select * from t3 where a > all (select max(b) from t2 group by a);
 
1709
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1710
1       PRIMARY t3      ALL     NULL    NULL    NULL    NULL    3       100.00  Using where
 
1711
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    4       100.00  Using temporary
 
1712
Warnings:
 
1713
Note    1003    select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,(<max>(select max(`test`.`t2`.`b`) from `test`.`t2` group by `test`.`t2`.`a`) >= <cache>(`test`.`t3`.`a`))))
 
1714
drop table t2, t3;
 
1715
CREATE TABLE `t1` ( `id` mediumint(9) NOT NULL auto_increment, `taskid` bigint(20) NOT NULL default '0', `dbid` int(11) NOT NULL default '0', `create_date` datetime NOT NULL default '0000-00-00 00:00:00', `last_update` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY  (`id`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=3 ;
 
1716
INSERT INTO `t1` (`id`, `taskid`, `dbid`, `create_date`,`last_update`) VALUES (1, 1, 15, '2003-09-29 10:31:36', '2003-09-29 10:31:36'), (2, 1, 21, now(), now());
 
1717
CREATE TABLE `t2` (`db_id` int(11) NOT NULL auto_increment,`name` varchar(200) NOT NULL default '',`primary_uid` smallint(6) NOT NULL default '0',`secondary_uid` smallint(6) NOT NULL default '0',PRIMARY KEY  (`db_id`),UNIQUE KEY `name_2` (`name`),FULLTEXT KEY `name` (`name`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2147483647;
 
1718
INSERT INTO `t2` (`db_id`, `name`, `primary_uid`, `secondary_uid`) VALUES (18, 'Not Set 1', 0, 0),(19, 'Valid', 1, 2),(20, 'Valid 2', 1, 2),(21, 'Should Not Return', 1, 2),(26, 'Not Set 2', 0, 0),(-1, 'ALL DB\'S', 0, 0);
 
1719
CREATE TABLE `t3` (`taskgenid` mediumint(9) NOT NULL auto_increment,`dbid` int(11) NOT NULL default '0',`taskid` int(11) NOT NULL default '0',`mon` tinyint(4) NOT NULL default '1',`tues` tinyint(4) NOT NULL default '1',`wed` tinyint(4) NOT NULL default '1',`thur` tinyint(4) NOT NULL default '1',`fri` tinyint(4) NOT NULL default '1',`sat` tinyint(4) NOT NULL default '0',`sun` tinyint(4) NOT NULL default '0',`how_often` smallint(6) NOT NULL default '1',`userid` smallint(6) NOT NULL default '0',`active` tinyint(4) NOT NULL default '1',PRIMARY KEY  (`taskgenid`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2 ;
 
1720
INSERT INTO `t3` (`taskgenid`, `dbid`, `taskid`, `mon`, `tues`,`wed`, `thur`, `fri`, `sat`, `sun`, `how_often`, `userid`, `active`) VALUES (1,-1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1);
 
1721
CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
 
1722
INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
 
1723
select  dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') from t3 a, t2 b, t4  WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
 
1724
dbid    name    (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')
 
1725
-1      Valid   1
 
1726
-1      Valid 2 1
 
1727
-1      Should Not Return       0
 
1728
SELECT dbid, name FROM t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND ((date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')) AND t4.task_id = taskid;
 
1729
dbid    name
 
1730
-1      Valid
 
1731
-1      Valid 2
 
1732
drop table t1,t2,t3,t4;
 
1733
CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
 
1734
INSERT INTO t1 VALUES (1),(5);
 
1735
CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
 
1736
INSERT INTO t2 VALUES (2),(6);
 
1737
select * from t1 where (1,2,6) in (select * from t2);
 
1738
ERROR 21000: Operand should contain 3 column(s)
 
1739
DROP TABLE t1,t2;
 
1740
create table t1 (s1 int);
 
1741
insert into t1 values (1);
 
1742
insert into t1 values (2);
 
1743
set sort_buffer_size = (select s1 from t1);
 
1744
ERROR 21000: Subquery returns more than 1 row
 
1745
do (select * from t1);
 
1746
Warnings:
 
1747
Error   1242    Subquery returns more than 1 row
 
1748
drop table t1;
 
1749
create table t1 (s1 char);
 
1750
insert into t1 values ('e');
 
1751
select * from t1 where 'f' > any (select s1 from t1);
 
1752
s1
 
1753
e
 
1754
select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
 
1755
s1
 
1756
e
 
1757
explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
 
1758
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1759
1       PRIMARY t1      system  NULL    NULL    NULL    NULL    1       100.00  
 
1760
2       SUBQUERY        t1      system  NULL    NULL    NULL    NULL    1       100.00  
 
1761
3       UNION   t1      system  NULL    NULL    NULL    NULL    1       100.00  
 
1762
NULL    UNION RESULT    <union2,3>      ALL     NULL    NULL    NULL    NULL    NULL    NULL    
 
1763
Warnings:
 
1764
Note    1003    select 'e' AS `s1` from dual where 1
 
1765
drop table t1;
 
1766
CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
 
1767
INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874');
 
1768
CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1;
 
1769
INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');
 
1770
select c.number as phone,(select p.code from t2 p where c.number like concat(p.code, '%') order by length(p.code) desc limit 1) as code from t1 c;
 
1771
phone   code
 
1772
69294728265     6
 
1773
18621828126     1862
 
1774
89356874041     NULL
 
1775
95895001874     NULL
 
1776
drop table t1, t2;
 
1777
create table t1 (s1 int);
 
1778
create table t2 (s1 int);
 
1779
select * from t1 where (select count(*) from t2 where t1.s2) = 1;
 
1780
ERROR 42S22: Unknown column 't1.s2' in 'where clause'
 
1781
select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
 
1782
ERROR 42S22: Unknown column 't1.s2' in 'group statement'
 
1783
select count(*) from t2 group by t1.s2;
 
1784
ERROR 42S22: Unknown column 't1.s2' in 'group statement'
 
1785
drop table t1, t2;
 
1786
CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB));
 
1787
CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA));
 
1788
INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365');
 
1789
INSERT INTO t2 VALUES (100, 200, 'C');
 
1790
SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1);
 
1791
COLC
 
1792
DROP TABLE t1, t2;
 
1793
CREATE TABLE t1 (a int(1));
 
1794
INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);
 
1795
SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;
 
1796
(SELECT a)
 
1797
1
 
1798
2
 
1799
3
 
1800
4
 
1801
5
 
1802
DROP TABLE t1;
 
1803
create table t1 (a int, b decimal(13, 3));
 
1804
insert into t1 values (1, 0.123);
 
1805
select a, (select max(b) from t1) into outfile "../../tmp/subselect.out.file.1" from t1;
 
1806
delete from t1;
 
1807
load data infile "../../tmp/subselect.out.file.1" into table t1;
 
1808
select * from t1;
 
1809
a       b
 
1810
1       0.123
 
1811
drop table t1;
 
1812
CREATE TABLE `t1` (
 
1813
`id` int(11) NOT NULL auto_increment,
 
1814
`id_cns` tinyint(3) unsigned NOT NULL default '0',
 
1815
`tipo` enum('','UNO','DUE') NOT NULL default '',
 
1816
`anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000',
 
1817
`particolare` mediumint(8) unsigned NOT NULL default '0',
 
1818
`generale` mediumint(8) unsigned NOT NULL default '0',
 
1819
`bis` tinyint(3) unsigned NOT NULL default '0',
 
1820
PRIMARY KEY  (`id`),
 
1821
UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
 
1822
UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
 
1823
);
 
1824
INSERT INTO `t1` VALUES (1,16,'UNO',1987,2048,9681,0),(2,50,'UNO',1987,1536,13987,0),(3,16,'UNO',1987,2432,14594,0),(4,16,'UNO',1987,1792,13422,0),(5,16,'UNO',1987,1025,10240,0),(6,16,'UNO',1987,1026,7089,0);
 
1825
CREATE TABLE `t2` (
 
1826
`id` tinyint(3) unsigned NOT NULL auto_increment,
 
1827
`max_anno_dep` smallint(6) unsigned NOT NULL default '0',
 
1828
PRIMARY KEY  (`id`)
 
1829
);
 
1830
INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
 
1831
SELECT cns.id, cns.max_anno_dep, cns.max_anno_dep = (SELECT s.anno_dep FROM t1 AS s WHERE s.id_cns = cns.id ORDER BY s.anno_dep DESC LIMIT 1) AS PIPPO FROM t2 AS cns;
 
1832
id      max_anno_dep    PIPPO
 
1833
16      1987    1
 
1834
50      1990    0
 
1835
51      1990    NULL
 
1836
DROP TABLE t1, t2;
 
1837
create table t1 (a int);
 
1838
insert into t1 values (1), (2), (3);
 
1839
SET SQL_SELECT_LIMIT=1;
 
1840
select sum(a) from (select * from t1) as a;
 
1841
sum(a)
 
1842
6
 
1843
select 2 in (select * from t1);
 
1844
2 in (select * from t1)
 
1845
1
 
1846
SET SQL_SELECT_LIMIT=default;
 
1847
drop table t1;
 
1848
CREATE TABLE t1 (a int, b int, INDEX (a));
 
1849
INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);
 
1850
SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b;
 
1851
a       b
 
1852
1       1
 
1853
1       2
 
1854
1       3
 
1855
DROP TABLE t1;
 
1856
create table t1(val varchar(10));
 
1857
insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');
 
1858
select count(*) from t1 as w1 where w1.val in (select w2.val from t1 as w2 where w2.val like 'm%') and w1.val in (select w3.val from t1 as w3 where w3.val like 'e%');
 
1859
count(*)
 
1860
0
 
1861
drop table t1;
 
1862
create table t1 (id int not null, text varchar(20) not null default '', primary key (id));
 
1863
insert into t1 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text11'), (12, 'text12');
 
1864
select * from t1 where id not in (select id from t1 where id < 8);
 
1865
id      text
 
1866
8       text8
 
1867
9       text9
 
1868
10      text10
 
1869
11      text11
 
1870
12      text12
 
1871
select * from t1 as tt where not exists (select id from t1 where id < 8 and (id = tt.id or id is null) having id is not null);
 
1872
id      text
 
1873
8       text8
 
1874
9       text9
 
1875
10      text10
 
1876
11      text11
 
1877
12      text12
 
1878
explain extended select * from t1 where id not in (select id from t1 where id < 8);
 
1879
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1880
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    12      100.00  Using where
 
1881
2       DEPENDENT SUBQUERY      t1      unique_subquery PRIMARY PRIMARY 4       func    1       100.00  Using index; Using where
 
1882
Warnings:
 
1883
Note    1003    select `test`.`t1`.`id` AS `id`,`test`.`t1`.`text` AS `text` from `test`.`t1` where (not(<expr_cache><`test`.`t1`.`id`>(<in_optimizer>(`test`.`t1`.`id`,<exists>(<primary_index_lookup>(<cache>(`test`.`t1`.`id`) in t1 on PRIMARY where ((`test`.`t1`.`id` < 8) and (<cache>(`test`.`t1`.`id`) = `test`.`t1`.`id`))))))))
 
1884
explain extended select * from t1 as tt where not exists (select id from t1 where id < 8 and (id = tt.id or id is null) having id is not null);
 
1885
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1886
1       PRIMARY tt      ALL     NULL    NULL    NULL    NULL    12      100.00  Using where
 
1887
2       DEPENDENT SUBQUERY      t1      eq_ref  PRIMARY PRIMARY 4       test.tt.id      1       100.00  Using where; Using index
 
1888
Warnings:
 
1889
Note    1276    Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1
 
1890
Note    1003    select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<expr_cache><`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null)))))
 
1891
insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
 
1892
create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
 
1893
insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10');
 
1894
select * from t1 a left join t2 b on (a.id=b.id or b.id is null) join t1 c on (if(isnull(b.id), 1000, b.id)=c.id);
 
1895
id      text    id      text    id      text
 
1896
1       text1   1       text1   1       text1
 
1897
2       text2   2       text2   2       text2
 
1898
3       text3   3       text3   3       text3
 
1899
4       text4   4       text4   4       text4
 
1900
5       text5   5       text5   5       text5
 
1901
6       text6   6       text6   6       text6
 
1902
7       text7   7       text7   7       text7
 
1903
8       text8   8       text8   8       text8
 
1904
9       text9   9       text9   9       text9
 
1905
10      text10  10      text10  10      text10
 
1906
11      text11  11      text1   11      text11
 
1907
12      text12  12      text2   12      text12
 
1908
1000    text1000        NULL    NULL    1000    text1000
 
1909
1001    text1001        NULL    NULL    1000    text1000
 
1910
explain extended select * from t1 a left join t2 b on (a.id=b.id or b.id is null) join t1 c on (if(isnull(b.id), 1000, b.id)=c.id);
 
1911
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
1912
1       SIMPLE  a       ALL     NULL    NULL    NULL    NULL    14      100.00  
 
1913
1       SIMPLE  b       eq_ref  PRIMARY PRIMARY 4       test.a.id       2       100.00  
 
1914
1       SIMPLE  c       eq_ref  PRIMARY PRIMARY 4       func    1       100.00  Using index condition
 
1915
Warnings:
 
1916
Note    1003    select `test`.`a`.`id` AS `id`,`test`.`a`.`text` AS `text`,`test`.`b`.`id` AS `id`,`test`.`b`.`text` AS `text`,`test`.`c`.`id` AS `id`,`test`.`c`.`text` AS `text` from `test`.`t1` `a` left join `test`.`t2` `b` on(((`test`.`b`.`id` = `test`.`a`.`id`) or isnull(`test`.`b`.`id`))) join `test`.`t1` `c` where (if(isnull(`test`.`b`.`id`),1000,`test`.`b`.`id`) = `test`.`c`.`id`)
 
1917
drop table t1,t2;
 
1918
create table t1 (a int);
 
1919
insert into t1 values (1);
 
1920
explain select benchmark(1000, (select a from t1 where a=sha(rand())));
 
1921
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
1922
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
1923
2       UNCACHEABLE SUBQUERY    t1      system  NULL    NULL    NULL    NULL    1       
 
1924
drop table t1;
 
1925
create table t1(id int);
 
1926
create table t2(id int);
 
1927
create table t3(flag int);
 
1928
select (select * from t3 where id not null) from t1, t2;
 
1929
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'null) from t1, t2' at line 1
 
1930
drop table t1,t2,t3;
 
1931
CREATE TABLE t1 (id INT);
 
1932
CREATE TABLE t2 (id INT);
 
1933
INSERT INTO t1 VALUES (1), (2);
 
1934
INSERT INTO t2 VALUES (1);
 
1935
SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
 
1936
id      c
 
1937
1       1
 
1938
2       0
 
1939
SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
 
1940
id      c
 
1941
1       1
 
1942
2       0
 
1943
SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY t1.id;
 
1944
id      c
 
1945
1       1
 
1946
2       0
 
1947
SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY id;
 
1948
id      c
 
1949
1       1
 
1950
2       0
 
1951
DROP TABLE t1,t2;
 
1952
CREATE TABLE t1 ( a int, b int );
 
1953
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
 
1954
SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
 
1955
a
 
1956
3
 
1957
SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
 
1958
a
 
1959
1
 
1960
SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
 
1961
a
 
1962
2
 
1963
SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
 
1964
a
 
1965
2
 
1966
3
 
1967
SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
 
1968
a
 
1969
1
 
1970
2
 
1971
SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
 
1972
a
 
1973
1
 
1974
3
 
1975
SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
 
1976
a
 
1977
3
 
1978
SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
 
1979
a
 
1980
1
 
1981
SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
 
1982
a
 
1983
2
 
1984
SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
 
1985
a
 
1986
2
 
1987
3
 
1988
SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
 
1989
a
 
1990
1
 
1991
2
 
1992
SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
 
1993
a
 
1994
1
 
1995
3
 
1996
ALTER TABLE t1 ADD INDEX (a);
 
1997
SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
 
1998
a
 
1999
3
 
2000
SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
 
2001
a
 
2002
1
 
2003
SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
 
2004
a
 
2005
2
 
2006
SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
 
2007
a
 
2008
2
 
2009
3
 
2010
SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
 
2011
a
 
2012
1
 
2013
2
 
2014
SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
 
2015
a
 
2016
1
 
2017
3
 
2018
SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
 
2019
a
 
2020
3
 
2021
SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
 
2022
a
 
2023
1
 
2024
SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
 
2025
a
 
2026
2
 
2027
SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
 
2028
a
 
2029
2
 
2030
3
 
2031
SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
 
2032
a
 
2033
1
 
2034
2
 
2035
SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
 
2036
a
 
2037
1
 
2038
3
 
2039
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2);
 
2040
a
 
2041
3
 
2042
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2);
 
2043
a
 
2044
1
 
2045
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2);
 
2046
a
 
2047
2
 
2048
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2);
 
2049
a
 
2050
2
 
2051
3
 
2052
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2);
 
2053
a
 
2054
1
 
2055
2
 
2056
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2);
 
2057
a
 
2058
1
 
2059
3
 
2060
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2);
 
2061
a
 
2062
3
 
2063
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2);
 
2064
a
 
2065
1
 
2066
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2);
 
2067
a
 
2068
2
 
2069
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2);
 
2070
a
 
2071
2
 
2072
3
 
2073
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2);
 
2074
a
 
2075
1
 
2076
2
 
2077
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2);
 
2078
a
 
2079
1
 
2080
3
 
2081
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
 
2082
a
 
2083
3
 
2084
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
 
2085
a
 
2086
1
 
2087
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
 
2088
a
 
2089
2
 
2090
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
 
2091
a
 
2092
2
 
2093
3
 
2094
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
 
2095
a
 
2096
1
 
2097
2
 
2098
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
 
2099
a
 
2100
1
 
2101
3
 
2102
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
 
2103
a
 
2104
3
 
2105
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
 
2106
a
 
2107
1
 
2108
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
 
2109
a
 
2110
2
 
2111
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
 
2112
a
 
2113
2
 
2114
3
 
2115
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
 
2116
a
 
2117
1
 
2118
2
 
2119
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
 
2120
a
 
2121
1
 
2122
3
 
2123
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
 
2124
a
 
2125
3
 
2126
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
 
2127
a
 
2128
1
 
2129
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
 
2130
a
 
2131
2
 
2132
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
 
2133
a
 
2134
2
 
2135
3
 
2136
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
 
2137
a
 
2138
1
 
2139
2
 
2140
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
 
2141
a
 
2142
1
 
2143
3
 
2144
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
 
2145
a
 
2146
3
 
2147
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
 
2148
a
 
2149
1
 
2150
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
 
2151
a
 
2152
2
 
2153
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
 
2154
a
 
2155
2
 
2156
3
 
2157
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
 
2158
a
 
2159
1
 
2160
2
 
2161
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
 
2162
a
 
2163
1
 
2164
3
 
2165
SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2);
 
2166
ERROR 21000: Operand should contain 1 column(s)
 
2167
SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2);
 
2168
ERROR 21000: Operand should contain 1 column(s)
 
2169
SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2);
 
2170
ERROR 21000: Operand should contain 1 column(s)
 
2171
SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2);
 
2172
ERROR 21000: Operand should contain 1 column(s)
 
2173
SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2);
 
2174
ERROR 21000: Operand should contain 1 column(s)
 
2175
SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2);
 
2176
ERROR 21000: Operand should contain 1 column(s)
 
2177
SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2);
 
2178
ERROR 21000: Operand should contain 1 column(s)
 
2179
SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2);
 
2180
ERROR 21000: Operand should contain 1 column(s)
 
2181
SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2);
 
2182
ERROR 21000: Operand should contain 2 column(s)
 
2183
SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2);
 
2184
ERROR 21000: Operand should contain 1 column(s)
 
2185
SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2);
 
2186
a
 
2187
SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
 
2188
ERROR 21000: Operand should contain 2 column(s)
 
2189
SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
 
2190
ERROR 21000: Operand should contain 1 column(s)
 
2191
SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
 
2192
a
 
2193
1
 
2194
2
 
2195
3
 
2196
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
 
2197
a
 
2198
2
 
2199
SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2);
 
2200
a
 
2201
1
 
2202
3
 
2203
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
 
2204
a
 
2205
2
 
2206
SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2);
 
2207
a
 
2208
1
 
2209
3
 
2210
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
 
2211
a
 
2212
2
 
2213
SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
 
2214
a
 
2215
1
 
2216
3
 
2217
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
 
2218
a
 
2219
2
 
2220
SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
 
2221
a
 
2222
1
 
2223
3
 
2224
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a);
 
2225
a
 
2226
3
 
2227
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a);
 
2228
a
 
2229
1
 
2230
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a);
 
2231
a
 
2232
2
 
2233
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
 
2234
a
 
2235
2
 
2236
3
 
2237
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
 
2238
a
 
2239
1
 
2240
2
 
2241
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a);
 
2242
a
 
2243
1
 
2244
3
 
2245
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a);
 
2246
a
 
2247
3
 
2248
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a);
 
2249
a
 
2250
1
 
2251
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a);
 
2252
a
 
2253
2
 
2254
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
 
2255
a
 
2256
2
 
2257
3
 
2258
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
 
2259
a
 
2260
1
 
2261
2
 
2262
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a);
 
2263
a
 
2264
1
 
2265
3
 
2266
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2);
 
2267
a
 
2268
3
 
2269
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2);
 
2270
a
 
2271
1
 
2272
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2);
 
2273
a
 
2274
2
 
2275
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2);
 
2276
a
 
2277
2
 
2278
3
 
2279
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2);
 
2280
a
 
2281
1
 
2282
2
 
2283
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2);
 
2284
a
 
2285
1
 
2286
3
 
2287
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2);
 
2288
a
 
2289
3
 
2290
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2);
 
2291
a
 
2292
1
 
2293
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2);
 
2294
a
 
2295
2
 
2296
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2);
 
2297
a
 
2298
2
 
2299
3
 
2300
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2);
 
2301
a
 
2302
1
 
2303
2
 
2304
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2);
 
2305
a
 
2306
1
 
2307
3
 
2308
SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a;
 
2309
concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-')
 
2310
0-
 
2311
0-
 
2312
1-
 
2313
SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a;
 
2314
concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-')
 
2315
1-
 
2316
0-
 
2317
0-
 
2318
SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a;
 
2319
concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-')
 
2320
0-
 
2321
1-
 
2322
0-
 
2323
DROP TABLE t1;
 
2324
CREATE TABLE t1 ( a double, b double );
 
2325
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
 
2326
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0);
 
2327
a
 
2328
3
 
2329
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0);
 
2330
a
 
2331
1
 
2332
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0);
 
2333
a
 
2334
2
 
2335
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0);
 
2336
a
 
2337
2
 
2338
3
 
2339
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0);
 
2340
a
 
2341
1
 
2342
2
 
2343
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0);
 
2344
a
 
2345
1
 
2346
3
 
2347
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0);
 
2348
a
 
2349
3
 
2350
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0);
 
2351
a
 
2352
1
 
2353
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0);
 
2354
a
 
2355
2
 
2356
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0);
 
2357
a
 
2358
2
 
2359
3
 
2360
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0);
 
2361
a
 
2362
1
 
2363
2
 
2364
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0);
 
2365
a
 
2366
1
 
2367
3
 
2368
DROP TABLE t1;
 
2369
CREATE TABLE t1 ( a char(1), b char(1));
 
2370
INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3');
 
2371
SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2');
 
2372
a
 
2373
3
 
2374
SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2');
 
2375
a
 
2376
1
 
2377
SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2');
 
2378
a
 
2379
2
 
2380
SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2');
 
2381
a
 
2382
2
 
2383
3
 
2384
SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2');
 
2385
a
 
2386
1
 
2387
2
 
2388
SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2');
 
2389
a
 
2390
1
 
2391
3
 
2392
SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2');
 
2393
a
 
2394
3
 
2395
SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2');
 
2396
a
 
2397
1
 
2398
SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2');
 
2399
a
 
2400
2
 
2401
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2');
 
2402
a
 
2403
2
 
2404
3
 
2405
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2');
 
2406
a
 
2407
1
 
2408
2
 
2409
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2');
 
2410
a
 
2411
1
 
2412
3
 
2413
DROP TABLE t1;
 
2414
create table t1 (a int, b int);
 
2415
insert into t1 values (1,2),(3,4);
 
2416
select * from t1 up where exists (select * from t1 where t1.a=up.a);
 
2417
a       b
 
2418
1       2
 
2419
3       4
 
2420
explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a);
 
2421
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
2422
1       PRIMARY up      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where
 
2423
2       DEPENDENT SUBQUERY      t1      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where
 
2424
Warnings:
 
2425
Note    1276    Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1
 
2426
Note    1003    select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <expr_cache><`test`.`up`.`a`>(exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`)))
 
2427
drop table t1;
 
2428
CREATE TABLE t1 (t1_a int);
 
2429
INSERT INTO t1 VALUES (1);
 
2430
CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b));
 
2431
INSERT INTO t2 VALUES (1, 1), (1, 2);
 
2432
SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1
 
2433
HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a);
 
2434
t1_a    t2_a    t2_b
 
2435
1       1       2
 
2436
DROP TABLE t1, t2;
 
2437
CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL);
 
2438
INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL);
 
2439
CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL);
 
2440
INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix');
 
2441
SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id;
 
2442
id      name    id      pet
 
2443
1       Tim     1       Fido
 
2444
2       Rebecca 2       Spot
 
2445
3       NULL    3       Felix
 
2446
drop table t1,t2;
 
2447
CREATE TABLE t1 ( a int, b int );
 
2448
CREATE TABLE t2 ( c int, d int );
 
2449
INSERT INTO t1 VALUES (1,2), (2,3), (3,4);
 
2450
SELECT a AS abc, b FROM t1 outr WHERE b =
 
2451
(SELECT MIN(b) FROM t1 WHERE a=outr.a);
 
2452
abc     b
 
2453
1       2
 
2454
2       3
 
2455
3       4
 
2456
INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b =
 
2457
(SELECT MIN(b) FROM t1 WHERE a=outr.a);
 
2458
select * from t2;
 
2459
c       d
 
2460
1       2
 
2461
2       3
 
2462
3       4
 
2463
CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b =
 
2464
(SELECT MIN(b) FROM t1 WHERE a=outr.a);
 
2465
select * from t3;
 
2466
abc     b
 
2467
1       2
 
2468
2       3
 
2469
3       4
 
2470
prepare stmt1 from "INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
 
2471
execute stmt1;
 
2472
deallocate prepare stmt1;
 
2473
select * from t2;
 
2474
c       d
 
2475
1       2
 
2476
2       3
 
2477
3       4
 
2478
1       2
 
2479
2       3
 
2480
3       4
 
2481
drop table t3;
 
2482
prepare stmt1 from "CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
 
2483
execute stmt1;
 
2484
select * from t3;
 
2485
abc     b
 
2486
1       2
 
2487
2       3
 
2488
3       4
 
2489
deallocate prepare stmt1;
 
2490
DROP TABLE t1, t2, t3;
 
2491
CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
2492
insert into t1 values (1);
 
2493
CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
2494
insert into t2 values (1,2);
 
2495
select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1);
 
2496
a       C
 
2497
1       1
 
2498
drop table t1,t2;
 
2499
create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b));
 
2500
insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan');
 
2501
create table t2 (a int);
 
2502
insert into t2 values (1),(3),(2),(7);
 
2503
select a,b from t1 where match(b) against ('Ball') > 0;
 
2504
a       b
 
2505
1       ball
 
2506
2       ball games
 
2507
select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0);
 
2508
a
 
2509
1
 
2510
2
 
2511
drop table t1,t2;
 
2512
CREATE TABLE t1(`IZAVORGANG_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin,`KUERZEL` VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_bin,`IZAANALYSEART_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin,`IZAPMKZ_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin);
 
2513
CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel);
 
2514
INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001');
 
2515
INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001');
 
2516
INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001');
 
2517
INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001');
 
2518
SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001');
 
2519
IZAVORGANG_ID
 
2520
D0000000001
 
2521
drop table t1;
 
2522
CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY  (`aid`,`bid`));
 
2523
CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY  (`aid`,`bid`));
 
2524
insert into t1 values (1,1),(1,2),(2,1),(2,2);
 
2525
insert into t2 values (1,2),(2,2);
 
2526
select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
 
2527
aid     bid
 
2528
1       1
 
2529
2       1
 
2530
alter table t2 drop primary key;
 
2531
alter table t2 add key KEY1 (aid, bid);
 
2532
select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
 
2533
aid     bid
 
2534
1       1
 
2535
2       1
 
2536
alter table t2 drop key KEY1;
 
2537
alter table t2 add primary key (bid, aid);
 
2538
select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
 
2539
aid     bid
 
2540
1       1
 
2541
2       1
 
2542
drop table t1,t2;
 
2543
CREATE TABLE t1 (howmanyvalues bigint, avalue int);
 
2544
INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4);
 
2545
SELECT howmanyvalues, count(*) from t1 group by howmanyvalues;
 
2546
howmanyvalues   count(*)
 
2547
1       1
 
2548
2       2
 
2549
3       3
 
2550
4       4
 
2551
SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
 
2552
howmanyvalues   mycount
 
2553
1       1
 
2554
2       2
 
2555
3       3
 
2556
4       4
 
2557
CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues);
 
2558
SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues;
 
2559
howmanyvalues   mycount
 
2560
1       1
 
2561
2       2
 
2562
3       3
 
2563
4       4
 
2564
SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
 
2565
howmanyvalues   mycount
 
2566
1       1
 
2567
2       2
 
2568
3       3
 
2569
4       4
 
2570
SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues;
 
2571
howmanyvalues   mycount
 
2572
1       1
 
2573
2       1
 
2574
3       1
 
2575
4       1
 
2576
drop table t1;
 
2577
create table t1 (x int);
 
2578
select  (select b.x from t1 as b where b.x=a.x) from t1 as a where a.x=2 group by a.x;
 
2579
(select b.x from t1 as b where b.x=a.x)
 
2580
drop table t1;
 
2581
CREATE TABLE `t1` ( `master` int(10) unsigned NOT NULL default '0', `map` smallint(6) unsigned NOT NULL default '0', `slave` int(10) unsigned NOT NULL default '0', `access` int(10) unsigned NOT NULL default '0', UNIQUE KEY `access_u` (`master`,`map`,`slave`));
 
2582
INSERT INTO `t1` VALUES (1,0,0,700),(1,1,1,400),(1,5,5,400),(1,12,12,400),(1,12,32,400),(4,12,32,400);
 
2583
CREATE TABLE `t2` ( `id` int(10) unsigned NOT NULL default '0', `pid` int(10) unsigned NOT NULL default '0', `map` smallint(6) unsigned NOT NULL default '0', `level` tinyint(4) unsigned NOT NULL default '0', `title` varchar(255) default NULL, PRIMARY KEY  (`id`,`pid`,`map`), KEY `level` (`level`), KEY `id` (`id`,`map`)) ;
 
2584
INSERT INTO `t2` VALUES (6,5,12,7,'a'),(12,0,0,7,'a'),(12,1,0,7,'a'),(12,5,5,7,'a'),(12,5,12,7,'a');
 
2585
SELECT b.sc FROM (SELECT (SELECT a.access FROM t1 a WHERE a.map = op.map AND a.slave = op.pid AND a.master = 1) ac FROM t2 op WHERE op.id = 12 AND op.map = 0) b;
 
2586
ERROR 42S22: Unknown column 'b.sc' in 'field list'
 
2587
SELECT b.ac FROM (SELECT (SELECT a.access FROM t1 a WHERE a.map = op.map AND a.slave = op.pid AND a.master = 1) ac FROM t2 op WHERE op.id = 12 AND op.map = 0) b;
 
2588
ac
 
2589
700
 
2590
NULL
 
2591
drop tables t1,t2;
 
2592
create table t1 (a int not null, b int not null, c int, primary key (a,b));
 
2593
insert into t1 values (1,1,1), (2,2,2), (3,3,3);
 
2594
set @b:= 0;
 
2595
explain select sum(a) from t1 where b > @b;
 
2596
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
2597
1       SIMPLE  t1      index   NULL    PRIMARY 8       NULL    3       Using where; Using index
 
2598
set @a:= (select sum(a) from t1 where b > @b);
 
2599
explain select a from t1 where c=2;
 
2600
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
2601
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       Using where
 
2602
do @a:= (select sum(a) from t1 where b > @b);
 
2603
explain select a from t1 where c=2;
 
2604
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
2605
1       SIMPLE  t1      ALL     NULL    NULL    NULL    NULL    3       Using where
 
2606
drop table t1;
 
2607
set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ;
 
2608
create table t1 (a int, b int);
 
2609
create table t2 (a int, b int);
 
2610
insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5);
 
2611
insert into t2 values (1,3),(2,1);
 
2612
select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b;
 
2613
a       b       (select max(b) from t2 where t1.b=t2.a)
 
2614
1       1       3
 
2615
1       2       1
 
2616
1       3       NULL
 
2617
2       4       NULL
 
2618
2       5       NULL
 
2619
drop table t1, t2;
 
2620
create table t1 (id int);
 
2621
create table t2 (id int, body text, fulltext (body));
 
2622
insert into t1 values(1),(2),(3);
 
2623
insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test');
 
2624
select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode));
 
2625
count(distinct id)
 
2626
1
 
2627
drop table t2,t1;
 
2628
create table t1 (s1 int,s2 int);
 
2629
insert into t1 values (20,15);
 
2630
select * from t1 where  (('a',null) <=> (select 'a',s2 from t1 where s1 = 0));
 
2631
s1      s2
 
2632
drop table t1;
 
2633
create table t1 (s1 int);
 
2634
insert into t1 values (1),(null);
 
2635
select * from t1 where s1 < all (select s1 from t1);
 
2636
s1
 
2637
select s1, s1 < all (select s1 from t1) from t1;
 
2638
s1      s1 < all (select s1 from t1)
 
2639
1       0
 
2640
NULL    NULL
 
2641
drop table t1;
 
2642
CREATE TABLE t1 (
 
2643
Code char(3) NOT NULL default '',
 
2644
Name char(52) NOT NULL default '',
 
2645
Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
 
2646
Region char(26) NOT NULL default '',
 
2647
SurfaceArea float(10,2) NOT NULL default '0.00',
 
2648
IndepYear smallint(6) default NULL,
 
2649
Population int(11) NOT NULL default '0',
 
2650
LifeExpectancy float(3,1) default NULL,
 
2651
GNP float(10,2) default NULL,
 
2652
GNPOld float(10,2) default NULL,
 
2653
LocalName char(45) NOT NULL default '',
 
2654
GovernmentForm char(45) NOT NULL default '',
 
2655
HeadOfState char(60) default NULL,
 
2656
Capital int(11) default NULL,
 
2657
Code2 char(2) NOT NULL default ''
 
2658
) ENGINE=MyISAM;
 
2659
INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX');
 
2660
INSERT INTO t1 VALUES ('ASM','American Samoa','Oceania','Polynesia',199.00,0,68000,75.1,334.00,NULL,'Amerika Samoa','US Territory','George W. Bush',54,'AS');
 
2661
INSERT INTO t1 VALUES ('ATF','French Southern territories','Antarctica','Antarctica',7780.00,0,0,NULL,0.00,NULL,'Terres australes françaises','Nonmetropolitan Territory of France','Jacques Chirac',NULL,'TF');
 
2662
INSERT INTO t1 VALUES ('UMI','United States Minor Outlying Islands','Oceania','Micronesia/Caribbean',16.00,0,0,NULL,0.00,NULL,'United States Minor Outlying Islands','Dependent Territory of the US','George W. Bush',NULL,'UM');
 
2663
/*!40000 ALTER TABLE t1 ENABLE KEYS */;
 
2664
SELECT DISTINCT Continent AS c FROM t1 outr WHERE
 
2665
Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND
 
2666
Population < 200);
 
2667
c
 
2668
Oceania
 
2669
drop table t1;
 
2670
create table t1 (a1 int);
 
2671
create table t2 (b1 int);
 
2672
select * from t1 where a2 > any(select b1 from t2);
 
2673
ERROR 42S22: Unknown column 'a2' in 'IN/ALL/ANY subquery'
 
2674
select * from t1 where a1 > any(select b1 from t2);
 
2675
a1
 
2676
drop table t1,t2;
 
2677
create table t1 (a integer, b integer);
 
2678
select (select * from t1) = (select 1,2);
 
2679
(select * from t1) = (select 1,2)
 
2680
NULL
 
2681
select (select 1,2) = (select * from t1);
 
2682
(select 1,2) = (select * from t1)
 
2683
NULL
 
2684
select  row(1,2) = ANY (select * from t1);
 
2685
row(1,2) = ANY (select * from t1)
 
2686
0
 
2687
select  row(1,2) != ALL (select * from t1);
 
2688
row(1,2) != ALL (select * from t1)
 
2689
1
 
2690
drop table t1;
 
2691
create table t1 (a integer, b integer);
 
2692
select row(1,(2,2)) in (select * from t1 );
 
2693
ERROR 21000: Operand should contain 2 column(s)
 
2694
select row(1,(2,2)) = (select * from t1 );
 
2695
ERROR 21000: Operand should contain 2 column(s)
 
2696
select (select * from t1) = row(1,(2,2));
 
2697
ERROR 21000: Operand should contain 1 column(s)
 
2698
drop table t1;
 
2699
create  table t1 (a integer);
 
2700
insert into t1 values (1);
 
2701
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
 
2702
ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
 
2703
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
 
2704
ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
 
2705
select 1 as xx, 1 = ALL (  select 1 from t1 where 1 = xx );
 
2706
xx      1 = ALL (  select 1 from t1 where 1 = xx )
 
2707
1       1
 
2708
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
 
2709
ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
 
2710
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL;
 
2711
ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
 
2712
drop table t1;
 
2713
CREATE TABLE t1 (
 
2714
categoryId int(11) NOT NULL,
 
2715
courseId int(11) NOT NULL,
 
2716
startDate datetime NOT NULL,
 
2717
endDate datetime NOT NULL,
 
2718
createDate datetime NOT NULL,
 
2719
modifyDate timestamp NOT NULL,
 
2720
attributes text NOT NULL
 
2721
);
 
2722
INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
 
2723
(1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
 
2724
(1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
 
2725
(2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''),
 
2726
(2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''),
 
2727
(2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
 
2728
(2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
 
2729
(3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
 
2730
(5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18','');
 
2731
CREATE TABLE t2 (
 
2732
userId int(11) NOT NULL,
 
2733
courseId int(11) NOT NULL,
 
2734
date datetime NOT NULL
 
2735
);
 
2736
INSERT INTO t2 VALUES (5141,71,'2003-11-18'),
 
2737
(5141,72,'2003-11-25'),(5141,41,'2004-08-06'),
 
2738
(5141,52,'2004-08-06'),(5141,53,'2004-08-06'),
 
2739
(5141,12,'2004-08-06'),(5141,86,'2004-10-21'),
 
2740
(5141,87,'2004-10-21'),(5141,88,'2004-10-21'),
 
2741
(5141,89,'2004-10-22'),(5141,51,'2004-10-26');
 
2742
CREATE TABLE t3 (
 
2743
groupId int(11) NOT NULL,
 
2744
parentId int(11) NOT NULL,
 
2745
startDate datetime NOT NULL,
 
2746
endDate datetime NOT NULL,
 
2747
createDate datetime NOT NULL,
 
2748
modifyDate timestamp NOT NULL,
 
2749
ordering int(11)
 
2750
);
 
2751
INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL);
 
2752
CREATE TABLE t4 (
 
2753
id int(11) NOT NULL,
 
2754
groupTypeId int(11) NOT NULL,
 
2755
groupKey varchar(50) NOT NULL,
 
2756
name text,
 
2757
ordering int(11),
 
2758
description text,
 
2759
createDate datetime NOT NULL,
 
2760
modifyDate timestamp NOT NULL
 
2761
);
 
2762
INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'),
 
2763
(12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29');
 
2764
CREATE TABLE t5 (
 
2765
userId int(11) NOT NULL,
 
2766
groupId int(11) NOT NULL,
 
2767
createDate datetime NOT NULL,
 
2768
modifyDate timestamp NOT NULL
 
2769
);
 
2770
INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06');
 
2771
select
 
2772
count(distinct t2.userid) pass,
 
2773
groupstuff.*,
 
2774
count(t2.courseid) crse,
 
2775
t1.categoryid,
 
2776
t2.courseid,
 
2777
date_format(date, '%b%y') as colhead
 
2778
from t2
 
2779
join t1 on t2.courseid=t1.courseid
 
2780
join
 
2781
(
 
2782
select
 
2783
t5.userid,
 
2784
parentid,
 
2785
parentgroup,
 
2786
childid,
 
2787
groupname,
 
2788
grouptypeid
 
2789
from t5
 
2790
join
 
2791
(
 
2792
select t4.id as parentid,
 
2793
t4.name as parentgroup,
 
2794
t4.id as childid,
 
2795
t4.name as groupname,
 
2796
t4.grouptypeid
 
2797
from t4
 
2798
) as gin on t5.groupid=gin.childid
 
2799
) as groupstuff on t2.userid = groupstuff.userid
 
2800
group by
 
2801
groupstuff.groupname, colhead , t2.courseid;
 
2802
pass    userid  parentid        parentgroup     childid groupname       grouptypeid     crse    categoryid      courseid        colhead
 
2803
1       5141    12      group2  12      group2  5       1       5       12      Aug04
 
2804
1       5141    12      group2  12      group2  5       1       1       41      Aug04
 
2805
1       5141    12      group2  12      group2  5       1       2       52      Aug04
 
2806
1       5141    12      group2  12      group2  5       1       2       53      Aug04
 
2807
1       5141    12      group2  12      group2  5       1       3       51      Oct04
 
2808
1       5141    12      group2  12      group2  5       1       1       86      Oct04
 
2809
1       5141    12      group2  12      group2  5       1       1       87      Oct04
 
2810
1       5141    12      group2  12      group2  5       1       2       88      Oct04
 
2811
1       5141    12      group2  12      group2  5       1       2       89      Oct04
 
2812
drop table t1, t2, t3, t4, t5;
 
2813
create table t1 (a int);
 
2814
insert into t1 values (1), (2), (3);
 
2815
SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1);
 
2816
1
 
2817
1
 
2818
1
 
2819
1
 
2820
drop table t1;
 
2821
create table t1 (a int);
 
2822
create table t2 (a int);
 
2823
insert into t1 values (1),(2);
 
2824
insert into t2 values (0),(1),(2),(3);
 
2825
select a from t2 where a in (select a from t1);
 
2826
a
 
2827
1
 
2828
2
 
2829
select a from t2 having a in (select a from t1);
 
2830
a
 
2831
1
 
2832
2
 
2833
prepare stmt1 from "select a from t2 where a in (select a from t1)";
 
2834
execute stmt1;
 
2835
a
 
2836
1
 
2837
2
 
2838
execute stmt1;
 
2839
a
 
2840
1
 
2841
2
 
2842
deallocate prepare stmt1;
 
2843
prepare stmt1 from "select a from t2 having a in (select a from t1)";
 
2844
execute stmt1;
 
2845
a
 
2846
1
 
2847
2
 
2848
execute stmt1;
 
2849
a
 
2850
1
 
2851
2
 
2852
deallocate prepare stmt1;
 
2853
drop table t1, t2;
 
2854
create table t1 (a int, b int);
 
2855
insert into t1 values (1,2);
 
2856
select 1 = (select * from t1);
 
2857
ERROR 21000: Operand should contain 1 column(s)
 
2858
select (select * from t1) = 1;
 
2859
ERROR 21000: Operand should contain 2 column(s)
 
2860
select (1,2) = (select a from t1);
 
2861
ERROR 21000: Operand should contain 2 column(s)
 
2862
select (select a from t1) = (1,2);
 
2863
ERROR 21000: Operand should contain 1 column(s)
 
2864
select (1,2,3) = (select * from t1);
 
2865
ERROR 21000: Operand should contain 3 column(s)
 
2866
select (select * from t1) = (1,2,3);
 
2867
ERROR 21000: Operand should contain 2 column(s)
 
2868
drop table t1;
 
2869
CREATE TABLE `t1` (
 
2870
`itemid` bigint(20) unsigned NOT NULL auto_increment,
 
2871
`sessionid` bigint(20) unsigned default NULL,
 
2872
`time` int(10) unsigned NOT NULL default '0',
 
2873
`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
 
2874
NULL default '',
 
2875
`data` text collate latin1_general_ci NOT NULL,
 
2876
PRIMARY KEY  (`itemid`)
 
2877
) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
 
2878
INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
 
2879
CREATE TABLE `t2` (
 
2880
`sessionid` bigint(20) unsigned NOT NULL auto_increment,
 
2881
`pid` int(10) unsigned NOT NULL default '0',
 
2882
`date` int(10) unsigned NOT NULL default '0',
 
2883
`ip` varchar(15) collate latin1_general_ci NOT NULL default '',
 
2884
PRIMARY KEY  (`sessionid`)
 
2885
) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
 
2886
INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
 
2887
SELECT s.ip, count( e.itemid ) FROM `t1` e JOIN t2 s ON s.sessionid = e.sessionid WHERE e.sessionid = ( SELECT sessionid FROM t2 ORDER BY sessionid DESC LIMIT 1 ) GROUP BY s.ip HAVING count( e.itemid ) >0 LIMIT 0 , 30;
 
2888
ip      count( e.itemid )
 
2889
10.10.10.1      1
 
2890
drop tables t1,t2;
 
2891
create table t1 (fld enum('0','1'));
 
2892
insert into t1 values ('1');
 
2893
select * from (select max(fld) from t1) as foo;
 
2894
max(fld)
 
2895
1
 
2896
drop table t1;
 
2897
set @tmp11867_optimizer_switch=@@optimizer_switch;
 
2898
set optimizer_switch='semijoin_with_cache=off';
 
2899
CREATE TABLE t1 (one int, two int, flag char(1));
 
2900
CREATE TABLE t2 (one int, two int, flag char(1));
 
2901
INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
 
2902
INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
 
2903
SELECT * FROM t1
 
2904
WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N');
 
2905
one     two     flag
 
2906
5       6       N
 
2907
7       8       N
 
2908
SELECT * FROM t1
 
2909
WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N');
 
2910
one     two     flag
 
2911
5       6       N
 
2912
7       8       N
 
2913
insert into t2 values (null,null,'N');
 
2914
insert into t2 values (null,3,'0');
 
2915
insert into t2 values (null,5,'0');
 
2916
insert into t2 values (10,null,'0');
 
2917
insert into t1 values (10,3,'0');
 
2918
insert into t1 values (10,5,'0');
 
2919
insert into t1 values (10,10,'0');
 
2920
SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1;
 
2921
one     two     test
 
2922
1       2       NULL
 
2923
2       3       NULL
 
2924
3       4       NULL
 
2925
5       6       1
 
2926
7       8       1
 
2927
10      3       NULL
 
2928
10      5       NULL
 
2929
10      10      NULL
 
2930
SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
 
2931
one     two
 
2932
5       6
 
2933
7       8
 
2934
SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1;
 
2935
one     two     test
 
2936
1       2       NULL
 
2937
2       3       NULL
 
2938
3       4       NULL
 
2939
5       6       1
 
2940
7       8       1
 
2941
10      3       NULL
 
2942
10      5       NULL
 
2943
10      10      NULL
 
2944
SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
 
2945
one     two     test
 
2946
1       2       0
 
2947
2       3       NULL
 
2948
3       4       0
 
2949
5       6       0
 
2950
7       8       0
 
2951
10      3       NULL
 
2952
10      5       NULL
 
2953
10      10      NULL
 
2954
SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
 
2955
one     two     test
 
2956
1       2       0
 
2957
2       3       NULL
 
2958
3       4       0
 
2959
5       6       0
 
2960
7       8       0
 
2961
10      3       NULL
 
2962
10      5       NULL
 
2963
10      10      NULL
 
2964
explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
 
2965
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
2966
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    8       100.00  
 
2967
2       DEPENDENT SUBQUERY      t2      ALL     NULL    NULL    NULL    NULL    9       100.00  Using where
 
2968
Warnings:
 
2969
Note    1003    select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1`
 
2970
explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
 
2971
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
2972
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    8       100.00  
 
2973
1       PRIMARY <subquery2>     eq_ref  distinct_key    distinct_key    8       func,func       1       100.00  
 
2974
2       MATERIALIZED    t2      ALL     NULL    NULL    NULL    NULL    9       100.00  Using where
 
2975
Warnings:
 
2976
Note    1003    select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` semi join (`test`.`t2`) where (`test`.`t2`.`flag` = 'N')
 
2977
explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
 
2978
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
2979
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    8       100.00  
 
2980
2       DEPENDENT SUBQUERY      t2      ALL     NULL    NULL    NULL    NULL    9       100.00  Using where
 
2981
Warnings:
 
2982
Note    1003    select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1`
 
2983
DROP TABLE t1,t2;
 
2984
set optimizer_switch=@tmp11867_optimizer_switch;
 
2985
CREATE TABLE t1 (a char(5), b char(5));
 
2986
INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa');
 
2987
SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb'));
 
2988
a       b
 
2989
aaa     aaa
 
2990
DROP TABLE t1;
 
2991
CREATE TABLE t1 (a int);
 
2992
CREATE TABLE t2 (a int, b int);
 
2993
CREATE TABLE t3 (b int NOT NULL);
 
2994
INSERT INTO t1 VALUES (1), (2), (3), (4);
 
2995
INSERT INTO t2 VALUES (1,10), (3,30);
 
2996
SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b
 
2997
WHERE t3.b IS NOT NULL OR t2.a > 10;
 
2998
a       b       b
 
2999
SELECT * FROM t1
 
3000
WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b
 
3001
WHERE t3.b IS NOT NULL OR t2.a > 10);
 
3002
a
 
3003
1
 
3004
2
 
3005
3
 
3006
4
 
3007
DROP TABLE t1,t2,t3;
 
3008
CREATE TABLE t1 (f1 INT);
 
3009
CREATE TABLE t2 (f2 INT);
 
3010
INSERT INTO t1 VALUES (1);
 
3011
SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2);
 
3012
f1
 
3013
1
 
3014
SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0);
 
3015
f1
 
3016
1
 
3017
INSERT INTO t2 VALUES (1);
 
3018
INSERT INTO t2 VALUES (2);
 
3019
SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0);
 
3020
f1
 
3021
1
 
3022
DROP TABLE t1, t2;
 
3023
select 1 from dual where 1 < any (select 2);
 
3024
1
 
3025
1
 
3026
select 1 from dual where 1 < all (select 2);
 
3027
1
 
3028
1
 
3029
select 1 from dual where 2 > any (select 1);
 
3030
1
 
3031
1
 
3032
select 1 from dual where 2 > all (select 1);
 
3033
1
 
3034
1
 
3035
select 1 from dual where 1 < any (select 2 from dual);
 
3036
1
 
3037
1
 
3038
select 1 from dual where 1 < all (select 2 from dual where 1!=1);
 
3039
1
 
3040
1
 
3041
create table t1 (s1 char);
 
3042
insert into t1 values (1),(2);
 
3043
select * from t1 where (s1 < any (select s1 from t1));
 
3044
s1
 
3045
1
 
3046
select * from t1 where not (s1 < any (select s1 from t1));
 
3047
s1
 
3048
2
 
3049
select * from t1 where (s1 < ALL (select s1+1 from t1));
 
3050
s1
 
3051
1
 
3052
select * from t1 where not(s1 < ALL (select s1+1 from t1));
 
3053
s1
 
3054
2
 
3055
select * from t1 where (s1+1 = ANY (select s1 from t1));
 
3056
s1
 
3057
1
 
3058
select * from t1 where NOT(s1+1 = ANY (select s1 from t1));
 
3059
s1
 
3060
2
 
3061
select * from t1 where (s1 = ALL (select s1/s1 from t1));
 
3062
s1
 
3063
1
 
3064
select * from t1 where NOT(s1 = ALL (select s1/s1 from t1));
 
3065
s1
 
3066
2
 
3067
drop table t1;
 
3068
create table t1 (
 
3069
retailerID varchar(8) NOT NULL,
 
3070
statusID   int(10) unsigned NOT NULL,
 
3071
changed    datetime NOT NULL,
 
3072
UNIQUE KEY retailerID (retailerID, statusID, changed)
 
3073
);
 
3074
INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56");
 
3075
INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53");
 
3076
INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56");
 
3077
INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53");
 
3078
INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50");
 
3079
INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50");
 
3080
select * from t1 r1
 
3081
where (r1.retailerID,(r1.changed)) in
 
3082
(SELECT r2.retailerId,(max(changed)) from t1 r2
 
3083
group by r2.retailerId);
 
3084
retailerID      statusID        changed
 
3085
0026    2       2006-01-06 12:25:53
 
3086
0037    2       2006-01-06 12:25:53
 
3087
0048    1       2006-01-06 12:37:50
 
3088
0059    1       2006-01-06 12:37:50
 
3089
drop table t1;
 
3090
create table t1(a int, primary key (a));
 
3091
insert into t1 values (10);
 
3092
create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b));
 
3093
insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989');
 
3094
explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
 
3095
ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
 
3096
             ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
 
3097
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
3098
1       PRIMARY t1      system  PRIMARY NULL    NULL    NULL    1       
 
3099
1       PRIMARY r       const   PRIMARY PRIMARY 4       const   1       
 
3100
2       SUBQUERY        t2      range   b       b       40      NULL    2       Using where
 
3101
SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
 
3102
ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
 
3103
            ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
 
3104
a       a       b
 
3105
10      3       35989
 
3106
explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
 
3107
ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
 
3108
            ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
 
3109
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
3110
1       PRIMARY t1      system  PRIMARY NULL    NULL    NULL    1       
 
3111
1       PRIMARY r       const   PRIMARY PRIMARY 4       const   1       
 
3112
2       SUBQUERY        t2      range   b       b       40      NULL    2       Using index condition
 
3113
SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
 
3114
ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
 
3115
            ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
 
3116
a       a       b
 
3117
10      1       359
 
3118
drop table t1,t2;
 
3119
CREATE TABLE t1 (
 
3120
field1 int NOT NULL,
 
3121
field2 int NOT NULL,
 
3122
field3 int NOT NULL,
 
3123
PRIMARY KEY  (field1,field2,field3)
 
3124
);
 
3125
CREATE TABLE t2 (
 
3126
fieldA int NOT NULL,
 
3127
fieldB int NOT NULL,
 
3128
PRIMARY KEY  (fieldA,fieldB)
 
3129
);
 
3130
INSERT INTO t1 VALUES
 
3131
(1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1);
 
3132
INSERT INTO t2 VALUES (1,1), (1,2), (1,3);
 
3133
SELECT field1, field2, COUNT(*)
 
3134
FROM t1 GROUP BY field1, field2;
 
3135
field1  field2  COUNT(*)
 
3136
1       1       2
 
3137
1       2       3
 
3138
1       3       1
 
3139
SELECT field1, field2
 
3140
FROM  t1
 
3141
GROUP BY field1, field2
 
3142
HAVING COUNT(*) >= ALL (SELECT fieldB
 
3143
FROM t2 WHERE fieldA = field1);
 
3144
field1  field2
 
3145
1       2
 
3146
SELECT field1, field2
 
3147
FROM  t1
 
3148
GROUP BY field1, field2
 
3149
HAVING COUNT(*) < ANY (SELECT fieldB
 
3150
FROM t2 WHERE fieldA = field1);
 
3151
field1  field2
 
3152
1       1
 
3153
1       3
 
3154
DROP TABLE t1, t2;
 
3155
CREATE TABLE t1(a int, INDEX (a));
 
3156
INSERT INTO t1 VALUES (1), (3), (5), (7);
 
3157
INSERT INTO t1 VALUES (NULL);
 
3158
CREATE TABLE t2(a int);
 
3159
INSERT INTO t2 VALUES (1),(2),(3);
 
3160
EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2;
 
3161
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
3162
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    3       
 
3163
2       SUBQUERY        t1      index_subquery  a       a       5       func    2       Using index; Full scan on NULL key
 
3164
SELECT a, a IN (SELECT a FROM t1) FROM t2;
 
3165
a       a IN (SELECT a FROM t1)
 
3166
1       1
 
3167
2       NULL
 
3168
3       1
 
3169
DROP TABLE t1,t2;
 
3170
CREATE TABLE t1 (a DATETIME);
 
3171
INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25');
 
3172
CREATE TABLE t2 AS SELECT
 
3173
(SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a
 
3174
FROM t1 WHERE a > '2000-01-01';
 
3175
SHOW CREATE TABLE t2;
 
3176
Table   Create Table
 
3177
t2      CREATE TABLE `t2` (
 
3178
  `sub_a` datetime DEFAULT NULL
 
3179
) ENGINE=MyISAM DEFAULT CHARSET=latin1
 
3180
CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01');
 
3181
SHOW CREATE TABLE t3;
 
3182
Table   Create Table
 
3183
t3      CREATE TABLE `t3` (
 
3184
  `a` datetime DEFAULT NULL
 
3185
) ENGINE=MyISAM DEFAULT CHARSET=latin1
 
3186
DROP TABLE t1,t2,t3;
 
3187
CREATE TABLE t1 (a int);
 
3188
INSERT INTO t1 VALUES (1), (2);
 
3189
SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0;
 
3190
a
 
3191
SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
 
3192
a
 
3193
1
 
3194
2
 
3195
EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
 
3196
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
3197
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       
 
3198
2       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE
 
3199
DROP TABLE t1;
 
3200
CREATE TABLE t1 (a int);
 
3201
INSERT INTO t1 VALUES (2), (4), (1), (3);
 
3202
CREATE TABLE t2 (b int, c int);
 
3203
INSERT INTO t2 VALUES
 
3204
(2,1), (1,3), (2,1), (4,4), (2,2), (1,4);
 
3205
SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 );
 
3206
a
 
3207
2
 
3208
4
 
3209
1
 
3210
3
 
3211
SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1);
 
3212
ERROR 21000: Subquery returns more than 1 row
 
3213
SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a;
 
3214
a
 
3215
1
 
3216
2
 
3217
3
 
3218
4
 
3219
SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a;
 
3220
ERROR 21000: Subquery returns more than 1 row
 
3221
SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2);
 
3222
b       MAX(c)
 
3223
1       4
 
3224
2       2
 
3225
4       4
 
3226
SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1);
 
3227
ERROR 21000: Subquery returns more than 1 row
 
3228
SELECT a FROM t1 GROUP BY a
 
3229
HAVING IFNULL((SELECT b FROM t2 WHERE b > 2),
 
3230
(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
 
3231
a
 
3232
1
 
3233
2
 
3234
3
 
3235
4
 
3236
SELECT a FROM t1 GROUP BY a
 
3237
HAVING IFNULL((SELECT b FROM t2 WHERE b > 1),
 
3238
(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
 
3239
ERROR 21000: Subquery returns more than 1 row
 
3240
SELECT a FROM t1 GROUP BY a
 
3241
HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
 
3242
(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
 
3243
a
 
3244
4
 
3245
SELECT a FROM t1 GROUP BY a
 
3246
HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
 
3247
(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3;
 
3248
ERROR 21000: Subquery returns more than 1 row
 
3249
SELECT a FROM t1
 
3250
ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2),
 
3251
(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
 
3252
a
 
3253
1
 
3254
2
 
3255
3
 
3256
4
 
3257
SELECT a FROM t1
 
3258
ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1),
 
3259
(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
 
3260
ERROR 21000: Subquery returns more than 1 row
 
3261
SELECT a FROM t1
 
3262
ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
 
3263
(SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
 
3264
a
 
3265
1
 
3266
2
 
3267
3
 
3268
4
 
3269
SELECT a FROM t1
 
3270
ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
 
3271
(SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
 
3272
ERROR 21000: Subquery returns more than 1 row
 
3273
DROP TABLE t1,t2;
 
3274
create table t1 (df decimal(5,1));
 
3275
insert into t1 values(1.1);
 
3276
insert into t1 values(2.2);
 
3277
select * from t1 where df <= all (select avg(df) from t1 group by df);
 
3278
df
 
3279
1.1
 
3280
select * from t1 where df >= all (select avg(df) from t1 group by df);
 
3281
df
 
3282
2.2
 
3283
drop table t1;
 
3284
create table t1 (df decimal(5,1));
 
3285
insert into t1 values(1.1);
 
3286
select 1.1 * exists(select * from t1);
 
3287
1.1 * exists(select * from t1)
 
3288
1.1
 
3289
drop table t1;
 
3290
CREATE TABLE t1 (
 
3291
grp int(11) default NULL,
 
3292
a decimal(10,2) default NULL);
 
3293
insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL);
 
3294
select * from t1;
 
3295
grp     a
 
3296
1       1.00
 
3297
2       2.00
 
3298
2       3.00
 
3299
3       4.00
 
3300
3       5.00
 
3301
3       6.00
 
3302
NULL    NULL
 
3303
select min(a) from t1 group by grp;
 
3304
min(a)
 
3305
NULL
 
3306
1.00
 
3307
2.00
 
3308
4.00
 
3309
drop table t1;
 
3310
CREATE table t1 ( c1 integer );
 
3311
INSERT INTO t1 VALUES ( 1 );
 
3312
INSERT INTO t1 VALUES ( 2 );
 
3313
INSERT INTO t1 VALUES ( 3 );
 
3314
CREATE TABLE t2 ( c2 integer );
 
3315
INSERT INTO t2 VALUES ( 1 );
 
3316
INSERT INTO t2 VALUES ( 4 );
 
3317
INSERT INTO t2 VALUES ( 5 );
 
3318
SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1);
 
3319
c1      c2
 
3320
1       1
 
3321
SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2
 
3322
WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) );
 
3323
c1      c2
 
3324
1       1
 
3325
DROP TABLE t1,t2;
 
3326
CREATE TABLE t1 ( c1 integer );
 
3327
INSERT INTO t1 VALUES ( 1 );
 
3328
INSERT INTO t1 VALUES ( 2 );
 
3329
INSERT INTO t1 VALUES ( 3 );
 
3330
INSERT INTO t1 VALUES ( 6 );
 
3331
CREATE TABLE t2 ( c2 integer );
 
3332
INSERT INTO t2 VALUES ( 1 );
 
3333
INSERT INTO t2 VALUES ( 4 );
 
3334
INSERT INTO t2 VALUES ( 5 );
 
3335
INSERT INTO t2 VALUES ( 6 );
 
3336
CREATE TABLE t3 ( c3 integer );
 
3337
INSERT INTO t3 VALUES ( 7 );
 
3338
INSERT INTO t3 VALUES ( 8 );
 
3339
SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2
 
3340
WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL );
 
3341
c1      c2
 
3342
2       NULL
 
3343
3       NULL
 
3344
DROP TABLE t1,t2,t3;
 
3345
CREATE TABLE `t1` (
 
3346
`itemid` bigint(20) unsigned NOT NULL auto_increment,
 
3347
`sessionid` bigint(20) unsigned default NULL,
 
3348
`time` int(10) unsigned NOT NULL default '0',
 
3349
`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
 
3350
NULL default '',
 
3351
`data` text collate latin1_general_ci NOT NULL,
 
3352
PRIMARY KEY  (`itemid`)
 
3353
) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
 
3354
INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
 
3355
CREATE TABLE `t2` (
 
3356
`sessionid` bigint(20) unsigned NOT NULL auto_increment,
 
3357
`pid` int(10) unsigned NOT NULL default '0',
 
3358
`date` int(10) unsigned NOT NULL default '0',
 
3359
`ip` varchar(15) collate latin1_general_ci NOT NULL default '',
 
3360
PRIMARY KEY  (`sessionid`)
 
3361
) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
 
3362
INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
 
3363
SELECT s.ip, count( e.itemid ) FROM `t1` e JOIN t2 s ON s.sessionid = e.sessionid WHERE e.sessionid = ( SELECT sessionid FROM t2 ORDER BY sessionid DESC LIMIT 1 ) GROUP BY s.ip HAVING count( e.itemid ) >0 LIMIT 0 , 30;
 
3364
ip      count( e.itemid )
 
3365
10.10.10.1      1
 
3366
drop tables t1,t2;
 
3367
CREATE TABLE t1 (EMPNUM   CHAR(3));
 
3368
CREATE TABLE t2 (EMPNUM   CHAR(3) );
 
3369
INSERT INTO t1 VALUES ('E1'),('E2');
 
3370
INSERT INTO t2 VALUES ('E1');
 
3371
DELETE FROM t1
 
3372
WHERE t1.EMPNUM NOT IN
 
3373
(SELECT t2.EMPNUM
 
3374
FROM t2
 
3375
WHERE t1.EMPNUM = t2.EMPNUM);
 
3376
select * from t1;
 
3377
EMPNUM
 
3378
E1
 
3379
DROP TABLE t1,t2;
 
3380
CREATE TABLE t1(select_id BIGINT, values_id BIGINT);
 
3381
INSERT INTO t1 VALUES (1, 1);
 
3382
CREATE TABLE t2 (select_id BIGINT, values_id BIGINT,
 
3383
PRIMARY KEY(select_id,values_id));
 
3384
INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5);
 
3385
SELECT values_id FROM t1
 
3386
WHERE values_id IN (SELECT values_id FROM t2
 
3387
WHERE select_id IN (1, 0));
 
3388
values_id
 
3389
1
 
3390
SELECT values_id FROM t1
 
3391
WHERE values_id IN (SELECT values_id FROM t2
 
3392
WHERE select_id BETWEEN 0 AND 1);
 
3393
values_id
 
3394
1
 
3395
SELECT values_id FROM t1
 
3396
WHERE values_id IN (SELECT values_id FROM t2
 
3397
WHERE select_id = 0 OR select_id = 1);
 
3398
values_id
 
3399
1
 
3400
DROP TABLE t1, t2;
 
3401
create table t1 (fld enum('0','1'));
 
3402
insert into t1 values ('1');
 
3403
select * from (select max(fld) from t1) as foo;
 
3404
max(fld)
 
3405
1
 
3406
drop table t1;
 
3407
CREATE TABLE t1 (a int, b int);
 
3408
CREATE TABLE t2 (c int, d int);
 
3409
CREATE TABLE t3 (e int);
 
3410
INSERT INTO t1 VALUES
 
3411
(1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
 
3412
INSERT INTO t2 VALUES
 
3413
(2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
 
3414
INSERT INTO t3 VALUES (10), (30), (10), (20) ;
 
3415
SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a;
 
3416
a       MAX(b)  MIN(b)
 
3417
1       20      10
 
3418
2       30      10
 
3419
3       20      20
 
3420
4       40      40
 
3421
SELECT * FROM t2;
 
3422
c       d
 
3423
2       10
 
3424
2       20
 
3425
4       10
 
3426
5       10
 
3427
3       20
 
3428
2       40
 
3429
SELECT * FROM t3;
 
3430
e
 
3431
10
 
3432
30
 
3433
10
 
3434
20
 
3435
SELECT a FROM t1 GROUP BY a
 
3436
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
 
3437
a
 
3438
2
 
3439
4
 
3440
SELECT a FROM t1 GROUP BY a
 
3441
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d);
 
3442
a
 
3443
2
 
3444
SELECT a FROM t1 GROUP BY a
 
3445
HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d);
 
3446
a
 
3447
2
 
3448
4
 
3449
SELECT a FROM t1 GROUP BY a
 
3450
HAVING a IN (SELECT c FROM t2
 
3451
WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e));
 
3452
a
 
3453
2
 
3454
3
 
3455
SELECT a FROM t1 GROUP BY a
 
3456
HAVING a IN (SELECT c FROM t2
 
3457
WHERE  EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
 
3458
a
 
3459
2
 
3460
3
 
3461
SELECT a FROM t1 GROUP BY a
 
3462
HAVING a IN (SELECT c FROM t2
 
3463
WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e));
 
3464
a
 
3465
2
 
3466
SELECT a FROM t1 GROUP BY a
 
3467
HAVING a IN (SELECT c FROM t2
 
3468
WHERE  EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d));
 
3469
a
 
3470
2
 
3471
SELECT a FROM t1 GROUP BY a
 
3472
HAVING a IN (SELECT c FROM t2
 
3473
WHERE MIN(b) < d AND
 
3474
EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
 
3475
a
 
3476
2
 
3477
SELECT a, SUM(a) FROM t1 GROUP BY a;
 
3478
a       SUM(a)
 
3479
1       2
 
3480
2       6
 
3481
3       3
 
3482
4       4
 
3483
SELECT a FROM t1
 
3484
WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a;
 
3485
a
 
3486
3
 
3487
4
 
3488
SELECT a FROM t1 GROUP BY a
 
3489
HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c);
 
3490
a
 
3491
1
 
3492
3
 
3493
4
 
3494
SELECT a FROM t1
 
3495
WHERE a < 3 AND
 
3496
EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a;
 
3497
a
 
3498
1
 
3499
2
 
3500
SELECT a FROM t1
 
3501
WHERE a < 3 AND
 
3502
EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c);
 
3503
a
 
3504
1
 
3505
2
 
3506
1
 
3507
2
 
3508
2
 
3509
SELECT t1.a FROM t1 GROUP BY t1.a
 
3510
HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c
 
3511
HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
 
3512
HAVING SUM(t1.a+t2.c) < t3.e/4));
 
3513
a
 
3514
1
 
3515
2
 
3516
SELECT t1.a FROM t1 GROUP BY t1.a
 
3517
HAVING t1.a > ALL(SELECT t2.c FROM t2
 
3518
WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
 
3519
HAVING SUM(t1.a+t2.c) < t3.e/4));
 
3520
a
 
3521
4
 
3522
SELECT t1.a FROM t1 GROUP BY t1.a
 
3523
HAVING t1.a > ALL(SELECT t2.c FROM t2
 
3524
WHERE EXISTS(SELECT t3.e FROM t3
 
3525
WHERE SUM(t1.a+t2.c) < t3.e/4));
 
3526
ERROR HY000: Invalid use of group function
 
3527
SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20;
 
3528
ERROR HY000: Invalid use of group function
 
3529
SELECT t1.a FROM t1 GROUP BY t1.a
 
3530
HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
 
3531
HAVING AVG(t2.c+SUM(t1.b)) > 20);
 
3532
a
 
3533
2
 
3534
3
 
3535
4
 
3536
SELECT t1.a FROM t1 GROUP BY t1.a
 
3537
HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
 
3538
HAVING AVG(SUM(t1.b)) > 20);
 
3539
a
 
3540
2
 
3541
4
 
3542
SELECT t1.a, SUM(b) AS sum  FROM t1 GROUP BY t1.a
 
3543
HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
 
3544
HAVING t2.c+sum > 20);
 
3545
a       sum
 
3546
2       60
 
3547
3       20
 
3548
4       40
 
3549
DROP TABLE t1,t2,t3;
 
3550
CREATE TABLE t1 (a varchar(5), b varchar(10));
 
3551
INSERT INTO t1 VALUES
 
3552
('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2),
 
3553
('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8);
 
3554
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
 
3555
a       b
 
3556
BBB     4
 
3557
CCC     7
 
3558
AAA     8
 
3559
EXPLAIN
 
3560
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
 
3561
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
3562
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    9       Using where
 
3563
1       PRIMARY <subquery2>     eq_ref  distinct_key    distinct_key    21      test.t1.a,test.t1.b     1       
 
3564
2       MATERIALIZED    t1      ALL     NULL    NULL    NULL    NULL    9       Using temporary
 
3565
ALTER TABLE t1 ADD INDEX(a);
 
3566
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
 
3567
a       b
 
3568
BBB     4
 
3569
CCC     7
 
3570
AAA     8
 
3571
EXPLAIN
 
3572
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
 
3573
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
3574
1       PRIMARY t1      ALL     a       NULL    NULL    NULL    9       Using where
 
3575
1       PRIMARY <subquery2>     eq_ref  distinct_key    distinct_key    21      test.t1.a,test.t1.b     1       
 
3576
2       MATERIALIZED    t1      ALL     NULL    NULL    NULL    NULL    9       Using temporary
 
3577
DROP TABLE t1;
 
3578
create table t1( f1 int,f2 int);
 
3579
insert into t1 values (1,1),(2,2);
 
3580
select tt.t from (select 'crash1' as t, f2 from t1) as tt left join t1 on tt.t = 'crash2' and tt.f2 = t1.f2 where tt.t = 'crash1';
 
3581
t
 
3582
crash1
 
3583
crash1
 
3584
drop table t1;
 
3585
create table t1 (c int, key(c));
 
3586
insert into t1 values (1142477582), (1142455969);
 
3587
create table t2 (a int, b int);
 
3588
insert into t2 values (2, 1), (1, 0);
 
3589
delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
 
3590
drop table t1, t2;
 
3591
CREATE TABLE t1 (a INT);
 
3592
CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
 
3593
ERROR 42S22: Unknown column 'no_such_column' in 'where clause'
 
3594
CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1);
 
3595
ERROR 42S22: Unknown column 'no_such_column' in 'where clause'
 
3596
SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
 
3597
ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery'
 
3598
DROP TABLE t1;
 
3599
create table t1 (i int, j bigint);
 
3600
insert into t1 values (1, 2), (2, 2), (3, 2);
 
3601
select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
 
3602
min(i)
 
3603
1
 
3604
drop table t1;
 
3605
CREATE TABLE t1 (i BIGINT UNSIGNED);
 
3606
INSERT INTO t1 VALUES (10000000000000000000);
 
3607
INSERT INTO t1 VALUES (1);
 
3608
CREATE TABLE t2 (i BIGINT UNSIGNED);
 
3609
INSERT INTO t2 VALUES (10000000000000000000);
 
3610
INSERT INTO t2 VALUES (1);
 
3611
/* simple test */
 
3612
SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i;
 
3613
i
 
3614
10000000000000000000
 
3615
1
 
3616
/* subquery test */
 
3617
SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
 
3618
i
 
3619
10000000000000000000
 
3620
/* subquery test with cast*/
 
3621
SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);
 
3622
i
 
3623
10000000000000000000
 
3624
DROP TABLE t1;
 
3625
DROP TABLE t2;
 
3626
CREATE TABLE t1 (
 
3627
id bigint(20) unsigned NOT NULL auto_increment,
 
3628
name varchar(255) NOT NULL,
 
3629
PRIMARY KEY  (id)
 
3630
);
 
3631
INSERT INTO t1 VALUES
 
3632
(1, 'Balazs'), (2, 'Joe'), (3, 'Frank');
 
3633
CREATE TABLE t2 (
 
3634
id bigint(20) unsigned NOT NULL auto_increment,
 
3635
mid bigint(20) unsigned NOT NULL,
 
3636
date date NOT NULL,
 
3637
PRIMARY KEY  (id)
 
3638
);
 
3639
INSERT INTO t2 VALUES
 
3640
(1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'),
 
3641
(4, 2, '2006-04-20'), (5, 1, '2006-05-01');
 
3642
SELECT *,
 
3643
(SELECT date FROM t2 WHERE mid = t1.id
 
3644
ORDER BY date DESC LIMIT 0, 1) AS date_last,
 
3645
(SELECT date FROM t2 WHERE mid = t1.id
 
3646
ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last
 
3647
FROM t1;
 
3648
id      name    date_last       date_next_to_last
 
3649
1       Balazs  2006-05-01      NULL
 
3650
2       Joe     2006-04-20      NULL
 
3651
3       Frank   2006-04-13      NULL
 
3652
SELECT *,
 
3653
(SELECT COUNT(*) FROM t2 WHERE mid = t1.id
 
3654
ORDER BY date DESC LIMIT 1, 1) AS date_count
 
3655
FROM t1;
 
3656
id      name    date_count
 
3657
1       Balazs  NULL
 
3658
2       Joe     NULL
 
3659
3       Frank   NULL
 
3660
SELECT *,
 
3661
(SELECT date FROM t2 WHERE mid = t1.id
 
3662
ORDER BY date DESC LIMIT 0, 1) AS date_last,
 
3663
(SELECT date FROM t2 WHERE mid = t1.id
 
3664
ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last
 
3665
FROM t1;
 
3666
id      name    date_last       date_next_to_last
 
3667
1       Balazs  2006-05-01      2006-03-30
 
3668
2       Joe     2006-04-20      2006-04-06
 
3669
3       Frank   2006-04-13      NULL
 
3670
DROP TABLE t1,t2;
 
3671
CREATE TABLE t1 (
 
3672
i1 int(11) NOT NULL default '0',
 
3673
i2 int(11) NOT NULL default '0',
 
3674
t datetime NOT NULL default '0000-00-00 00:00:00',
 
3675
PRIMARY KEY  (i1,i2,t)
 
3676
);
 
3677
INSERT INTO t1 VALUES
 
3678
(24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'),
 
3679
(24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'),
 
3680
(24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'),
 
3681
(24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'),
 
3682
(24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'),
 
3683
(24,2,'2005-05-27 12:40:06');
 
3684
CREATE TABLE t2 (
 
3685
i1 int(11) NOT NULL default '0',
 
3686
i2 int(11) NOT NULL default '0',
 
3687
t datetime default NULL,
 
3688
PRIMARY KEY  (i1)
 
3689
);
 
3690
INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40');
 
3691
EXPLAIN
 
3692
SELECT * FROM t1,t2
 
3693
WHERE t1.t = (SELECT t1.t FROM t1
 
3694
WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
 
3695
ORDER BY t1.t DESC LIMIT 1);
 
3696
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
3697
1       PRIMARY t2      system  NULL    NULL    NULL    NULL    1       
 
3698
1       PRIMARY t1      index   NULL    PRIMARY 16      NULL    11      Using where; Using index
 
3699
2       SUBQUERY        t1      range   PRIMARY PRIMARY 16      NULL    5       Using where; Using index
 
3700
SELECT * FROM t1,t2
 
3701
WHERE t1.t = (SELECT t1.t FROM t1
 
3702
WHERE t1.t < t2.t  AND t1.i2=1 AND t2.i1=t1.i1
 
3703
ORDER BY t1.t DESC LIMIT 1);
 
3704
i1      i2      t       i1      i2      t
 
3705
24      1       2005-05-27 12:40:30     24      1       2006-06-20 12:29:40
 
3706
DROP TABLE t1, t2;
 
3707
CREATE TABLE t1 (i INT);
 
3708
(SELECT i FROM t1) UNION (SELECT i FROM t1);
 
3709
i
 
3710
SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS
 
3711
(
 
3712
(SELECT i FROM t1) UNION
 
3713
(SELECT i FROM t1)
 
3714
);
 
3715
i
 
3716
SELECT * FROM t1
 
3717
WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
 
3718
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION (SELECT i FROM t1)))' at line 2
 
3719
explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12))
 
3720
from t1;
 
3721
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'union (select t12.i from t1 t12))
 
3722
from t1' at line 1
 
3723
explain select * from t1 where not exists
 
3724
((select t11.i from t1 t11) union (select t12.i from t1 t12));
 
3725
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
3726
1       PRIMARY t1      system  NULL    NULL    NULL    NULL    0       const row not found
 
3727
2       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    no matching row in const table
 
3728
3       UNION   NULL    NULL    NULL    NULL    NULL    NULL    NULL    no matching row in const table
 
3729
NULL    UNION RESULT    <union2,3>      ALL     NULL    NULL    NULL    NULL    NULL    
 
3730
DROP TABLE t1;
 
3731
CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b));
 
3732
insert into t1 (a) values (FLOOR(rand() * 100));
 
3733
insert into t1 (a) select FLOOR(rand() * 100) from t1;
 
3734
insert into t1 (a) select FLOOR(rand() * 100) from t1;
 
3735
insert into t1 (a) select FLOOR(rand() * 100) from t1;
 
3736
insert into t1 (a) select FLOOR(rand() * 100) from t1;
 
3737
insert into t1 (a) select FLOOR(rand() * 100) from t1;
 
3738
insert into t1 (a) select FLOOR(rand() * 100) from t1;
 
3739
insert into t1 (a) select FLOOR(rand() * 100) from t1;
 
3740
insert into t1 (a) select FLOOR(rand() * 100) from t1;
 
3741
insert into t1 (a) select FLOOR(rand() * 100) from t1;
 
3742
insert into t1 (a) select FLOOR(rand() * 100) from t1;
 
3743
insert into t1 (a) select FLOOR(rand() * 100) from t1;
 
3744
insert into t1 (a) select FLOOR(rand() * 100) from t1;
 
3745
insert into t1 (a) select FLOOR(rand() * 100) from t1;
 
3746
SELECT a,
 
3747
(SELECT REPEAT(' ',250) FROM t1 i1
 
3748
WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a
 
3749
FROM t1 ORDER BY a LIMIT 5;
 
3750
a       a
 
3751
0       NULL
 
3752
0       NULL
 
3753
0       NULL
 
3754
0       NULL
 
3755
0       NULL
 
3756
DROP TABLE t1;
 
3757
CREATE TABLE t1 (a INT, b INT);
 
3758
CREATE TABLE t2 (a INT);
 
3759
INSERT INTO t2 values (1);
 
3760
INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
 
3761
SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
 
3762
(SELECT COUNT(DISTINCT t1.b) from t2)
 
3763
2
 
3764
1
 
3765
1
 
3766
SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
 
3767
FROM t1 GROUP BY t1.a;
 
3768
(SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
 
3769
2
 
3770
1
 
3771
1
 
3772
SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
 
3773
COUNT(DISTINCT t1.b)    (SELECT COUNT(DISTINCT t1.b))
 
3774
2       2
 
3775
1       1
 
3776
1       1
 
3777
SELECT COUNT(DISTINCT t1.b),
 
3778
(SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
 
3779
FROM t1 GROUP BY t1.a;
 
3780
COUNT(DISTINCT t1.b)    (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
 
3781
2       2
 
3782
1       1
 
3783
1       1
 
3784
SELECT (
 
3785
SELECT (
 
3786
SELECT COUNT(DISTINCT t1.b)
 
3787
)
 
3788
)
 
3789
FROM t1 GROUP BY t1.a;
 
3790
(
 
3791
SELECT (
 
3792
SELECT COUNT(DISTINCT t1.b)
 
3793
)
 
3794
)
 
3795
2
 
3796
1
 
3797
1
 
3798
SELECT (
 
3799
SELECT (
 
3800
SELECT (
 
3801
SELECT COUNT(DISTINCT t1.b)
 
3802
)
 
3803
)
 
3804
FROM t1 GROUP BY t1.a LIMIT 1)
 
3805
FROM t1 t2
 
3806
GROUP BY t2.a;
 
3807
(
 
3808
SELECT (
 
3809
SELECT (
 
3810
SELECT COUNT(DISTINCT t1.b)
 
3811
)
 
3812
)
 
3813
FROM t1 GROUP BY t1.a LIMIT 1)
 
3814
2
 
3815
2
 
3816
2
 
3817
DROP TABLE t1,t2;
 
3818
CREATE TABLE t1 (a int, b int, PRIMARY KEY (b));
 
3819
CREATE TABLE t2 (x int auto_increment, y int, z int,
 
3820
PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b));
 
3821
create table t3 (a int);
 
3822
insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
 
3823
insert into t1 select RAND()*1000, A.a + 10*(B.a+10*(C.a+10*D.a))
 
3824
from t3 A, t3 B, t3 C, t3 D where D.a<3;
 
3825
insert into t2(y,z) select t1.b, RAND()*1000 from t1, t3;
 
3826
SET SESSION sort_buffer_size = 32 * 1024;
 
3827
SELECT SQL_NO_CACHE COUNT(*)
 
3828
FROM (SELECT  a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
 
3829
FROM t1) t;
 
3830
COUNT(*)
 
3831
3000
 
3832
SET SESSION sort_buffer_size = 8 * 1024 * 1024;
 
3833
SELECT SQL_NO_CACHE COUNT(*)
 
3834
FROM (SELECT  a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
 
3835
FROM t1) t;
 
3836
COUNT(*)
 
3837
3000
 
3838
DROP TABLE t1,t2,t3;
 
3839
CREATE TABLE t1 (id char(4) PRIMARY KEY, c int);
 
3840
CREATE TABLE t2 (c int);
 
3841
INSERT INTO t1 VALUES ('aa', 1);
 
3842
INSERT INTO t2 VALUES (1);
 
3843
SELECT * FROM t1
 
3844
WHERE EXISTS (SELECT c FROM t2 WHERE c=1
 
3845
UNION
 
3846
SELECT c from t2 WHERE c=t1.c);
 
3847
id      c
 
3848
aa      1
 
3849
INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1);
 
3850
SELECT * FROM t1
 
3851
WHERE EXISTS (SELECT c FROM t2 WHERE c=1
 
3852
UNION
 
3853
SELECT c from t2 WHERE c=t1.c);
 
3854
id      c
 
3855
aa      1
 
3856
bb      2
 
3857
cc      3
 
3858
dd      1
 
3859
INSERT INTO t2 VALUES (2);
 
3860
CREATE TABLE t3 (c int);
 
3861
INSERT INTO t3 VALUES (1);
 
3862
SELECT * FROM t1
 
3863
WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1
 
3864
UNION
 
3865
SELECT c from t2 WHERE c=t1.c);
 
3866
id      c
 
3867
aa      1
 
3868
bb      2
 
3869
cc      3
 
3870
dd      1
 
3871
DROP TABLE t1,t2,t3;
 
3872
CREATE TABLE t1(f1 int);
 
3873
CREATE TABLE t2(f2 int, f21 int, f3 timestamp);
 
3874
INSERT INTO t1 VALUES (1),(1),(2),(2);
 
3875
INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11");
 
3876
SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1;
 
3877
sq
 
3878
2
 
3879
4
 
3880
SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2;
 
3881
tt
 
3882
2
 
3883
2
 
3884
PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1';
 
3885
EXECUTE stmt1;
 
3886
sq
 
3887
2
 
3888
4
 
3889
EXECUTE stmt1;
 
3890
sq
 
3891
2
 
3892
4
 
3893
DEALLOCATE PREPARE stmt1;
 
3894
SELECT f2, AVG(f21),
 
3895
(SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test
 
3896
FROM t2 GROUP BY f2;
 
3897
f2      AVG(f21)        test
 
3898
1       1.0000  2004-02-29 11:11:11
 
3899
2       2.0000  2004-02-29 11:11:11
 
3900
DROP TABLE t1,t2;
 
3901
CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL);
 
3902
INSERT INTO t1 VALUES
 
3903
(1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
 
3904
(2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'),
 
3905
(3,2,'k'), (3,1,'l'), (1,9,'m');
 
3906
SELECT a, MAX(b),
 
3907
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test
 
3908
FROM t1 GROUP BY a;
 
3909
a       MAX(b)  test
 
3910
1       9       m
 
3911
2       3       h
 
3912
3       4       i
 
3913
DROP TABLE t1;
 
3914
DROP TABLE IF EXISTS t1;
 
3915
DROP TABLE IF EXISTS t2;
 
3916
DROP TABLE IF EXISTS t1xt2;
 
3917
CREATE TABLE t1 (
 
3918
id_1 int(5) NOT NULL,
 
3919
t varchar(4) DEFAULT NULL
 
3920
);
 
3921
CREATE TABLE t2 (
 
3922
id_2 int(5) NOT NULL,
 
3923
t varchar(4) DEFAULT NULL
 
3924
);
 
3925
CREATE TABLE t1xt2 (
 
3926
id_1 int(5) NOT NULL,
 
3927
id_2 int(5) NOT NULL
 
3928
);
 
3929
INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd');
 
3930
INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa');
 
3931
INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4);
 
3932
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
3933
(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
 
3934
id_1
 
3935
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
3936
(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
 
3937
id_1
 
3938
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
3939
(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
 
3940
id_1
 
3941
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
3942
(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
 
3943
id_1
 
3944
1
 
3945
2
 
3946
3
 
3947
4
 
3948
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
3949
(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)));
 
3950
id_1
 
3951
1
 
3952
2
 
3953
3
 
3954
4
 
3955
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
3956
(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))));
 
3957
id_1
 
3958
1
 
3959
2
 
3960
3
 
3961
4
 
3962
insert INTO t1xt2 VALUES (1, 12);
 
3963
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
3964
(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
 
3965
id_1
 
3966
1
 
3967
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
3968
(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
 
3969
id_1
 
3970
1
 
3971
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
3972
(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
 
3973
id_1
 
3974
1
 
3975
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
3976
(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
 
3977
id_1
 
3978
2
 
3979
3
 
3980
4
 
3981
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
3982
(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
 
3983
id_1
 
3984
2
 
3985
3
 
3986
4
 
3987
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
3988
(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
 
3989
id_1
 
3990
2
 
3991
3
 
3992
4
 
3993
insert INTO t1xt2 VALUES (2, 12);
 
3994
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
3995
(12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
 
3996
id_1
 
3997
1
 
3998
2
 
3999
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
4000
(12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
 
4001
id_1
 
4002
1
 
4003
2
 
4004
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
4005
(12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
 
4006
id_1
 
4007
1
 
4008
2
 
4009
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
4010
(12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
 
4011
id_1
 
4012
3
 
4013
4
 
4014
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
4015
(12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
 
4016
id_1
 
4017
3
 
4018
4
 
4019
SELECT DISTINCT t1.id_1 FROM t1 WHERE
 
4020
(12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
 
4021
id_1
 
4022
3
 
4023
4
 
4024
DROP TABLE t1;
 
4025
DROP TABLE t2;
 
4026
DROP TABLE t1xt2;
 
4027
CREATE TABLE t1 (a int);
 
4028
INSERT INTO t1 VALUES (3), (1), (2);
 
4029
SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1;
 
4030
col1    col2
 
4031
this is a test. 3
 
4032
this is a test. 1
 
4033
this is a test. 2
 
4034
SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t;
 
4035
col1    t2
 
4036
this is a test. 3
 
4037
this is a test. 1
 
4038
this is a test. 2
 
4039
DROP table t1;
 
4040
CREATE TABLE t1 (a int, b int);
 
4041
CREATE TABLE t2 (m int, n int);
 
4042
INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
 
4043
INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
 
4044
SELECT COUNT(*), a,
 
4045
(SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
 
4046
FROM t1 GROUP BY a;
 
4047
COUNT(*)        a       (SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
 
4048
2       2       2
 
4049
3       3       3
 
4050
1       4       1
 
4051
SELECT COUNT(*), a,
 
4052
(SELECT MIN(m) FROM t2 WHERE m = count(*))
 
4053
FROM t1 GROUP BY a;
 
4054
COUNT(*)        a       (SELECT MIN(m) FROM t2 WHERE m = count(*))
 
4055
2       2       2
 
4056
3       3       3
 
4057
1       4       1
 
4058
SELECT COUNT(*), a
 
4059
FROM t1 GROUP BY a
 
4060
HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1;
 
4061
COUNT(*)        a
 
4062
2       2
 
4063
3       3
 
4064
DROP TABLE t1,t2;
 
4065
CREATE TABLE t1 (a int, b int);
 
4066
CREATE TABLE t2 (m int, n int);
 
4067
INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
 
4068
INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
 
4069
SELECT COUNT(*) c, a,
 
4070
(SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
 
4071
FROM t1 GROUP BY a;
 
4072
c       a       (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
 
4073
2       2       2
 
4074
3       3       3
 
4075
1       4       1,1
 
4076
SELECT COUNT(*) c, a,
 
4077
(SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
 
4078
FROM t1 GROUP BY a;
 
4079
c       a       (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
 
4080
2       2       3
 
4081
3       3       4
 
4082
1       4       2,2
 
4083
DROP table t1,t2;
 
4084
CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
 
4085
INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'),
 
4086
(1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'),
 
4087
(3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p');
 
4088
SELECT a, MAX(b),
 
4089
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test
 
4090
FROM t1 GROUP BY a;
 
4091
a       MAX(b)  test
 
4092
1       9       m
 
4093
2       3       h
 
4094
3       4       i
 
4095
SELECT a x, MAX(b),
 
4096
(SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test
 
4097
FROM t1 GROUP BY a;
 
4098
x       MAX(b)  test
 
4099
1       9       m
 
4100
2       3       h
 
4101
3       4       i
 
4102
SELECT a, AVG(b),
 
4103
(SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test
 
4104
FROM t1 WHERE t1.d=0 GROUP BY a;
 
4105
a       AVG(b)  test
 
4106
1       4.0000  d
 
4107
2       2.0000  g
 
4108
3       2.5000  NULL
 
4109
SELECT tt.a,
 
4110
(SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
 
4111
LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
 
4112
FROM t1 as tt;
 
4113
a       test
 
4114
1       n
 
4115
1       n
 
4116
1       n
 
4117
1       n
 
4118
1       n
 
4119
1       n
 
4120
1       n
 
4121
2       o
 
4122
2       o
 
4123
2       o
 
4124
2       o
 
4125
3       p
 
4126
3       p
 
4127
3       p
 
4128
3       p
 
4129
3       p
 
4130
SELECT tt.a,
 
4131
(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
 
4132
LIMIT 1)
 
4133
FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
 
4134
FROM t1 as tt GROUP BY tt.a;
 
4135
a       test
 
4136
1       n
 
4137
2       o
 
4138
3       p
 
4139
SELECT tt.a, MAX(
 
4140
(SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
 
4141
LIMIT 1)
 
4142
FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test
 
4143
FROM t1 as tt GROUP BY tt.a;
 
4144
a       test
 
4145
1       n
 
4146
2       o
 
4147
3       p
 
4148
DROP TABLE t1;
 
4149
CREATE TABLE t1 (a int, b int);
 
4150
INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
 
4151
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
 
4152
a
 
4153
1
 
4154
2
 
4155
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
 
4156
a
 
4157
SELECT a FROM t1 t0
 
4158
WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
 
4159
a
 
4160
1
 
4161
2
 
4162
SET @@sql_mode='ansi';
 
4163
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
 
4164
ERROR HY000: Invalid use of group function
 
4165
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
 
4166
ERROR HY000: Invalid use of group function
 
4167
SELECT a FROM t1 t0
 
4168
WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
 
4169
ERROR HY000: Invalid use of group function
 
4170
SET @@sql_mode=default;
 
4171
DROP TABLE t1;
 
4172
CREATE TABLE t1 (a INT);
 
4173
INSERT INTO t1 values (1),(1),(1),(1);
 
4174
CREATE TABLE t2 (x INT);
 
4175
INSERT INTO t1 values (1000),(1001),(1002);
 
4176
SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1;
 
4177
ERROR HY000: Invalid use of group function
 
4178
SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1;
 
4179
ERROR HY000: Invalid use of group function
 
4180
SELECT COUNT(1) FROM DUAL;
 
4181
COUNT(1)
 
4182
1
 
4183
SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1;
 
4184
ERROR HY000: Invalid use of group function
 
4185
SELECT
 
4186
SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) )
 
4187
FROM t1;
 
4188
ERROR HY000: Invalid use of group function
 
4189
SELECT t1.a as XXA,
 
4190
SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) )
 
4191
FROM t1;
 
4192
ERROR HY000: Invalid use of group function
 
4193
DROP TABLE t1,t2;
 
4194
CREATE TABLE t1 (a int, b int, KEY (a));
 
4195
INSERT INTO t1 VALUES (1,1),(2,1);
 
4196
EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b);
 
4197
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
4198
1       PRIMARY t1      ref     a       a       5       const   1       Using where; Using index
 
4199
2       SUBQUERY        t1      ALL     NULL    NULL    NULL    NULL    2       Using temporary; Using filesort
 
4200
DROP TABLE t1;
 
4201
CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id));
 
4202
INSERT INTO t1 VALUES
 
4203
(3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY');
 
4204
CREATE TABLE t2 (id int NOT NULL, INDEX idx(id));
 
4205
INSERT INTO t2 VALUES (7), (5), (1), (3);
 
4206
SELECT id, st FROM t1
 
4207
WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
 
4208
id      st
 
4209
3       FL
 
4210
1       GA
 
4211
7       FL
 
4212
SELECT id, st FROM t1
 
4213
WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
 
4214
GROUP BY id;
 
4215
id      st
 
4216
1       GA
 
4217
3       FL
 
4218
7       FL
 
4219
SELECT id, st FROM t1
 
4220
WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
 
4221
id      st
 
4222
2       GA
 
4223
4       FL
 
4224
SELECT id, st FROM t1
 
4225
WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
 
4226
GROUP BY id;
 
4227
id      st
 
4228
2       GA
 
4229
4       FL
 
4230
DROP TABLE t1,t2;
 
4231
CREATE TABLE t1 (a int);
 
4232
INSERT INTO t1 VALUES (1), (2);
 
4233
EXPLAIN EXTENDED
 
4234
SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res;
 
4235
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
4236
1       PRIMARY <derived2>      ALL     NULL    NULL    NULL    NULL    2       100.00  
 
4237
2       DERIVED t1      ALL     NULL    NULL    NULL    NULL    2       100.00  Using temporary; Using filesort
 
4238
Warnings:
 
4239
Note    1003    select `res`.`count(*)` AS `count(*)` from (select count(0) AS `count(*)` from `test`.`t1` group by `test`.`t1`.`a`) `res`
 
4240
DROP TABLE t1;
 
4241
CREATE TABLE t1 (
 
4242
a varchar(255) default NULL,
 
4243
b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
 
4244
INDEX idx(a,b)
 
4245
);
 
4246
CREATE TABLE t2 (
 
4247
a varchar(255) default NULL
 
4248
);
 
4249
INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24');
 
4250
INSERT INTO t1 SELECT * FROM t1;
 
4251
INSERT INTO t1 SELECT * FROM t1;
 
4252
INSERT INTO t1 SELECT * FROM t1;
 
4253
INSERT INTO t1 SELECT * FROM t1;
 
4254
INSERT INTO t1 SELECT * FROM t1;
 
4255
INSERT INTO t1 SELECT * FROM t1;
 
4256
INSERT INTO t1 SELECT * FROM t1;
 
4257
INSERT INTO t1 SELECT * FROM t1;
 
4258
INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26');
 
4259
INSERT INTO `t2` VALUES ('abcdefghijk');
 
4260
INSERT INTO `t2` VALUES ('asdf');
 
4261
SET session sort_buffer_size=8192;
 
4262
SELECT (SELECT 1 FROM  t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2;
 
4263
d1
 
4264
1
 
4265
1
 
4266
DROP TABLE t1,t2;
 
4267
CREATE TABLE t1 (a INTEGER, b INTEGER);
 
4268
CREATE TABLE t2 (x INTEGER);
 
4269
INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
 
4270
INSERT INTO t2 VALUES (1), (2);
 
4271
SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
 
4272
ERROR 21000: Subquery returns more than 1 row
 
4273
SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
 
4274
ERROR 21000: Subquery returns more than 1 row
 
4275
SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
 
4276
(SELECT SUM(t1.a)/AVG(t2.x) FROM t2)
 
4277
3.3333
 
4278
DROP TABLE t1,t2;
 
4279
CREATE TABLE t1 (a INT, b INT);
 
4280
INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2);
 
4281
SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1
 
4282
AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a)
 
4283
GROUP BY a1.a;
 
4284
a       COUNT(*)
 
4285
1       3
 
4286
DROP TABLE t1;
 
4287
CREATE TABLE t1 (a INT);
 
4288
CREATE TABLE t2 (a INT);
 
4289
INSERT INTO t1 VALUES (1),(2);
 
4290
INSERT INTO t2 VALUES (1),(2);
 
4291
SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;
 
4292
(SELECT SUM(t1.a) FROM t2 WHERE a=0)
 
4293
NULL
 
4294
SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1;
 
4295
ERROR 21000: Subquery returns more than 1 row
 
4296
SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1;
 
4297
(SELECT SUM(t1.a) FROM t2 WHERE a=1)
 
4298
3
 
4299
DROP TABLE t1,t2;
 
4300
CREATE TABLE t1 (a1 INT, a2 INT);
 
4301
CREATE TABLE t2 (b1 INT, b2 INT);
 
4302
INSERT INTO t1 VALUES (100, 200);
 
4303
INSERT INTO t1 VALUES (101, 201);
 
4304
INSERT INTO t2 VALUES (101, 201);
 
4305
INSERT INTO t2 VALUES (103, 203);
 
4306
SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1;
 
4307
((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL
 
4308
0
 
4309
0
 
4310
DROP TABLE t1, t2;
 
4311
CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5));
 
4312
INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43);
 
4313
SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
 
4314
s1      s2
 
4315
SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
 
4316
s1      s2
 
4317
CREATE INDEX I1 ON t1 (s1);
 
4318
CREATE INDEX I2 ON t1 (s2);
 
4319
SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
 
4320
s1      s2
 
4321
SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
 
4322
s1      s2
 
4323
TRUNCATE t1;
 
4324
INSERT INTO t1 VALUES (0x41,0x41);
 
4325
SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1);
 
4326
s1      s2
 
4327
DROP TABLE t1;
 
4328
CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1));
 
4329
CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2));
 
4330
CREATE TABLE t3 (a3 BINARY(2) default '0');
 
4331
INSERT INTO t1 VALUES (1),(2),(3),(4);
 
4332
INSERT INTO t2 VALUES (1),(2),(3);
 
4333
INSERT INTO t3 VALUES (1),(2),(3);
 
4334
SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2;
 
4335
LEFT(t2.a2, 1)
 
4336
1
 
4337
2
 
4338
3
 
4339
SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1;
 
4340
a1      t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2)
 
4341
1       0
 
4342
2       0
 
4343
3       0
 
4344
4       0
 
4345
DROP TABLE t1,t2,t3;
 
4346
CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3));
 
4347
CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY);
 
4348
CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY);
 
4349
INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40);
 
4350
INSERT INTO t2 VALUES (2), (3), (4), (5);
 
4351
INSERT INTO t3 VALUES (10), (20), (30);
 
4352
SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3;
 
4353
LEFT(t1.a1,1)
 
4354
1
 
4355
2
 
4356
3
 
4357
SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3);
 
4358
a2
 
4359
DROP TABLE t1, t2, t3;
 
4360
SET @save_optimizer_switch=@@optimizer_switch;
 
4361
SET optimizer_switch='semijoin_with_cache=off';
 
4362
SET optimizer_switch='materialization=off';
 
4363
CREATE TABLE t1 (a CHAR(1), b VARCHAR(10));
 
4364
INSERT INTO t1 VALUES ('a', 'aa');
 
4365
INSERT INTO t1 VALUES ('a', 'aaa');
 
4366
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 
4367
a       b
 
4368
CREATE INDEX I1 ON t1 (a);
 
4369
CREATE INDEX I2 ON t1 (b);
 
4370
EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 
4371
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
4372
1       PRIMARY t1      ALL     I2      NULL    NULL    NULL    2       Using where
 
4373
1       PRIMARY t1      ref     I1      I1      2       test.t1.b       2       Using where; Using index; FirstMatch(t1)
 
4374
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
 
4375
a       b
 
4376
CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
 
4377
INSERT INTO t2 SELECT * FROM t1;
 
4378
CREATE INDEX I1 ON t2 (a);
 
4379
CREATE INDEX I2 ON t2 (b);
 
4380
EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 
4381
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
4382
1       PRIMARY t2      ALL     I2      NULL    NULL    NULL    2       Using where
 
4383
1       PRIMARY t2      ref     I1      I1      4       test.t2.b       2       Using where; Using index; FirstMatch(t2)
 
4384
SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
 
4385
a       b
 
4386
EXPLAIN
 
4387
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 
4388
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
4389
1       PRIMARY t1      ALL     I2      NULL    NULL    NULL    2       Using where
 
4390
1       PRIMARY t1      ref     I1      I1      2       test.t1.b       2       Using where; Using index; FirstMatch(t1)
 
4391
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
 
4392
a       b
 
4393
DROP TABLE t1,t2;
 
4394
SET optimizer_switch= @save_optimizer_switch;
 
4395
CREATE TABLE t1(a INT, b INT);
 
4396
INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4);
 
4397
EXPLAIN
 
4398
SELECT a AS out_a, MIN(b) FROM t1
 
4399
WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
 
4400
GROUP BY a;
 
4401
ERROR 42S22: Unknown column 'out_a' in 'where clause'
 
4402
SELECT a AS out_a, MIN(b) FROM t1
 
4403
WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
 
4404
GROUP BY a;
 
4405
ERROR 42S22: Unknown column 'out_a' in 'where clause'
 
4406
EXPLAIN
 
4407
SELECT a AS out_a, MIN(b) FROM t1 t1_outer
 
4408
WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
 
4409
GROUP BY a;
 
4410
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
4411
1       PRIMARY t1_outer        ALL     NULL    NULL    NULL    NULL    4       Using where; Using temporary; Using filesort
 
4412
2       DEPENDENT SUBQUERY      t1      ALL     NULL    NULL    NULL    NULL    4       Using where
 
4413
SELECT a AS out_a, MIN(b) FROM t1 t1_outer
 
4414
WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
 
4415
GROUP BY a;
 
4416
out_a   MIN(b)
 
4417
1       2
 
4418
2       4
 
4419
DROP TABLE t1;
 
4420
CREATE TABLE t1 (a INT);
 
4421
CREATE TABLE t2 (a INT);
 
4422
INSERT INTO t1 VALUES (1),(2);
 
4423
INSERT INTO t2 VALUES (1),(2);
 
4424
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
 
4425
2
 
4426
2
 
4427
2
 
4428
EXPLAIN EXTENDED
 
4429
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
 
4430
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
4431
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where
 
4432
2       DEPENDENT SUBQUERY      t2      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where
 
4433
Warnings:
 
4434
Note    1276    Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
 
4435
Note    1003    select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)))
 
4436
EXPLAIN EXTENDED
 
4437
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
 
4438
(SELECT 1 FROM t2 WHERE t1.a = t2.a));
 
4439
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
4440
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where
 
4441
2       DEPENDENT SUBQUERY      t2      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where
 
4442
3       DEPENDENT UNION t2      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where
 
4443
NULL    UNION RESULT    <union2,3>      ALL     NULL    NULL    NULL    NULL    NULL    NULL    
 
4444
Warnings:
 
4445
Note    1276    Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
 
4446
Note    1276    Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1
 
4447
Note    1003    select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`))))
 
4448
DROP TABLE t1,t2;
 
4449
create table t0(a int);
 
4450
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
 
4451
create table t1(f11 int, f12 int);
 
4452
create table t2(f21 int unsigned not null, f22 int, f23 varchar(10));
 
4453
insert into t1 values(1,1),(2,2), (3, 3);
 
4454
insert into t2
 
4455
select -1 , (@a:=(A.a + 10 * (B.a + 10 * (C.a+10*D.a))))/5000 + 1, @a
 
4456
from t0 A, t0 B, t0 C, t0 D;
 
4457
set session sort_buffer_size= 33*1024;
 
4458
select count(*) from t1 where f12 =
 
4459
(select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1);
 
4460
count(*)
 
4461
3
 
4462
drop table t0,t1,t2;
 
4463
CREATE TABLE t4 (
 
4464
f7 varchar(32) collate utf8_bin NOT NULL default '',
 
4465
f10 varchar(32) collate utf8_bin default NULL,
 
4466
PRIMARY KEY  (f7)
 
4467
);
 
4468
INSERT INTO t4 VALUES(1,1), (2,null);
 
4469
CREATE TABLE t2 (
 
4470
f4 varchar(32) collate utf8_bin NOT NULL default '',
 
4471
f2 varchar(50) collate utf8_bin default NULL,
 
4472
f3 varchar(10) collate utf8_bin default NULL,
 
4473
PRIMARY KEY  (f4),
 
4474
UNIQUE KEY uk1 (f2)
 
4475
);
 
4476
INSERT INTO t2 VALUES(1,1,null), (2,2,null);
 
4477
CREATE TABLE t1 (
 
4478
f8 varchar(32) collate utf8_bin NOT NULL default '',
 
4479
f1 varchar(10) collate utf8_bin default NULL,
 
4480
f9 varchar(32) collate utf8_bin default NULL,
 
4481
PRIMARY KEY  (f8)
 
4482
);
 
4483
INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2);
 
4484
CREATE TABLE t3 (
 
4485
f6 varchar(32) collate utf8_bin NOT NULL default '',
 
4486
f5 varchar(50) collate utf8_bin default NULL,
 
4487
PRIMARY KEY (f6)
 
4488
);
 
4489
INSERT INTO t3 VALUES (1,null), (2,null);
 
4490
SELECT
 
4491
IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4,
 
4492
IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3,
 
4493
SUM(
 
4494
IF(
 
4495
(SELECT VPC.f2
 
4496
FROM t2 VPC, t4 a2, t2 a3
 
4497
WHERE
 
4498
VPC.f4 = a2.f10 AND a3.f2 = a4
 
4499
LIMIT 1) IS NULL,
 
4500
0,
 
4501
t3.f5
 
4502
)
 
4503
) AS a6
 
4504
FROM
 
4505
t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4
 
4506
GROUP BY a4;
 
4507
a4      f3      a6
 
4508
1       NULL    NULL
 
4509
2       NULL    NULL
 
4510
DROP TABLE t1, t2, t3, t4;
 
4511
create table t1 (a float(5,4) zerofill);
 
4512
create table t2 (a float(5,4),b float(2,0));
 
4513
select t1.a from t1 where
 
4514
t1.a= (select b from t2 limit 1) and not
 
4515
t1.a= (select a from t2 limit 1) ;
 
4516
a
 
4517
drop table t1, t2;
 
4518
CREATE TABLE t1 (a INT);
 
4519
INSERT INTO t1 VALUES (1),(2);
 
4520
SET @save_join_cache_level=@@join_cache_level;
 
4521
SET join_cache_level=0;
 
4522
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 GROUP BY a);
 
4523
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
4524
1       PRIMARY <subquery2>     const   distinct_key    distinct_key    4       const   1       100.00  
 
4525
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       100.00  
 
4526
2       MATERIALIZED    t1      ALL     NULL    NULL    NULL    NULL    2       100.00  Using temporary
 
4527
Warnings:
 
4528
Note    1003    select 1 AS `1` from  <materialize> (select min(`test`.`t1`.`a`) from `test`.`t1` group by `test`.`t1`.`a`) join `test`.`t1` where (`<subquery2>`.`min(a)` = 1)
 
4529
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 WHERE a > 3 GROUP BY a);
 
4530
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
4531
1       PRIMARY <subquery2>     const   distinct_key    distinct_key    4       const   1       100.00  
 
4532
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       100.00  
 
4533
2       MATERIALIZED    t1      ALL     NULL    NULL    NULL    NULL    2       100.00  Using where; Using temporary
 
4534
Warnings:
 
4535
Note    1003    select 1 AS `1` from  <materialize> (select min(`test`.`t1`.`a`) from `test`.`t1` where (`test`.`t1`.`a` > 3) group by `test`.`t1`.`a`) join `test`.`t1` where (`<subquery2>`.`min(a)` = 1)
 
4536
SET join_cache_level=@save_join_cache_level;
 
4537
DROP TABLE t1;
 
4538
#
 
4539
# Bug#45061: Incorrectly market field caused wrong result.
 
4540
#
 
4541
CREATE TABLE `C` (
 
4542
`int_nokey` int(11) NOT NULL,
 
4543
`int_key` int(11) NOT NULL,
 
4544
KEY `int_key` (`int_key`)
 
4545
);
 
4546
INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4),
 
4547
(1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7),
 
4548
(5,2), (1,8), (7,0), (0,9), (9,5);
 
4549
SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
 
4550
int_nokey       int_key
 
4551
9       9
 
4552
0       0
 
4553
5       5
 
4554
0       0
 
4555
EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
 
4556
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
4557
1       PRIMARY C       ALL     NULL    NULL    NULL    NULL    20      100.00  Using where
 
4558
DROP TABLE C;
 
4559
# End of test for bug#45061.
 
4560
#
 
4561
# Bug #46749: Segfault in add_key_fields() with outer subquery level 
 
4562
#   field references
 
4563
#
 
4564
CREATE TABLE t1 (
 
4565
a int,
 
4566
b int,
 
4567
UNIQUE (a), KEY (b)
 
4568
);
 
4569
INSERT INTO t1 VALUES (1,1), (2,1);
 
4570
CREATE TABLE st1 like t1;
 
4571
INSERT INTO st1 VALUES (1,1), (2,1);
 
4572
CREATE TABLE st2 like t1;
 
4573
INSERT INTO st2 VALUES (1,1), (2,1);
 
4574
EXPLAIN
 
4575
SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
 
4576
FROM t1 
 
4577
WHERE a = 230;
 
4578
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
4579
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
4580
2       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
4581
SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
 
4582
FROM t1 
 
4583
WHERE a = 230;
 
4584
MAX(b)  (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
 
4585
NULL    NULL
 
4586
DROP TABLE t1, st1, st2;
 
4587
#
 
4588
# Bug #48709: Assertion failed in sql_select.cc:11782: 
 
4589
#   int join_read_key(JOIN_TAB*)
 
4590
#
 
4591
CREATE TABLE t1 (pk int PRIMARY KEY, int_key int);
 
4592
INSERT INTO t1 VALUES (10,1), (14,1);
 
4593
CREATE TABLE t2 (pk int PRIMARY KEY, int_key int);
 
4594
INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3);
 
4595
# should have eq_ref for t1
 
4596
EXPLAIN
 
4597
SELECT * FROM t2 outr
 
4598
WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)  
 
4599
ORDER BY outr.pk;
 
4600
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
4601
x       x       outr    ALL     x       x       x       x       x       x
 
4602
x       x       t1      eq_ref  x       x       x       x       x       x
 
4603
x       x       t2      index   x       x       x       x       x       x
 
4604
# should not crash on debug binaries
 
4605
SELECT * FROM t2 outr
 
4606
WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)  
 
4607
ORDER BY outr.pk;
 
4608
pk      int_key
 
4609
3       3
 
4610
7       3
 
4611
DROP TABLE t1,t2;
 
4612
#
 
4613
# Bug#12329653 
 
4614
# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
 
4615
#
 
4616
CREATE TABLE t1(a1 int);
 
4617
INSERT INTO t1 VALUES (1),(2);
 
4618
SELECT @@session.sql_mode INTO @old_sql_mode;
 
4619
SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
 
4620
EXPLAIN EXTENDED
 
4621
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
 
4622
id      select_type     table   type    possible_keys   key     key_len ref     rows    filtered        Extra
 
4623
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       100.00  
 
4624
2       SUBQUERY        t1      ALL     NULL    NULL    NULL    NULL    2       100.00  
 
4625
Warnings:
 
4626
Note    1003    select 1 AS `1` from `test`.`t1` where 1
 
4627
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
 
4628
1
 
4629
1
 
4630
1
 
4631
PREPARE stmt FROM 
 
4632
'SELECT 1 UNION ALL 
 
4633
SELECT 1 FROM t1
 
4634
ORDER BY
 
4635
(SELECT 1 FROM t1 AS t1_0  
 
4636
  WHERE 1 < SOME (SELECT a1 FROM t1)
 
4637
)' ;
 
4638
EXECUTE stmt ;
 
4639
ERROR 21000: Subquery returns more than 1 row
 
4640
EXECUTE stmt ;
 
4641
ERROR 21000: Subquery returns more than 1 row
 
4642
SET SESSION sql_mode=@old_sql_mode;
 
4643
DEALLOCATE PREPARE stmt;
 
4644
DROP TABLE t1;
 
4645
#
 
4646
# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
 
4647
#
 
4648
CREATE TABLE t1(a1 int);
 
4649
INSERT INTO t1 VALUES (1),(2);
 
4650
CREATE TABLE t2(a1 int);
 
4651
INSERT INTO t2 VALUES (3);
 
4652
SELECT @@session.sql_mode INTO @old_sql_mode;
 
4653
SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
 
4654
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
 
4655
1
 
4656
1
 
4657
1
 
4658
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
 
4659
1
 
4660
1
 
4661
1
 
4662
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
 
4663
1
 
4664
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
 
4665
1
 
4666
1
 
4667
1
 
4668
SET SESSION sql_mode=@old_sql_mode;
 
4669
DROP TABLE t1, t2;
 
4670
#
 
4671
# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
 
4672
#
 
4673
create table t2(i int);
 
4674
insert into t2 values(0);
 
4675
SELECT @@session.sql_mode INTO @old_sql_mode;
 
4676
SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
 
4677
CREATE VIEW v1 AS  
 
4678
SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
 
4679
;
 
4680
CREATE TABLE t1 (
 
4681
pk int NOT NULL,
 
4682
col_varchar_key varchar(1) DEFAULT NULL,
 
4683
PRIMARY KEY (pk),
 
4684
KEY col_varchar_key (col_varchar_key)
 
4685
);
 
4686
SELECT t1.pk
 
4687
FROM t1
 
4688
WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
 
4689
;
 
4690
pk
 
4691
SET SESSION sql_mode=@old_sql_mode;
 
4692
drop table t2, t1;
 
4693
drop view v1;
 
4694
End of 5.0 tests.
 
4695
create table t_out (subcase char(3),
 
4696
a1 char(2), b1 char(2), c1 char(2));
 
4697
create table t_in  (a2 char(2), b2 char(2), c2 char(2));
 
4698
insert into t_out values ('A.1','2a', NULL, '2a');
 
4699
insert into t_out values ('A.3', '2a', NULL, '2a');
 
4700
insert into t_out values ('A.4', '2a', NULL, 'xx');
 
4701
insert into t_out values ('B.1', '2a', '2a', '2a');
 
4702
insert into t_out values ('B.2', '2a', '2a', '2a');
 
4703
insert into t_out values ('B.3', '3a', 'xx', '3a');
 
4704
insert into t_out values ('B.4', 'xx', '3a', '3a');
 
4705
insert into t_in values ('1a', '1a', '1a');
 
4706
insert into t_in values ('2a', '2a', '2a');
 
4707
insert into t_in values (NULL, '2a', '2a');
 
4708
insert into t_in values ('3a', NULL, '3a');
 
4709
 
 
4710
Test general IN semantics (not top-level)
 
4711
 
 
4712
case A.1
 
4713
select subcase,
 
4714
(a1, b1, c1)     IN (select * from t_in where a2 = 'no_match') pred_in,
 
4715
(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
 
4716
from t_out where subcase = 'A.1';
 
4717
subcase pred_in pred_not_in
 
4718
A.1     0       1
 
4719
case A.2 - impossible
 
4720
case A.3
 
4721
select subcase,
 
4722
(a1, b1, c1)     IN (select * from t_in) pred_in,
 
4723
(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
 
4724
from t_out where subcase = 'A.3';
 
4725
subcase pred_in pred_not_in
 
4726
A.3     NULL    NULL
 
4727
case A.4
 
4728
select subcase,
 
4729
(a1, b1, c1)     IN (select * from t_in) pred_in,
 
4730
(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
 
4731
from t_out where subcase = 'A.4';
 
4732
subcase pred_in pred_not_in
 
4733
A.4     0       1
 
4734
case B.1
 
4735
select subcase,
 
4736
(a1, b1, c1)     IN (select * from t_in where a2 = 'no_match') pred_in,
 
4737
(a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
 
4738
from t_out where subcase = 'B.1';
 
4739
subcase pred_in pred_not_in
 
4740
B.1     0       1
 
4741
case B.2
 
4742
select subcase,
 
4743
(a1, b1, c1)     IN (select * from t_in) pred_in,
 
4744
(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
 
4745
from t_out where subcase = 'B.2';
 
4746
subcase pred_in pred_not_in
 
4747
B.2     1       0
 
4748
case B.3
 
4749
select subcase,
 
4750
(a1, b1, c1)     IN (select * from t_in) pred_in,
 
4751
(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
 
4752
from t_out where subcase = 'B.3';
 
4753
subcase pred_in pred_not_in
 
4754
B.3     NULL    NULL
 
4755
case B.4
 
4756
select subcase,
 
4757
(a1, b1, c1)     IN (select * from t_in) pred_in,
 
4758
(a1, b1, c1) NOT IN (select * from t_in) pred_not_in
 
4759
from t_out where subcase = 'B.4';
 
4760
subcase pred_in pred_not_in
 
4761
B.4     0       1
 
4762
 
 
4763
Test IN as top-level predicate, and
 
4764
as non-top level for cases A.3, B.3 (the only cases with NULL result).
 
4765
 
 
4766
case A.1
 
4767
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
 
4768
where subcase = 'A.1' and
 
4769
(a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
 
4770
pred_in
 
4771
F
 
4772
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
 
4773
where subcase = 'A.1' and
 
4774
(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
 
4775
pred_not_in
 
4776
T
 
4777
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
 
4778
where subcase = 'A.1' and
 
4779
NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
 
4780
not_pred_in
 
4781
T
 
4782
case A.3
 
4783
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
 
4784
where subcase = 'A.3' and
 
4785
(a1, b1, c1) IN (select * from t_in);
 
4786
pred_in
 
4787
F
 
4788
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
 
4789
where subcase = 'A.3' and
 
4790
(a1, b1, c1) NOT IN (select * from t_in);
 
4791
pred_not_in
 
4792
F
 
4793
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
 
4794
where subcase = 'A.3' and
 
4795
NOT((a1, b1, c1) IN (select * from t_in));
 
4796
not_pred_in
 
4797
F
 
4798
select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
 
4799
where subcase = 'A.3' and
 
4800
((a1, b1, c1) IN (select * from t_in)) is NULL and
 
4801
((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
 
4802
pred_in
 
4803
N
 
4804
case A.4
 
4805
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
 
4806
where subcase = 'A.4' and
 
4807
(a1, b1, c1) IN (select * from t_in);
 
4808
pred_in
 
4809
F
 
4810
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
 
4811
where subcase = 'A.4' and
 
4812
(a1, b1, c1) NOT IN (select * from t_in);
 
4813
pred_not_in
 
4814
T
 
4815
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
 
4816
where subcase = 'A.4' and
 
4817
NOT((a1, b1, c1) IN (select * from t_in));
 
4818
not_pred_in
 
4819
T
 
4820
case B.1
 
4821
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
 
4822
where subcase = 'B.1' and
 
4823
(a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
 
4824
pred_in
 
4825
F
 
4826
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
 
4827
where subcase = 'B.1' and
 
4828
(a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
 
4829
pred_not_in
 
4830
T
 
4831
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
 
4832
where subcase = 'B.1' and
 
4833
NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
 
4834
not_pred_in
 
4835
T
 
4836
case B.2
 
4837
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
 
4838
where subcase = 'B.2' and
 
4839
(a1, b1, c1) IN (select * from t_in);
 
4840
pred_in
 
4841
T
 
4842
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
 
4843
where subcase = 'B.2' and
 
4844
(a1, b1, c1) NOT IN (select * from t_in);
 
4845
pred_not_in
 
4846
F
 
4847
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
 
4848
where subcase = 'B.2' and
 
4849
NOT((a1, b1, c1) IN (select * from t_in));
 
4850
not_pred_in
 
4851
F
 
4852
case B.3
 
4853
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
 
4854
where subcase = 'B.3' and
 
4855
(a1, b1, c1) IN (select * from t_in);
 
4856
pred_in
 
4857
F
 
4858
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
 
4859
where subcase = 'B.3' and
 
4860
(a1, b1, c1) NOT IN (select * from t_in);
 
4861
pred_not_in
 
4862
F
 
4863
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
 
4864
where subcase = 'B.3' and
 
4865
NOT((a1, b1, c1) IN (select * from t_in));
 
4866
not_pred_in
 
4867
F
 
4868
select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
 
4869
where subcase = 'B.3' and
 
4870
((a1, b1, c1) IN (select * from t_in)) is NULL and
 
4871
((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
 
4872
pred_in
 
4873
N
 
4874
case B.4
 
4875
select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
 
4876
where subcase = 'B.4' and
 
4877
(a1, b1, c1) IN (select * from t_in);
 
4878
pred_in
 
4879
F
 
4880
select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
 
4881
where subcase = 'B.4' and
 
4882
(a1, b1, c1) NOT IN (select * from t_in);
 
4883
pred_not_in
 
4884
T
 
4885
select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
 
4886
where subcase = 'B.4' and
 
4887
NOT((a1, b1, c1) IN (select * from t_in));
 
4888
not_pred_in
 
4889
T
 
4890
drop table t_out;
 
4891
drop table t_in;
 
4892
CREATE TABLE t1 (a INT, b INT);
 
4893
INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
 
4894
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
 
4895
a
 
4896
1
 
4897
2
 
4898
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
 
4899
a
 
4900
SELECT a FROM t1 t0
 
4901
WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
 
4902
a
 
4903
1
 
4904
2
 
4905
SET @@sql_mode='ansi';
 
4906
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
 
4907
ERROR HY000: Invalid use of group function
 
4908
SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
 
4909
ERROR HY000: Invalid use of group function
 
4910
SELECT a FROM t1 t0
 
4911
WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
 
4912
ERROR HY000: Invalid use of group function
 
4913
SET @@sql_mode=default;
 
4914
DROP TABLE t1;
 
4915
CREATE TABLE t1 (s1 CHAR(1));
 
4916
INSERT INTO t1 VALUES ('a');
 
4917
SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1);
 
4918
s1
 
4919
a
 
4920
DROP TABLE t1;
 
4921
CREATE TABLE t1(c INT, KEY(c));
 
4922
CREATE TABLE t2(a INT, b INT);
 
4923
INSERT INTO t2 VALUES (1, 10), (2, NULL);
 
4924
INSERT INTO t1 VALUES (1), (3);
 
4925
SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10);
 
4926
a       b
 
4927
DROP TABLE t1,t2;
 
4928
CREATE TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a));
 
4929
INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20);
 
4930
CREATE TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a));
 
4931
INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100);
 
4932
SELECT * FROM t1
 
4933
WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a ORDER BY b);
 
4934
pk      a
 
4935
1       10
 
4936
DROP TABLE t1,t2;
 
4937
CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b));
 
4938
INSERT INTO t1 VALUES (1,NULL), (9,NULL);
 
4939
CREATE TABLE t2 (
 
4940
a INT,
 
4941
b INT,
 
4942
c INT,
 
4943
d INT,
 
4944
PRIMARY KEY (a),
 
4945
UNIQUE KEY b (b,c,d),
 
4946
KEY b_2 (b),
 
4947
KEY c (c),
 
4948
KEY d (d)
 
4949
);
 
4950
INSERT INTO t2 VALUES
 
4951
(43, 2, 11 ,30),
 
4952
(44, 2, 12 ,30),
 
4953
(45, 1, 1  ,10000),
 
4954
(46, 1, 2  ,10000),
 
4955
(556,1, 32 ,10000);
 
4956
CREATE TABLE t3 (
 
4957
a INT,
 
4958
b INT,
 
4959
c INT,
 
4960
PRIMARY KEY (a),
 
4961
UNIQUE KEY b (b,c),
 
4962
KEY c (c),
 
4963
KEY b_2 (b)
 
4964
);
 
4965
INSERT INTO t3 VALUES (1,1,1), (2,32,1);
 
4966
explain
 
4967
SELECT t1.a, (SELECT 1 FROM t2 WHERE t2.b=t3.c AND t2.c=t1.a ORDER BY t2.d LIMIT 1) AS incorrect FROM t1, t3 WHERE t3.b=t1.a;
 
4968
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
4969
1       PRIMARY t1      index   PRIMARY PRIMARY 4       NULL    2       Using index
 
4970
1       PRIMARY t3      ref     b,b_2   b       5       test.t1.a       1       Using index
 
4971
2       DEPENDENT SUBQUERY      t2      ref     b,b_2,c b       10      test.t3.c,test.t1.a     1       Using where; Using index; Using filesort
 
4972
SELECT t1.a, (SELECT 1 FROM t2 WHERE t2.b=t3.c AND t2.c=t1.a ORDER BY t2.d LIMIT 1) AS incorrect FROM t1, t3 WHERE t3.b=t1.a;
 
4973
a       incorrect
 
4974
1       1
 
4975
DROP TABLE t1,t2,t3;
 
4976
CREATE TABLE t1 (id int);
 
4977
CREATE TABLE t2 (id int, c int);
 
4978
INSERT INTO t1 (id) VALUES (1);
 
4979
INSERT INTO t2 (id) VALUES (1);
 
4980
INSERT INTO t1 (id) VALUES (1);
 
4981
INSERT INTO t2 (id) VALUES (1);
 
4982
CREATE VIEW v1 AS
 
4983
SELECT t2.c AS c FROM t1, t2
 
4984
WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
 
4985
UPDATE v1 SET c=1;
 
4986
CREATE VIEW v2 (a,b) AS
 
4987
SELECT t2.id, t2.c AS c FROM t1, t2
 
4988
WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
 
4989
INSERT INTO v2(a,b) VALUES (2,2);
 
4990
ERROR HY000: CHECK OPTION failed 'test.v2'
 
4991
SELECT * FROM v1;
 
4992
c
 
4993
1
 
4994
1
 
4995
1
 
4996
1
 
4997
CREATE VIEW v3 AS
 
4998
SELECT t2.c AS c FROM t2
 
4999
WHERE 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
 
5000
DELETE FROM v3;
 
5001
DROP VIEW v1,v2,v3;
 
5002
DROP TABLE t1,t2;
 
5003
#
 
5004
# BUG#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
 
5005
#
 
5006
create table t1(id integer primary key, g integer, v integer, s char(1));
 
5007
create table t2(id integer primary key, g integer, v integer, s char(1));
 
5008
insert into t1 values
 
5009
(10, 10, 10,   'l'),
 
5010
(20, 20, 20,   'l'),
 
5011
(40, 40, 40,   'l'),
 
5012
(41, 40, null, 'l'),
 
5013
(50, 50, 50,   'l'),
 
5014
(51, 50, null, 'l'),
 
5015
(60, 60, 60,   'l'),
 
5016
(61, 60, null, 'l'),
 
5017
(70, 70, 70,   'l'),
 
5018
(90, 90, null, 'l');
 
5019
insert into t2 values
 
5020
(10, 10, 10,   'r'),
 
5021
(30, 30, 30,   'r'),
 
5022
(50, 50, 50,   'r'),
 
5023
(60, 60, 60,   'r'),
 
5024
(61, 60, null, 'r'),
 
5025
(70, 70, 70,   'r'),
 
5026
(71, 70, null, 'r'),
 
5027
(80, 80, 80,   'r'),
 
5028
(81, 80, null, 'r'),
 
5029
(100,100,null, 'r');
 
5030
select *
 
5031
from t1
 
5032
where v in(select v
 
5033
from t2
 
5034
where t1.g=t2.g) is unknown;
 
5035
id      g       v       s
 
5036
51      50      NULL    l
 
5037
61      60      NULL    l
 
5038
drop table t1, t2;
 
5039
#
 
5040
# Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
 
5041
#
 
5042
create table t1(id integer primary key, g integer, v integer, s char(1));
 
5043
create table t2(id integer primary key, g integer, v integer, s char(1));
 
5044
insert into t1 values
 
5045
(10, 10, 10,   'l'),
 
5046
(20, 20, 20,   'l'),
 
5047
(40, 40, 40,   'l'),
 
5048
(41, 40, null, 'l'),
 
5049
(50, 50, 50,   'l'),
 
5050
(51, 50, null, 'l'),
 
5051
(60, 60, 60,   'l'),
 
5052
(61, 60, null, 'l'),
 
5053
(70, 70, 70,   'l'),
 
5054
(90, 90, null, 'l');
 
5055
insert into t2 values
 
5056
(10, 10, 10,   'r'),
 
5057
(30, 30, 30,   'r'),
 
5058
(50, 50, 50,   'r'),
 
5059
(60, 60, 60,   'r'),
 
5060
(61, 60, null, 'r'),
 
5061
(70, 70, 70,   'r'),
 
5062
(71, 70, null, 'r'),
 
5063
(80, 80, 80,   'r'),
 
5064
(81, 80, null, 'r'),
 
5065
(100,100,null, 'r');
 
5066
select *
 
5067
from t1
 
5068
where v in(select v
 
5069
from t2
 
5070
where t1.g=t2.g) is unknown;
 
5071
id      g       v       s
 
5072
51      50      NULL    l
 
5073
61      60      NULL    l
 
5074
drop table t1, t2;
 
5075
#
 
5076
# Bug#33204: INTO is allowed in subselect, causing inconsistent results
 
5077
#
 
5078
CREATE TABLE t1( a INT );
 
5079
INSERT INTO t1 VALUES (1),(2);
 
5080
CREATE TABLE t2( a INT, b INT );
 
5081
SELECT * 
 
5082
FROM (SELECT a INTO @var FROM t1 WHERE a = 2) t1a;
 
5083
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @var FROM t1 WHERE a = 2) t1a' at line 2
 
5084
SELECT * 
 
5085
FROM (SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a;
 
5086
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a' at line 2
 
5087
SELECT * 
 
5088
FROM (SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a;
 
5089
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a' at line 2
 
5090
SELECT * FROM ( 
 
5091
SELECT 1 a 
 
5092
UNION 
 
5093
SELECT a INTO @var FROM t1 WHERE a = 2 
 
5094
) t1a;
 
5095
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @var FROM t1 WHERE a = 2 
 
5096
) t1a' at line 4
 
5097
SELECT * FROM ( 
 
5098
SELECT 1 a 
 
5099
UNION 
 
5100
SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2 
 
5101
) t1a;
 
5102
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' FROM t1 WHERE a = 2 
 
5103
) t1a' at line 4
 
5104
SELECT * FROM ( 
 
5105
SELECT 1 a 
 
5106
UNION 
 
5107
SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2 
 
5108
) t1a;
 
5109
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' FROM t1 WHERE a = 2 
 
5110
) t1a' at line 4
 
5111
SELECT * FROM (SELECT a FROM t1 WHERE a = 2) t1a;
 
5112
a
 
5113
2
 
5114
SELECT * FROM ( 
 
5115
SELECT a FROM t1 WHERE a = 2 
 
5116
UNION 
 
5117
SELECT a FROM t1 WHERE a = 2 
 
5118
) t1a;
 
5119
a
 
5120
2
 
5121
SELECT * FROM ( 
 
5122
SELECT 1 a 
 
5123
UNION 
 
5124
SELECT a FROM t1 WHERE a = 2 
 
5125
UNION 
 
5126
SELECT a FROM t1 WHERE a = 2 
 
5127
) t1a;
 
5128
a
 
5129
1
 
5130
2
 
5131
SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a);
 
5132
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1
 
5133
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
 
5134
a
 
5135
1
 
5136
SELECT * FROM (SELECT 1 UNION SELECT 1) t1a;
 
5137
1
 
5138
1
 
5139
SELECT * FROM ((SELECT 1 a INTO @a)) t1a;
 
5140
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)) t1a' at line 1
 
5141
SELECT * FROM ((SELECT 1 a INTO OUTFILE 'file' )) t1a;
 
5142
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )) t1a' at line 1
 
5143
SELECT * FROM ((SELECT 1 a INTO DUMPFILE 'file' )) t1a;
 
5144
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )) t1a' at line 1
 
5145
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO @a)) t1a;
 
5146
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)) t1a' at line 1
 
5147
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO DUMPFILE 'file' )) t1a;
 
5148
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )) t1a' at line 1
 
5149
SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO OUTFILE 'file' )) t1a;
 
5150
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )) t1a' at line 1
 
5151
SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO @a))) t1a;
 
5152
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a))) t1a' at line 1
 
5153
SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO DUMPFILE 'file' ))) t1a;
 
5154
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' ))) t1a' at line 1
 
5155
SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO OUTFILE 'file' ))) t1a;
 
5156
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' ))) t1a' at line 1
 
5157
SELECT * FROM (SELECT 1 a ORDER BY a) t1a;
 
5158
a
 
5159
1
 
5160
SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a) t1a;
 
5161
a
 
5162
1
 
5163
SELECT * FROM (SELECT 1 a UNION SELECT 1 a LIMIT 1) t1a;
 
5164
a
 
5165
1
 
5166
SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a;
 
5167
a
 
5168
1
 
5169
SELECT * FROM t1 JOIN  (SELECT 1 UNION SELECT 1) alias ON 1;
 
5170
a       1
 
5171
1       1
 
5172
2       1
 
5173
SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
 
5174
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) ON 1' at line 1
 
5175
SELECT * FROM t1 JOIN  (t1 t1a UNION SELECT 1)  ON 1;
 
5176
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')  ON 1' at line 1
 
5177
SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
 
5178
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) ON 1' at line 1
 
5179
SELECT * FROM t1 JOIN  (t1 t1a)  t1a ON 1;
 
5180
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a ON 1' at line 1
 
5181
SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
 
5182
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a ON 1' at line 1
 
5183
SELECT * FROM t1 JOIN  (t1 t1a)  ON 1;
 
5184
a       a
 
5185
1       1
 
5186
2       1
 
5187
1       2
 
5188
2       2
 
5189
SELECT * FROM t1 JOIN ((t1 t1a)) ON 1;
 
5190
a       a
 
5191
1       1
 
5192
2       1
 
5193
1       2
 
5194
2       2
 
5195
SELECT * FROM (t1 t1a);
 
5196
a
 
5197
1
 
5198
2
 
5199
SELECT * FROM ((t1 t1a));
 
5200
a
 
5201
1
 
5202
2
 
5203
SELECT * FROM t1 JOIN  (SELECT 1 t1a) alias ON 1;
 
5204
a       t1a
 
5205
1       1
 
5206
2       1
 
5207
SELECT * FROM t1 JOIN ((SELECT 1 t1a)) alias ON 1;
 
5208
a       t1a
 
5209
1       1
 
5210
2       1
 
5211
SELECT * FROM t1 JOIN  (SELECT 1 a)  a ON 1;
 
5212
a       a
 
5213
1       1
 
5214
2       1
 
5215
SELECT * FROM t1 JOIN ((SELECT 1 a)) a ON 1;
 
5216
a       a
 
5217
1       1
 
5218
2       1
 
5219
SELECT * FROM (t1 JOIN (SELECT 1) t1a1 ON 1) t1a2;
 
5220
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a2' at line 1
 
5221
SELECT * FROM t1 WHERE a = ALL ( SELECT 1 );
 
5222
a
 
5223
1
 
5224
SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 );
 
5225
a
 
5226
1
 
5227
SELECT * FROM t1 WHERE a = ANY ( SELECT 3 UNION SELECT 1 );
 
5228
a
 
5229
1
 
5230
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO @a);
 
5231
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)' at line 1
 
5232
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
 
5233
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
 
5234
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
 
5235
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
 
5236
SELECT * FROM t1 WHERE a = ( SELECT 1 );
 
5237
a
 
5238
1
 
5239
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 );
 
5240
a
 
5241
1
 
5242
SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a);
 
5243
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)' at line 1
 
5244
SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE 'file' );
 
5245
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
 
5246
SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE 'file' );
 
5247
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
 
5248
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO @a);
 
5249
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)' at line 1
 
5250
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
 
5251
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
 
5252
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
 
5253
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
 
5254
SELECT ( SELECT 1 INTO @v );
 
5255
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
 
5256
SELECT ( SELECT 1 INTO OUTFILE 'file' );
 
5257
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
 
5258
SELECT ( SELECT 1 INTO DUMPFILE 'file' );
 
5259
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
 
5260
SELECT ( SELECT 1 UNION SELECT 1 INTO @v );
 
5261
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
 
5262
SELECT ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
 
5263
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
 
5264
SELECT ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
 
5265
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
 
5266
SELECT ( SELECT a FROM t1 WHERE a = 1 ), a FROM t1;
 
5267
( SELECT a FROM t1 WHERE a = 1 )        a
 
5268
1       1
 
5269
1       2
 
5270
SELECT ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ), a FROM t1;
 
5271
( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ) a
 
5272
1       1
 
5273
1       2
 
5274
SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2);
 
5275
a       b
 
5276
SELECT 1 UNION ( SELECT 1 UNION SELECT 1 );
 
5277
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 )' at line 1
 
5278
( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
 
5279
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) UNION SELECT 1' at line 1
 
5280
SELECT ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
 
5281
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) )' at line 1
 
5282
SELECT ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
 
5283
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1' at line 1
 
5284
SELECT ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
 
5285
( SELECT 1 UNION SELECT 1 UNION SELECT 1 )
 
5286
1
 
5287
SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
 
5288
((SELECT 1 UNION SELECT 1 UNION SELECT 1))
 
5289
1
 
5290
SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
 
5291
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) )' at line 1
 
5292
SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
 
5293
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') UNION SELECT 1 )' at line 1
 
5294
SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
 
5295
1
 
5296
1
 
5297
SELECT * FROM t1 WHERE a =     ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
 
5298
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) )' at line 1
 
5299
SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
 
5300
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) )' at line 1
 
5301
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
 
5302
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) )' at line 1
 
5303
SELECT * FROM t1 WHERE a IN    ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
 
5304
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 ) )' at line 1
 
5305
SELECT * FROM t1 WHERE a =     ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
 
5306
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 )' at line 1
 
5307
SELECT * FROM t1 WHERE a = ALL ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
 
5308
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 )  UNION SELECT 1 )' at line 1
 
5309
SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
 
5310
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 )  UNION SELECT 1 )' at line 1
 
5311
SELECT * FROM t1 WHERE a IN    ( ( SELECT 1 UNION SELECT 1 )  UNION SELECT 1 );
 
5312
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 )' at line 1
 
5313
SELECT * FROM t1 WHERE a =     ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
 
5314
a
 
5315
1
 
5316
SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
 
5317
a
 
5318
1
 
5319
SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
 
5320
a
 
5321
1
 
5322
SELECT * FROM t1 WHERE a IN    ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
 
5323
a
 
5324
1
 
5325
SELECT * FROM t1 WHERE EXISTS ( SELECT 1 UNION SELECT 1 INTO @v );
 
5326
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
 
5327
SELECT EXISTS(SELECT 1+1);
 
5328
EXISTS(SELECT 1+1)
 
5329
1
 
5330
SELECT EXISTS(SELECT 1+1 INTO @test);
 
5331
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @test)' at line 1
 
5332
SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 INTO @v );
 
5333
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
 
5334
SELECT * FROM t1 WHERE EXISTS ( SELECT 1 INTO @v );
 
5335
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
 
5336
SELECT * FROM t1 WHERE a IN ( SELECT 1 INTO @v );
 
5337
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
 
5338
DROP TABLE t1, t2;
 
5339
CREATE TABLE t1 (a ENUM('rainbow'));
 
5340
INSERT INTO t1 VALUES (),(),(),(),();
 
5341
SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
 
5342
1
 
5343
1
 
5344
DROP TABLE t1;
 
5345
CREATE TABLE t1 (a LONGBLOB);
 
5346
INSERT INTO t1 SET a = 'aaaa';
 
5347
INSERT INTO t1 SET a = 'aaaa';
 
5348
SELECT 1 FROM t1 GROUP BY
 
5349
(SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
 
5350
1
 
5351
1
 
5352
DROP TABLE t1;
 
5353
#
 
5354
# Bug #49512 : subquery with aggregate function crash 
 
5355
#   subselect_single_select_engine::exec()
 
5356
CREATE TABLE t1(a INT);
 
5357
INSERT INTO t1 VALUES();
 
5358
# should not crash
 
5359
SELECT 1 FROM t1 WHERE a <> SOME
 
5360
(
 
5361
SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d
 
5362
FROM t1,t1 a
 
5363
);
 
5364
1
 
5365
DROP TABLE t1;
 
5366
#
 
5367
# Bug #45989 take 2 : memory leak after explain encounters an 
 
5368
# error in the query
 
5369
#
 
5370
CREATE TABLE t1(a LONGTEXT);
 
5371
INSERT INTO t1 VALUES (repeat('a',@@global.max_allowed_packet));
 
5372
INSERT INTO t1 VALUES (repeat('b',@@global.max_allowed_packet));
 
5373
EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1,
 
5374
(SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1  
 
5375
WHERE t1.a = d1.a;
 
5376
ERROR 42S22: Unknown column 'd1.a' in 'where clause'
 
5377
DROP TABLE t1;
 
5378
Set up test tables.
 
5379
CREATE TABLE t1 (
 
5380
t1_id INT UNSIGNED,
 
5381
PRIMARY KEY(t1_id)
 
5382
) Engine=MyISAM;
 
5383
INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5);
 
5384
CREATE TABLE t2 SELECT * FROM t1;
 
5385
CREATE TABLE t3 (
 
5386
t3_id INT UNSIGNED AUTO_INCREMENT,
 
5387
t1_id INT UNSIGNED,
 
5388
amount DECIMAL(16,2),
 
5389
PRIMARY KEY(t3_id),
 
5390
KEY(t1_id)
 
5391
) Engine=MyISAM;
 
5392
INSERT INTO t3 (t1_id, t3_id, amount) 
 
5393
VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00);
 
5394
This is the 'inner query' running by itself.
 
5395
Produces correct results.
 
5396
SELECT
 
5397
t1.t1_id,
 
5398
IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
 
5399
FROM
 
5400
t1
 
5401
LEFT JOIN t2 ON t2.t1_id=t1.t1_id
 
5402
GROUP BY
 
5403
t1.t1_id
 
5404
;
 
5405
t1_id   total_amount
 
5406
1       100.00
 
5407
2       200.00
 
5408
3       0.00
 
5409
4       400.00
 
5410
5       0.00
 
5411
SELECT * FROM (the same inner query)
 
5412
Produces correct results.
 
5413
SELECT * FROM (
 
5414
SELECT
 
5415
t1.t1_id,
 
5416
IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
 
5417
FROM
 
5418
t1
 
5419
LEFT JOIN t2 ON t2.t1_id=t1.t1_id
 
5420
GROUP BY
 
5421
t1.t1_id
 
5422
) AS t;
 
5423
t1_id   total_amount
 
5424
1       100.00
 
5425
2       200.00
 
5426
3       0.00
 
5427
4       400.00
 
5428
5       0.00
 
5429
Now make t2.t1_id part of a key.
 
5430
ALTER TABLE t2 ADD PRIMARY KEY(t1_id);
 
5431
Same inner query by itself.
 
5432
Still correct results.
 
5433
SELECT
 
5434
t1.t1_id,
 
5435
IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
 
5436
FROM
 
5437
t1
 
5438
LEFT JOIN t2 ON t2.t1_id=t1.t1_id
 
5439
GROUP BY
 
5440
t1.t1_id;
 
5441
t1_id   total_amount
 
5442
1       100.00
 
5443
2       200.00
 
5444
3       0.00
 
5445
4       400.00
 
5446
5       0.00
 
5447
SELECT * FROM (the same inner query), now with indexes on the LEFT JOIN
 
5448
SELECT * FROM (
 
5449
SELECT
 
5450
t1.t1_id,
 
5451
IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
 
5452
FROM
 
5453
t1
 
5454
LEFT JOIN t2 ON t2.t1_id=t1.t1_id
 
5455
GROUP BY
 
5456
t1.t1_id
 
5457
) AS t;
 
5458
t1_id   total_amount
 
5459
1       100.00
 
5460
2       200.00
 
5461
3       0.00
 
5462
4       400.00
 
5463
5       0.00
 
5464
DROP TABLE t3;
 
5465
DROP TABLE t2;
 
5466
DROP TABLE t1;
 
5467
#
 
5468
# Bug #52711: Segfault when doing EXPLAIN SELECT with 
 
5469
#  union...order by (select... where...)
 
5470
#
 
5471
CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a));
 
5472
INSERT INTO t1 VALUES (1),(2);
 
5473
CREATE TABLE t2 (b INT);
 
5474
INSERT INTO t2 VALUES (1),(2);
 
5475
# Should not crash
 
5476
EXPLAIN
 
5477
SELECT * FROM t2 UNION SELECT * FROM t2
 
5478
ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
 
5479
# Should not crash
 
5480
SELECT * FROM t2 UNION SELECT * FROM t2
 
5481
ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
 
5482
DROP TABLE t1,t2;
 
5483
#
 
5484
# Bug #58818: Incorrect result for IN/ANY subquery
 
5485
# with HAVING condition 
 
5486
#
 
5487
CREATE TABLE t1(i INT);
 
5488
INSERT INTO t1 VALUES (1), (2), (3);
 
5489
CREATE TABLE t1s(i INT);
 
5490
INSERT INTO t1s VALUES (10), (20), (30);
 
5491
CREATE TABLE t2s(i INT);
 
5492
INSERT INTO t2s VALUES (100), (200), (300);
 
5493
SELECT * FROM t1
 
5494
WHERE t1.i NOT IN
 
5495
(
 
5496
SELECT STRAIGHT_JOIN t2s.i 
 
5497
FROM
 
5498
t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
 
5499
HAVING t2s.i = 999
 
5500
);
 
5501
i
 
5502
1
 
5503
2
 
5504
3
 
5505
SELECT * FROM t1
 
5506
WHERE t1.I IN
 
5507
(
 
5508
SELECT STRAIGHT_JOIN t2s.i 
 
5509
FROM
 
5510
t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
 
5511
HAVING t2s.i = 999
 
5512
) IS UNKNOWN;
 
5513
i
 
5514
SELECT * FROM t1
 
5515
WHERE NOT t1.I = ANY
 
5516
(
 
5517
SELECT STRAIGHT_JOIN t2s.i 
 
5518
FROM
 
5519
t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
 
5520
HAVING t2s.i = 999
 
5521
);
 
5522
i
 
5523
1
 
5524
2
 
5525
3
 
5526
SELECT * FROM t1
 
5527
WHERE t1.i = ANY (
 
5528
SELECT STRAIGHT_JOIN t2s.i 
 
5529
FROM
 
5530
t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
 
5531
HAVING t2s.i = 999
 
5532
) IS UNKNOWN;
 
5533
i
 
5534
DROP TABLE t1,t1s,t2s;
 
5535
# LP BUG#675248 - select->prep_where references on freed memory 
 
5536
CREATE TABLE t1 (a int, b int);
 
5537
insert into t1 values (1,1),(0,0);
 
5538
CREATE TABLE t2 (c int);
 
5539
insert into t2 values (1),(2);
 
5540
prepare stmt1 from "select sum(a),(select sum(c) from t2 where table1.b) as sub
 
5541
from t1 as table1 group by sub";
 
5542
execute stmt1;
 
5543
sum(a)  sub
 
5544
0       NULL
 
5545
1       3
 
5546
deallocate prepare stmt1;
 
5547
prepare stmt1 from "select sum(a),(select sum(c) from t2 having table1.b) as sub
 
5548
from t1 as table1";
 
5549
execute stmt1;
 
5550
sum(a)  sub
 
5551
1       3
 
5552
deallocate prepare stmt1;
 
5553
drop table t1,t2;
 
5554
#
 
5555
# Bug LP#693935/#58727: Assertion failure with 
 
5556
# a single row subquery returning more than one row
 
5557
#
 
5558
create table t1 (a char(1) charset utf8);
 
5559
insert into t1 values ('a'), ('b');
 
5560
create table t2 (a binary(1));
 
5561
insert into t2 values ('x'), ('y');
 
5562
select * from t2 where a=(select a from t1) and a='x';
 
5563
ERROR 21000: Subquery returns more than 1 row
 
5564
drop table t1,t2;
 
5565
End of 5.1 tests
 
5566
#
 
5567
# Bug #11765713 58705:
 
5568
# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
 
5569
# CREATED BY OPT_SUM_QUERY
 
5570
#
 
5571
CREATE TABLE t1(a INT NOT NULL, KEY (a));
 
5572
INSERT INTO t1 VALUES (0), (1);
 
5573
SELECT 1 as foo FROM t1 WHERE a < SOME
 
5574
(SELECT a FROM t1 WHERE a <=>
 
5575
(SELECT a FROM t1)
 
5576
);
 
5577
ERROR 21000: Subquery returns more than 1 row
 
5578
SELECT 1 as foo FROM t1 WHERE a < SOME
 
5579
(SELECT a FROM t1 WHERE a <=>
 
5580
(SELECT a FROM t1 where a is null)
 
5581
);
 
5582
foo
 
5583
DROP TABLE t1;
 
5584
#
 
5585
# Bug #57704: Cleanup code dies with void TABLE::set_keyread(bool): 
 
5586
#             Assertion `file' failed.
 
5587
#
 
5588
CREATE TABLE t1 (a INT);
 
5589
SELECT 1 FROM 
 
5590
(SELECT ROW(
 
5591
(SELECT 1 FROM t1 RIGHT JOIN 
 
5592
(SELECT 1 FROM t1, t1 t2) AS d ON 1),
 
5593
1) FROM t1) AS e;
 
5594
ERROR 21000: Operand should contain 1 column(s)
 
5595
DROP TABLE t1;
 
5596
#
 
5597
# Bug#13721076 CRASH WITH TIME TYPE/TIMESTAMP() AND WARNINGS IN SUBQUERY
 
5598
#
 
5599
CREATE TABLE t1(a TIME NOT NULL);
 
5600
INSERT INTO t1 VALUES ('00:00:32');
 
5601
SELECT 1 FROM t1 WHERE a >
 
5602
(SELECT timestamp(a) AS a FROM t1);
 
5603
1
 
5604
DROP TABLE t1;
 
5605
#
 
5606
# No BUG#, a case brought from 5.2's innodb_mysql_lock.test
 
5607
#
 
5608
create table t1 (i int  not null primary key);
 
5609
insert into t1 values (1),(2),(3),(4),(5);
 
5610
create table t2 (j int not null  primary key);
 
5611
insert into t2 values (1),(2),(3),(4),(5);
 
5612
create table t3 (k int not null primary key);
 
5613
insert into t3 values (1),(2),(3);
 
5614
create view v2 as select t2.j as j from t2 where t2.j in (select t1.i from t1);
 
5615
select * from t3 where k in (select j from v2);
 
5616
k
 
5617
1
 
5618
2
 
5619
3
 
5620
drop table t1,t2,t3;
 
5621
drop view v2;
 
5622
#
 
5623
# Bug#52068: Optimizer generates invalid semijoin materialization plan
 
5624
#
 
5625
drop table if exists ot1, ot2, it1, it2;
 
5626
CREATE TABLE ot1(a INTEGER);
 
5627
INSERT INTO ot1 VALUES(5), (8);
 
5628
CREATE TABLE it2(a INTEGER);
 
5629
INSERT INTO it2 VALUES(9), (5), (1), (8);
 
5630
CREATE TABLE it3(a INTEGER);
 
5631
INSERT INTO it3 VALUES(7), (1), (0), (5), (1), (4);
 
5632
CREATE TABLE ot4(a INTEGER);
 
5633
INSERT INTO ot4 VALUES(1), (3), (5), (7), (9), (7), (3), (1);
 
5634
SELECT * FROM ot1,ot4
 
5635
WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
 
5636
FROM it2,it3);
 
5637
a       a
 
5638
5       1
 
5639
8       1
 
5640
5       5
 
5641
8       5
 
5642
5       7
 
5643
8       7
 
5644
5       7
 
5645
8       7
 
5646
5       1
 
5647
8       1
 
5648
explain SELECT * FROM ot1,ot4
 
5649
WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
 
5650
FROM it2,it3);
 
5651
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
5652
1       PRIMARY ot1     ALL     NULL    NULL    NULL    NULL    2       
 
5653
1       PRIMARY ot4     ALL     NULL    NULL    NULL    NULL    8       Using join buffer (flat, BNL join)
 
5654
1       PRIMARY <subquery2>     eq_ref  distinct_key    distinct_key    8       func,func       1       
 
5655
2       MATERIALIZED    it2     ALL     NULL    NULL    NULL    NULL    4       
 
5656
2       MATERIALIZED    it3     ALL     NULL    NULL    NULL    NULL    6       Using join buffer (flat, BNL join)
 
5657
DROP TABLE IF EXISTS ot1, ot4, it2, it3;
 
5658
#
 
5659
# Bug#729039: NULL keys used to evaluate subquery
 
5660
#
 
5661
CREATE TABLE t1 (a int) ;
 
5662
INSERT INTO t1 VALUES (NULL), (1), (NULL), (2);
 
5663
CREATE TABLE t2 (a int, INDEX idx(a)) ;
 
5664
INSERT INTO t2 VALUES (NULL), (1), (NULL);
 
5665
SELECT * FROM t1
 
5666
WHERE EXISTS (SELECT a FROM t2 USE INDEX () WHERE t2.a = t1.a);
 
5667
a
 
5668
1
 
5669
EXPLAIN
 
5670
SELECT * FROM t1
 
5671
WHERE EXISTS (SELECT a FROM t2 USE INDEX() WHERE t2.a = t1.a);
 
5672
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
5673
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    4       Using where
 
5674
2       DEPENDENT SUBQUERY      t2      ALL     NULL    NULL    NULL    NULL    3       Using where
 
5675
SELECT * FROM t1
 
5676
WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
 
5677
a
 
5678
1
 
5679
EXPLAIN
 
5680
SELECT * FROM t1
 
5681
WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
 
5682
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
5683
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    4       Using where
 
5684
2       DEPENDENT SUBQUERY      t2      ref     idx     idx     5       test.t1.a       2       Using index
 
5685
DROP TABLE t1,t2;
 
5686
#
 
5687
# BUG#752992: Wrong results for a subquery with 'semijoin=on'
 
5688
#
 
5689
CREATE TABLE t1 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
 
5690
INSERT INTO t1 VALUES (11,0);
 
5691
INSERT INTO t1 VALUES (12,5);
 
5692
INSERT INTO t1 VALUES (15,0);
 
5693
CREATE TABLE t2 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
 
5694
INSERT INTO t2 VALUES (11,1);
 
5695
INSERT INTO t2 VALUES (12,2);
 
5696
INSERT INTO t2 VALUES (15,4);
 
5697
SET @save_join_cache_level=@@join_cache_level;
 
5698
SET join_cache_level=0;
 
5699
EXPLAIN SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
 
5700
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
5701
1       PRIMARY t1      ALL     PRIMARY NULL    NULL    NULL    3       
 
5702
1       PRIMARY it      eq_ref  PRIMARY PRIMARY 4       test.t1.pk      1       Using index
 
5703
1       PRIMARY t2      index   NULL    PRIMARY 4       NULL    3       Using index; FirstMatch(it)
 
5704
SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
 
5705
pk      i
 
5706
11      0
 
5707
12      5
 
5708
15      0
 
5709
SET join_cache_level=@save_join_cache_level;
 
5710
DROP table t1,t2;
 
5711
#
 
5712
# Bug#751350: crash with pushed condition for outer references when
 
5713
#             there should be none of such conditions
 
5714
#
 
5715
CREATE TABLE t1 (a int, b int) ;
 
5716
INSERT INTO t1 VALUES (0,0),(0,0);
 
5717
set @optimizer_switch_save=@@optimizer_switch;
 
5718
set @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=on';
 
5719
EXPLAIN
 
5720
SELECT b FROM t1
 
5721
WHERE ('0') IN ( SELECT a  FROM t1 GROUP BY a )
 
5722
GROUP BY b;
 
5723
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
5724
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       Using temporary; Using filesort
 
5725
2       SUBQUERY        t1      ALL     NULL    NULL    NULL    NULL    2       Using where
 
5726
SELECT b FROM t1
 
5727
WHERE ('0') IN ( SELECT a  FROM t1 GROUP BY a )
 
5728
GROUP BY b;
 
5729
b
 
5730
0
 
5731
set @@optimizer_switch=@optimizer_switch_save;
 
5732
DROP TABLE t1;
 
5733
#
 
5734
# Bug #11765713 58705:
 
5735
# OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
 
5736
# CREATED BY OPT_SUM_QUERY
 
5737
#
 
5738
CREATE TABLE t1(a INT NOT NULL, KEY (a));
 
5739
INSERT INTO t1 VALUES (0), (1);
 
5740
SELECT 1 as foo FROM t1 WHERE a < SOME
 
5741
(SELECT a FROM t1 WHERE a <=>
 
5742
(SELECT a FROM t1)
 
5743
);
 
5744
ERROR 21000: Subquery returns more than 1 row
 
5745
SELECT 1 as foo FROM t1 WHERE a < SOME
 
5746
(SELECT a FROM t1 WHERE a <=>
 
5747
(SELECT a FROM t1 where a is null)
 
5748
);
 
5749
foo
 
5750
DROP TABLE t1;
 
5751
CREATE TABLE t1 (a int(11), b varchar(1));
 
5752
INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
 
5753
SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
 
5754
a
 
5755
5
 
5756
SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
 
5757
a
 
5758
5
 
5759
SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
 
5760
a
 
5761
7
 
5762
SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
 
5763
a
 
5764
7
 
5765
SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
 
5766
a
 
5767
5
 
5768
7
 
5769
SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
 
5770
a
 
5771
5
 
5772
7
 
5773
SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
 
5774
a
 
5775
5
 
5776
7
 
5777
SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
 
5778
a
 
5779
5
 
5780
7
 
5781
SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
 
5782
a
 
5783
5
 
5784
7
 
5785
SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
 
5786
a
 
5787
5
 
5788
7
 
5789
SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
 
5790
a
 
5791
5
 
5792
7
 
5793
SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
 
5794
a
 
5795
5
 
5796
7
 
5797
SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
 
5798
a
 
5799
SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
 
5800
a
 
5801
SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
 
5802
a
 
5803
SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
 
5804
a
 
5805
SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
 
5806
a
 
5807
SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
 
5808
a
 
5809
SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
 
5810
a
 
5811
SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
 
5812
a
 
5813
SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
 
5814
a
 
5815
SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
 
5816
a
 
5817
SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
 
5818
a
 
5819
SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
 
5820
a
 
5821
delete from t1;
 
5822
INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
 
5823
SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
 
5824
a
 
5825
5
 
5826
SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
 
5827
a
 
5828
5
 
5829
SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
 
5830
a
 
5831
7
 
5832
SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
 
5833
a
 
5834
7
 
5835
SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
 
5836
a
 
5837
5
 
5838
7
 
5839
SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
 
5840
a
 
5841
5
 
5842
7
 
5843
SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
 
5844
a
 
5845
5
 
5846
7
 
5847
SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
 
5848
a
 
5849
5
 
5850
7
 
5851
SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
 
5852
a
 
5853
5
 
5854
7
 
5855
SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
 
5856
a
 
5857
5
 
5858
7
 
5859
SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
 
5860
a
 
5861
5
 
5862
7
 
5863
SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
 
5864
a
 
5865
5
 
5866
7
 
5867
SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
 
5868
a
 
5869
SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
 
5870
a
 
5871
SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
 
5872
a
 
5873
SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
 
5874
a
 
5875
SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
 
5876
a
 
5877
SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
 
5878
a
 
5879
SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
 
5880
a
 
5881
SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
 
5882
a
 
5883
SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
 
5884
a
 
5885
SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
 
5886
a
 
5887
SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
 
5888
a
 
5889
SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
 
5890
a
 
5891
drop table t1;
 
5892
#
 
5893
# Fix of lp:780386 (NULL left part with empty ALL subquery).
 
5894
#
 
5895
CREATE TABLE t1 ( f11 int) ;
 
5896
INSERT IGNORE INTO t1 VALUES (0),(0);
 
5897
CREATE TABLE t2 ( f3 int, f10 int, KEY (f10,f3)) ;
 
5898
INSERT IGNORE INTO t2 VALUES (NULL,NULL),(5,0);
 
5899
DROP TABLE IF EXISTS t3;
 
5900
Warnings:
 
5901
Note    1051    Unknown table 't3'
 
5902
CREATE TABLE t3 ( f3 int) ;
 
5903
INSERT INTO t3 VALUES (0),(0);
 
5904
SELECT a1.f3 AS r FROM t2 AS a1 , t1 WHERE a1.f3 < ALL ( SELECT f3 FROM t3 WHERE f3 = 1 ) ;
 
5905
r
 
5906
NULL
 
5907
5
 
5908
NULL
 
5909
5
 
5910
DROP TABLE t1, t2, t3;
 
5911
#
 
5912
# Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
 
5913
#
 
5914
CREATE TABLE t1(a1 int);
 
5915
INSERT INTO t1 VALUES (1),(2);
 
5916
CREATE TABLE t2(a1 int);
 
5917
INSERT INTO t2 VALUES (3);
 
5918
SELECT @@session.sql_mode INTO @old_sql_mode;
 
5919
SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
 
5920
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
 
5921
1
 
5922
1
 
5923
1
 
5924
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
 
5925
1
 
5926
1
 
5927
1
 
5928
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
 
5929
1
 
5930
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
 
5931
1
 
5932
1
 
5933
1
 
5934
SET SESSION sql_mode=@old_sql_mode;
 
5935
DROP TABLE t1, t2;
 
5936
create table t2(i int);
 
5937
insert into t2 values(0);
 
5938
SELECT @@session.sql_mode INTO @old_sql_mode;
 
5939
SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
 
5940
CREATE VIEW v1 AS  
 
5941
SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
 
5942
;
 
5943
CREATE TABLE t1 (
 
5944
pk int NOT NULL,
 
5945
col_varchar_key varchar(1) DEFAULT NULL,
 
5946
PRIMARY KEY (pk),
 
5947
KEY col_varchar_key (col_varchar_key)
 
5948
);
 
5949
SELECT t1.pk
 
5950
FROM t1
 
5951
WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
 
5952
;
 
5953
pk
 
5954
SET SESSION sql_mode=@old_sql_mode;
 
5955
drop table t2, t1;
 
5956
drop view v1;
 
5957
#
 
5958
# BUG#50257: Missing info in REF column of the EXPLAIN 
 
5959
#            lines for subselects
 
5960
#
 
5961
CREATE TABLE t1 (a INT, b INT, INDEX (a));
 
5962
INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
 
5963
EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
 
5964
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
5965
1       SIMPLE  t1      ref     a       a       5       const   1       
 
5966
EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
 
5967
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
5968
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    4       
 
5969
2       SUBQUERY        t1      ref     a       a       5       const   1       Using index
 
5970
DROP TABLE t1;
 
5971
#
 
5972
# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
 
5973
# (duplicate of LP bug #888456)
 
5974
#
 
5975
CREATE TABLE t1 (f1 varchar(1));
 
5976
INSERT INTO t1 VALUES ('v'),('s');
 
5977
CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
 
5978
INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
 
5979
('d'),('y'),('t'),('d'),('s');
 
5980
EXPLAIN
 
5981
SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2
 
5982
WHERE EXISTS (SELECT DISTINCT f1_key FROM t2 
 
5983
WHERE f1_key != table2.f1_key AND f1_key >= table1.f1);
 
5984
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
5985
1       PRIMARY table1  ALL     NULL    NULL    NULL    NULL    2       
 
5986
1       PRIMARY table2  index   NULL    f1_key  4       NULL    10      Using where; Using index; Using join buffer (flat, BNL join)
 
5987
2       DEPENDENT SUBQUERY      t2      index   f1_key  f1_key  4       NULL    10      Using where; Using index
 
5988
SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2
 
5989
WHERE EXISTS (SELECT DISTINCT f1_key FROM t2 
 
5990
WHERE f1_key != table2.f1_key AND f1_key >= table1.f1);
 
5991
f1      f1_key
 
5992
v       j
 
5993
s       j
 
5994
v       v
 
5995
s       v
 
5996
v       c
 
5997
s       c
 
5998
v       m
 
5999
s       m
 
6000
v       d
 
6001
s       d
 
6002
v       d
 
6003
s       d
 
6004
v       y
 
6005
s       y
 
6006
v       t
 
6007
s       t
 
6008
v       d
 
6009
s       d
 
6010
v       s
 
6011
s       s
 
6012
DROP TABLE t1,t2;
 
6013
#
 
6014
# LP bug 919427: EXPLAIN for a query over a single-row table 
 
6015
#                with IN subquery in WHERE condition
 
6016
 
6017
CREATE TABLE ot (
 
6018
col_int_nokey int(11), 
 
6019
col_varchar_nokey varchar(1)
 
6020
) ;
 
6021
INSERT INTO ot VALUES (1,'x');
 
6022
CREATE TABLE it1(
 
6023
col_int_key int(11), 
 
6024
col_varchar_key varchar(1), 
 
6025
KEY idx_cvk_cik (col_varchar_key,col_int_key)
 
6026
);
 
6027
INSERT INTO it1 VALUES (NULL,'x'), (NULL,'f');
 
6028
CREATE TABLE it2 (
 
6029
col_int_key int(11),
 
6030
col_varchar_key varchar(1),
 
6031
col_varchar_key2 varchar(1),
 
6032
KEY idx_cvk_cvk2_cik (col_varchar_key, col_varchar_key2, col_int_key),
 
6033
KEY idx_cvk_cik (col_varchar_key, col_int_key)
 
6034
);
 
6035
INSERT INTO it2 VALUES (NULL,'x','x'), (NULL,'f','f');
 
6036
EXPLAIN
 
6037
SELECT col_int_nokey FROM ot 
 
6038
WHERE col_varchar_nokey IN 
 
6039
(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
 
6040
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6041
1       PRIMARY ot      system  NULL    NULL    NULL    NULL    1       
 
6042
1       PRIMARY <subquery2>     eq_ref  distinct_key    distinct_key    4       func    1       
 
6043
2       MATERIALIZED    it1     ref     idx_cvk_cik     idx_cvk_cik     9       const,const     1       Using where; Using index
 
6044
SELECT col_int_nokey FROM ot 
 
6045
WHERE col_varchar_nokey IN 
 
6046
(SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
 
6047
col_int_nokey
 
6048
1
 
6049
EXPLAIN
 
6050
SELECT col_int_nokey FROM ot
 
6051
WHERE (col_varchar_nokey, 'x') IN
 
6052
(SELECT col_varchar_key, col_varchar_key2 FROM it2);
 
6053
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6054
1       PRIMARY ot      system  NULL    NULL    NULL    NULL    1       
 
6055
1       PRIMARY <subquery2>     eq_ref  distinct_key    distinct_key    8       func,func       1       
 
6056
2       MATERIALIZED    it2     ref     idx_cvk_cvk2_cik,idx_cvk_cik    idx_cvk_cvk2_cik        8       const,const     1       Using where; Using index
 
6057
SELECT col_int_nokey FROM ot
 
6058
WHERE (col_varchar_nokey, 'x') IN
 
6059
(SELECT col_varchar_key, col_varchar_key2 FROM it2);
 
6060
col_int_nokey
 
6061
1
 
6062
DROP TABLE ot,it1,it2;
 
6063
#
 
6064
# MDEV-746
 
6065
# Bug#13651009 WRONG RESULT FROM DERIVED TABLE IF THE SUBQUERY
 
6066
# HAS AN EMPTY RESULT
 
6067
#
 
6068
CREATE TABLE t1 (
 
6069
pk int NOT NULL,
 
6070
col_int_nokey int NOT NULL,
 
6071
col_int_key int NOT NULL,
 
6072
col_time_key time NOT NULL,
 
6073
col_varchar_key varchar(1) NOT NULL,
 
6074
col_varchar_nokey varchar(1) NOT NULL,
 
6075
PRIMARY KEY (pk),
 
6076
KEY col_int_key (col_int_key),
 
6077
KEY col_time_key (col_time_key),
 
6078
KEY col_varchar_key (col_varchar_key,col_int_key)
 
6079
) ENGINE=MyISAM;
 
6080
CREATE TABLE t2 (
 
6081
pk int NOT NULL AUTO_INCREMENT,
 
6082
col_int_nokey int NOT NULL,
 
6083
col_int_key int NOT NULL,
 
6084
col_time_key time NOT NULL,
 
6085
col_varchar_key varchar(1) NOT NULL,
 
6086
col_varchar_nokey varchar(1) NOT NULL,
 
6087
PRIMARY KEY (pk),
 
6088
KEY col_int_key (col_int_key),
 
6089
KEY col_time_key (col_time_key),
 
6090
KEY col_varchar_key (col_varchar_key,col_int_key)
 
6091
) ENGINE=MyISAM;
 
6092
INSERT INTO t2 VALUES (1,4,4,'00:00:00','b','b');
 
6093
SET @var2:=4, @var3:=8;
 
6094
 
 
6095
Testcase without inner subquery
 
6096
EXPLAIN SELECT @var3:=12, sq4_alias1.*
 
6097
FROM t1 AS sq4_alias1
 
6098
WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
 
6099
sq4_alias1.col_varchar_key = @var3;
 
6100
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6101
1       SIMPLE  NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
6102
SELECT @var3:=12, sq4_alias1.*
 
6103
FROM t1 AS sq4_alias1
 
6104
WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
 
6105
sq4_alias1.col_varchar_key = @var3;
 
6106
@var3:=12       pk      col_int_nokey   col_int_key     col_time_key    col_varchar_key col_varchar_nokey
 
6107
SELECT @var3;
 
6108
@var3
 
6109
8
 
6110
EXPLAIN SELECT * FROM ( SELECT @var3:=12, sq4_alias1.*
 
6111
FROM t1 AS sq4_alias1
 
6112
WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
 
6113
sq4_alias1.col_varchar_key = @var3 ) AS alias3;
 
6114
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6115
1       SIMPLE  <derived2>      system  NULL    NULL    NULL    NULL    0       const row not found
 
6116
2       DERIVED NULL    NULL    NULL    NULL    NULL    NULL    NULL    no matching row in const table
 
6117
SELECT * FROM ( SELECT @var3:=12, sq4_alias1.*
 
6118
FROM t1 AS sq4_alias1
 
6119
WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
 
6120
sq4_alias1.col_varchar_key = @var3 ) AS alias3;
 
6121
@var3:=12       pk      col_int_nokey   col_int_key     col_time_key    col_varchar_key col_varchar_nokey
 
6122
SELECT @var3;
 
6123
@var3
 
6124
8
 
6125
 
 
6126
Testcase with inner subquery; crashed WL#6095
 
6127
SET @var3=8;
 
6128
EXPLAIN SELECT sq4_alias1.*
 
6129
FROM t1 AS sq4_alias1
 
6130
WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
 
6131
NOT IN
 
6132
(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
 
6133
c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
 
6134
FROM t2 AS c_sq1_alias1
 
6135
WHERE (c_sq1_alias1.col_int_nokey != @var2
 
6136
OR c_sq1_alias1.pk != @var3));
 
6137
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6138
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
6139
2       DEPENDENT SUBQUERY      c_sq1_alias1    system  PRIMARY NULL    NULL    NULL    1       
 
6140
SELECT sq4_alias1.*
 
6141
FROM t1 AS sq4_alias1
 
6142
WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
 
6143
NOT IN
 
6144
(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
 
6145
c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
 
6146
FROM t2 AS c_sq1_alias1
 
6147
WHERE (c_sq1_alias1.col_int_nokey != @var2
 
6148
OR c_sq1_alias1.pk != @var3));
 
6149
pk      col_int_nokey   col_int_key     col_time_key    col_varchar_key col_varchar_nokey
 
6150
EXPLAIN SELECT * FROM ( SELECT sq4_alias1.*
 
6151
FROM t1 AS sq4_alias1
 
6152
WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
 
6153
NOT IN
 
6154
(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
 
6155
c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
 
6156
FROM t2 AS c_sq1_alias1
 
6157
WHERE (c_sq1_alias1.col_int_nokey != @var2
 
6158
OR c_sq1_alias1.pk != @var3)) ) AS alias3;
 
6159
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6160
1       SIMPLE  NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
6161
3       DEPENDENT SUBQUERY      c_sq1_alias1    system  PRIMARY NULL    NULL    NULL    1       
 
6162
SELECT * FROM ( SELECT sq4_alias1.*
 
6163
FROM t1 AS sq4_alias1
 
6164
WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
 
6165
NOT IN
 
6166
(SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
 
6167
c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
 
6168
FROM t2 AS c_sq1_alias1
 
6169
WHERE (c_sq1_alias1.col_int_nokey != @var2
 
6170
OR c_sq1_alias1.pk != @var3)) ) AS alias3;
 
6171
pk      col_int_nokey   col_int_key     col_time_key    col_varchar_key col_varchar_nokey
 
6172
DROP TABLE t1,t2;
 
6173
End of 5.2 tests
 
6174
#
 
6175
# BUG#779885: Crash in eliminate_item_equal with materialization=on in
 
6176
#
 
6177
CREATE TABLE t1 ( f1 int );
 
6178
INSERT INTO t1 VALUES (19), (20);
 
6179
CREATE TABLE t2 ( f10 varchar(32) );
 
6180
INSERT INTO t2 VALUES ('c'),('d');
 
6181
CREATE TABLE t3 ( f10 varchar(32) );
 
6182
INSERT INTO t3 VALUES ('a'),('b');
 
6183
SELECT *
 
6184
FROM t1
 
6185
WHERE
 
6186
( 't' ) IN (
 
6187
SELECT t3.f10
 
6188
FROM t3
 
6189
JOIN t2
 
6190
ON t2.f10 = t3.f10
 
6191
);
 
6192
f1
 
6193
DROP TABLE t1,t2,t3;
 
6194
#
 
6195
# BUG lp:813473: Wrong result with outer join + NOT IN subquery
 
6196
# This bug is a duplicate of Bug#11764086 whose test case is added below
 
6197
#
 
6198
CREATE TABLE t1 (c int) ;
 
6199
INSERT INTO t1 VALUES (5),(6);
 
6200
CREATE TABLE t2 (a int, b int) ;
 
6201
INSERT INTO t2 VALUES (20,9),(20,9);
 
6202
create table t3 (d int, e int);
 
6203
insert into t3 values (2, 9), (3,10);
 
6204
SET @save_optimizer_switch=@@optimizer_switch;
 
6205
SET optimizer_switch='outer_join_with_cache=off';
 
6206
EXPLAIN
 
6207
SELECT t2.b , t1.c
 
6208
FROM t2 LEFT JOIN t1 ON t1.c < 3
 
6209
WHERE (t2.b , t1.c) NOT IN (SELECT * from t3);
 
6210
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6211
1       PRIMARY t2      ALL     NULL    NULL    NULL    NULL    2       
 
6212
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       Using where
 
6213
2       DEPENDENT SUBQUERY      t3      ALL     NULL    NULL    NULL    NULL    2       Using where
 
6214
SELECT t2.b , t1.c
 
6215
FROM t2 LEFT JOIN t1 ON t1.c < 3
 
6216
WHERE (t2.b, t1.c) NOT IN (SELECT * from t3);
 
6217
b       c
 
6218
9       NULL
 
6219
9       NULL
 
6220
SET optimizer_switch=@save_optimizer_switch;
 
6221
drop table t1, t2, t3;
 
6222
#
 
6223
# BUG#50257: Missing info in REF column of the EXPLAIN 
 
6224
#            lines for subselects
 
6225
#
 
6226
CREATE TABLE t1 (a INT, b INT, INDEX (a));
 
6227
INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
 
6228
 
 
6229
set @tmp_optimizer_switch=@@optimizer_switch;
 
6230
set optimizer_switch='derived_merge=off,derived_with_keys=off';
 
6231
EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
 
6232
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6233
1       PRIMARY <derived2>      ALL     NULL    NULL    NULL    NULL    2       
 
6234
2       DERIVED t1      ref     a       a       5       const   1       
 
6235
set optimizer_switch=@tmp_optimizer_switch;
 
6236
 
 
6237
EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
 
6238
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6239
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    4       
 
6240
2       SUBQUERY        t1      ref     a       a       5       const   1       Using index
 
6241
 
 
6242
DROP TABLE t1;
 
6243
#
 
6244
# Bug#11764086: Null left operand to NOT IN in WHERE clause
 
6245
# behaves differently than real NULL
 
6246
#
 
6247
CREATE TABLE parent (id int);
 
6248
INSERT INTO parent VALUES (1), (2);
 
6249
CREATE TABLE child (parent_id int, other int);
 
6250
INSERT INTO child VALUES (1,NULL);
 
6251
# Offending query (c.parent_id is NULL for null-complemented rows only)
 
6252
SELECT    p.id, c.parent_id
 
6253
FROM      parent p
 
6254
LEFT JOIN child  c
 
6255
ON        p.id = c.parent_id
 
6256
WHERE     c.parent_id NOT IN (
 
6257
SELECT parent_id 
 
6258
FROM   child
 
6259
WHERE  parent_id = 3
 
6260
);
 
6261
id      parent_id
 
6262
1       1
 
6263
2       NULL
 
6264
# Some syntactic variations with IS FALSE and IS NOT TRUE
 
6265
SELECT    p.id, c.parent_id
 
6266
FROM      parent p
 
6267
LEFT JOIN child  c
 
6268
ON        p.id = c.parent_id
 
6269
WHERE     c.parent_id IN (
 
6270
SELECT parent_id 
 
6271
FROM   child
 
6272
WHERE  parent_id = 3
 
6273
) IS NOT TRUE;
 
6274
id      parent_id
 
6275
1       1
 
6276
2       NULL
 
6277
SELECT    p.id, c.parent_id
 
6278
FROM      parent p
 
6279
LEFT JOIN child  c
 
6280
ON        p.id = c.parent_id
 
6281
WHERE     c.parent_id IN (
 
6282
SELECT parent_id 
 
6283
FROM   child
 
6284
WHERE  parent_id = 3
 
6285
) IS FALSE;
 
6286
id      parent_id
 
6287
1       1
 
6288
2       NULL
 
6289
DROP TABLE parent, child;
 
6290
# End of test for bug#11764086.
 
6291
#
 
6292
# Bug 11765699 - 58690: !TABLE || (!TABLE->READ_SET || 
 
6293
#                BITMAP_IS_SET(TABLE->READ_SET, FIELD_INDEX
 
6294
#
 
6295
CREATE TABLE t1(a INT);
 
6296
INSERT INTO t1 VALUES (0), (1);
 
6297
CREATE TABLE t2(
 
6298
b TEXT, 
 
6299
c INT, 
 
6300
PRIMARY KEY (b(1))
 
6301
);
 
6302
INSERT INTO t2 VALUES ('a', 2), ('b', 3);
 
6303
SELECT 1 FROM t1 WHERE a = 
 
6304
(SELECT 1 FROM t2 WHERE b = 
 
6305
(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
 
6306
ORDER BY b
 
6307
);
 
6308
1
 
6309
SELECT 1 FROM t1 WHERE a = 
 
6310
(SELECT 1 FROM t2 WHERE b = 
 
6311
(SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
 
6312
GROUP BY b
 
6313
);
 
6314
1
 
6315
DROP TABLE t1, t2;
 
6316
#
 
6317
# BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
 
6318
#
 
6319
CREATE TABLE t1 (f1 varchar(1));
 
6320
INSERT INTO t1 VALUES ('v'),('s');
 
6321
CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
 
6322
INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
 
6323
('d'),('y'),('t'),('d'),('s');
 
6324
SELECT table1.f1, table2.f1_key
 
6325
FROM t1 AS table1, t2 AS table2
 
6326
WHERE EXISTS 
 
6327
 
6328
SELECT DISTINCT f1_key 
 
6329
FROM t2 
 
6330
WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
 
6331
f1      f1_key
 
6332
v       j
 
6333
s       j
 
6334
v       v
 
6335
s       v
 
6336
v       c
 
6337
s       c
 
6338
v       m
 
6339
s       m
 
6340
v       d
 
6341
s       d
 
6342
v       d
 
6343
s       d
 
6344
v       y
 
6345
s       y
 
6346
v       t
 
6347
s       t
 
6348
v       d
 
6349
s       d
 
6350
v       s
 
6351
s       s
 
6352
explain SELECT table1.f1, table2.f1_key
 
6353
FROM t1 AS table1, t2 AS table2
 
6354
WHERE EXISTS 
 
6355
 
6356
SELECT DISTINCT f1_key 
 
6357
FROM t2 
 
6358
WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
 
6359
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6360
1       PRIMARY table1  ALL     NULL    NULL    NULL    NULL    2       
 
6361
1       PRIMARY table2  index   NULL    f1_key  4       NULL    10      Using where; Using index; Using join buffer (flat, BNL join)
 
6362
2       DEPENDENT SUBQUERY      t2      index   f1_key  f1_key  4       NULL    10      Using where; Using index
 
6363
DROP TABLE t1,t2;
 
6364
#
 
6365
# lp:826279: assertion failure with GROUP BY a result of subquery
 
6366
#
 
6367
CREATE TABLE t1 (a int);
 
6368
INSERT INTO t1 VALUES (0), (0);
 
6369
CREATE TABLE t2 (a int, b int, c int);
 
6370
INSERT INTO t2 VALUES (10,7,0), (0,7,0);
 
6371
CREATE TABLE t3 (a int, b int);
 
6372
INSERT INTO t3 VALUES (10,7), (0,7);
 
6373
SELECT SUM(DISTINCT b),
 
6374
(SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
 
6375
WHERE t.a != 0 AND t2.a != 0)
 
6376
FROM (SELECT * FROM t3) AS t
 
6377
GROUP BY 2;
 
6378
SUM(DISTINCT b) (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
 
6379
WHERE t.a != 0 AND t2.a != 0)
 
6380
7       NULL
 
6381
SELECT SUM(DISTINCT b), 
 
6382
(SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
 
6383
FROM (SELECT * FROM t3) AS t
 
6384
GROUP BY 2;
 
6385
SUM(DISTINCT b) (SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
 
6386
7       NULL
 
6387
7       10
 
6388
DROP TABLE t1,t2,t3;
 
6389
#
 
6390
# Bug#12329653 
 
6391
# EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
 
6392
#
 
6393
CREATE TABLE t1(a1 int);
 
6394
INSERT INTO t1 VALUES (1),(2);
 
6395
SELECT @@session.sql_mode INTO @old_sql_mode;
 
6396
SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
 
6397
SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
 
6398
1
 
6399
1
 
6400
1
 
6401
PREPARE stmt FROM 
 
6402
'SELECT 1 UNION ALL 
 
6403
SELECT 1 FROM t1
 
6404
ORDER BY
 
6405
(SELECT 1 FROM t1 AS t1_0  
 
6406
  WHERE 1 < SOME (SELECT a1 FROM t1)
 
6407
)' ;
 
6408
EXECUTE stmt ;
 
6409
ERROR 21000: Subquery returns more than 1 row
 
6410
EXECUTE stmt ;
 
6411
ERROR 21000: Subquery returns more than 1 row
 
6412
SET SESSION sql_mode=@old_sql_mode;
 
6413
DEALLOCATE PREPARE stmt;
 
6414
DROP TABLE t1;
 
6415
#
 
6416
# LP BUG#833777 Performance regression with deeply nested subqueries
 
6417
#
 
6418
create table t1 (a int not null, b char(10) not null);
 
6419
insert into t1 values (1, 'a');
 
6420
set @@optimizer_switch='in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off';
 
6421
select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1)))))))))))))))))))))))))))));
 
6422
a
 
6423
1
 
6424
set @@optimizer_switch=@subselect_tmp;
 
6425
drop table t1;
 
6426
#
 
6427
# LP BUG#894397 Wrong result with in_to_exists, constant table , semijoin=OFF,materialization=OFF
 
6428
#
 
6429
CREATE TABLE t1 (a varchar(3));
 
6430
INSERT INTO t1 VALUES ('AAA'),('BBB');
 
6431
CREATE TABLE t2 (a varchar(3));
 
6432
INSERT INTO t2 VALUES ('CCC');
 
6433
set @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off';
 
6434
SELECT * FROM t1 WHERE t1.a IN (SELECT t2.a FROM t2 WHERE t2.a < 'ZZZ');
 
6435
a
 
6436
set @@optimizer_switch=@subselect_tmp;
 
6437
drop table t1, t2;
 
6438
#
 
6439
# LP bug #859375: Assertion `0' failed in st_select_lex_unit::optimize
 
6440
# with view , UNION and prepared statement (rewriting fake_select
 
6441
# condition).
 
6442
#
 
6443
CREATE TABLE t1 ( f1 int NOT NULL, f4 varchar(1) NOT NULL) ;
 
6444
INSERT INTO t1 VALUES (6,'d'),(7,'y');
 
6445
CREATE TABLE t2 ( f1 int NOT NULL, f2 int NOT NULL) ;
 
6446
INSERT INTO t2 VALUES (10,7);
 
6447
CREATE VIEW v2 AS SELECT * FROM t2;
 
6448
PREPARE st1 FROM "
 
6449
        SELECT *
 
6450
        FROM t1
 
6451
        LEFT JOIN v2 ON ( v2.f2 = t1.f1 )
 
6452
        WHERE v2.f1 NOT IN (
 
6453
                SELECT 1 UNION
 
6454
                SELECT 247
 
6455
        )
 
6456
";
 
6457
EXECUTE st1;
 
6458
f1      f4      f1      f2
 
6459
7       y       10      7
 
6460
deallocate prepare st1;
 
6461
DROP VIEW v2;
 
6462
DROP TABLE t1,t2;
 
6463
#
 
6464
# LP bug #887458 Crash in subselect_union_engine::no_rows with
 
6465
# double UNION and join_cache_level=3,8
 
6466
# (IN/ALL/ANY optimizations should not be applied to fake_select)
 
6467
CREATE TABLE t2 ( a int, b varchar(1)) ;
 
6468
INSERT IGNORE INTO t2 VALUES (8,'y'),(8,'y');
 
6469
CREATE TABLE t1 ( b varchar(1)) ;
 
6470
INSERT IGNORE INTO t1 VALUES (NULL),(NULL);
 
6471
set @save_join_cache_level=@@join_cache_level;
 
6472
SET SESSION join_cache_level=3;
 
6473
SELECT *
 
6474
FROM t1, t2
 
6475
WHERE t2.b IN (
 
6476
SELECT 'm' UNION
 
6477
SELECT 'm'
 
6478
) OR t1.b <> SOME (
 
6479
SELECT 'v' UNION
 
6480
SELECT 't'
 
6481
);
 
6482
b       a       b
 
6483
set @@join_cache_level= @save_join_cache_level;
 
6484
drop table t1,t2;
 
6485
#
 
6486
# LP bug #885162 Got error 124 from storage engine with UNION inside
 
6487
# subquery and join_cache_level=3..8
 
6488
# (IN/ALL/ANY optimizations should not be applied to fake_select)
 
6489
#
 
6490
CREATE TABLE t1 (
 
6491
f1 varchar(1) DEFAULT NULL
 
6492
);
 
6493
INSERT INTO t1 VALUES ('c');
 
6494
set @save_join_cache_level=@@join_cache_level;
 
6495
SET SESSION join_cache_level=8;
 
6496
SELECT * FROM t1 WHERE t1.f1 IN ( SELECT 'k' UNION SELECT 'e' );
 
6497
f1
 
6498
set @@join_cache_level= @save_join_cache_level;
 
6499
drop table t1;
 
6500
#
 
6501
# LP BUG#747278 incorrect values of the NULL (no rows) single 
 
6502
# row subquery requested via element_index() interface
 
6503
#
 
6504
CREATE TABLE t1 (f1a int, f1b int) ;
 
6505
INSERT IGNORE INTO t1 VALUES (1,1),(2,2);
 
6506
CREATE TABLE t2 ( f2 int);
 
6507
INSERT IGNORE INTO t2 VALUES (3),(4);
 
6508
CREATE TABLE t3 (f3a int default 1, f3b int default 2);
 
6509
INSERT INTO t3 VALUES (1,1),(2,2);
 
6510
set @old_optimizer_switch = @@session.optimizer_switch;
 
6511
set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
 
6512
SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
 
6513
(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
 
6514
NULL
 
6515
NULL
 
6516
SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
 
6517
(SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
 
6518
NULL
 
6519
NULL
 
6520
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
 
6521
(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
 
6522
NULL
 
6523
NULL
 
6524
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
 
6525
(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
 
6526
NULL
 
6527
SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
 
6528
(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
 
6529
NULL
 
6530
NULL
 
6531
SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
 
6532
(SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
 
6533
NULL
 
6534
NULL
 
6535
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
 
6536
(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
 
6537
NULL
 
6538
NULL
 
6539
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
 
6540
(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
 
6541
NULL
 
6542
set @@session.optimizer_switch=@old_optimizer_switch;
 
6543
SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
 
6544
(SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
 
6545
NULL
 
6546
NULL
 
6547
SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
 
6548
(SELECT f3a,f3a  FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
 
6549
NULL
 
6550
NULL
 
6551
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
 
6552
(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
 
6553
NULL
 
6554
NULL
 
6555
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
 
6556
(SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
 
6557
NULL
 
6558
SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
 
6559
(SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
 
6560
NULL
 
6561
NULL
 
6562
SELECT (SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
 
6563
(SELECT f3a,f3a  FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
 
6564
NULL
 
6565
NULL
 
6566
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
 
6567
(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
 
6568
NULL
 
6569
NULL
 
6570
SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
 
6571
(SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
 
6572
NULL
 
6573
select (null, null) = (null, null);
 
6574
(null, null) = (null, null)
 
6575
NULL
 
6576
SELECT (SELECT f3a, f3a FROM t3  where f3a > 3) = (0, 0);
 
6577
(SELECT f3a, f3a FROM t3  where f3a > 3) = (0, 0)
 
6578
NULL
 
6579
drop tables t1,t2,t3;
 
6580
#
 
6581
# LP BUG#825051 Wrong result with date/datetime and subquery with GROUP BY and in_to_exists
 
6582
#
 
6583
CREATE TABLE t1 (a date, KEY (a)) ;
 
6584
INSERT INTO t1 VALUES ('2009-01-01'),('2009-02-02');
 
6585
set @old_optimizer_switch = @@optimizer_switch;
 
6586
SET @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off';
 
6587
EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
 
6588
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6589
1       PRIMARY t1      index   NULL    a       4       NULL    2       Using where; Using index
 
6590
2       DEPENDENT SUBQUERY      t1      index_subquery  a       a       4       func    2       Using index
 
6591
SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
 
6592
a
 
6593
2009-01-01
 
6594
2009-02-02
 
6595
SET @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=off,subquery_cache=off';
 
6596
EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
 
6597
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6598
1       PRIMARY t1      index   NULL    a       4       NULL    2       Using where; Using index
 
6599
2       MATERIALIZED    t1      index   NULL    a       4       NULL    2       Using index
 
6600
SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
 
6601
a
 
6602
2009-01-01
 
6603
2009-02-02
 
6604
set @@optimizer_switch=@old_optimizer_switch;
 
6605
drop table t1;
 
6606
#
 
6607
# LP BUG#908269 incorrect condition in case of subqueries depending
 
6608
# on constant tables
 
6609
#
 
6610
CREATE TABLE t1 ( a INT );
 
6611
INSERT INTO t1 VALUES (1),(5);
 
6612
CREATE TABLE t2 ( b INT ) ENGINE=MyISAM;
 
6613
INSERT INTO t2 VALUES (1);
 
6614
CREATE TABLE t3 ( c INT );
 
6615
INSERT INTO t3 VALUES (4),(5);
 
6616
SET optimizer_switch='subquery_cache=off';
 
6617
SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
 
6618
( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) )
 
6619
1
 
6620
NULL
 
6621
SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
 
6622
( SELECT b FROM t2 WHERE b = a OR b * 0)
 
6623
1
 
6624
NULL
 
6625
SELECT ( SELECT b FROM t2 WHERE b = a OR rand() * 0) FROM t1;
 
6626
( SELECT b FROM t2 WHERE b = a OR rand() * 0)
 
6627
1
 
6628
NULL
 
6629
drop table t1,t2,t3;
 
6630
set optimizer_switch=@subselect_tmp;
 
6631
#
 
6632
# LP BUG#905353 Wrong non-empty result with a constant table,
 
6633
# aggregate function in subquery, MyISAM or Aria
 
6634
#
 
6635
CREATE TABLE t1 ( a INT ) ENGINE=MyISAM;
 
6636
INSERT INTO t1 VALUES (1);
 
6637
SELECT a FROM t1 WHERE ( SELECT MIN(a) = 100 );
 
6638
a
 
6639
drop table t1;
 
6640
#
 
6641
# LP BUG#985667 Wrong result with subquery in SELECT clause, and constant table in 
 
6642
# main query and implicit grouping
 
6643
#
 
6644
CREATE TABLE t1 (f1 int) engine=MyISAM;
 
6645
INSERT INTO t1 VALUES (7),(8);
 
6646
CREATE TABLE t2 (f2 int, f3 varchar(1)) engine=MyISAM;
 
6647
INSERT INTO t2 VALUES (3,'f');
 
6648
EXPLAIN
 
6649
SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
 
6650
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6651
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
6652
2       SUBQUERY        t1      ALL     NULL    NULL    NULL    NULL    2       
 
6653
SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
 
6654
COUNT(f1)       f4
 
6655
0       NULL
 
6656
EXPLAIN
 
6657
SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
 
6658
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6659
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
6660
2       SUBQUERY        t1      ALL     NULL    NULL    NULL    NULL    2       
 
6661
SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
 
6662
COUNT(f1)       f4
 
6663
0       0
 
6664
EXPLAIN
 
6665
SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
 
6666
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6667
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
6668
2       DEPENDENT SUBQUERY      t1      ALL     NULL    NULL    NULL    NULL    2       Using where
 
6669
SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
 
6670
COUNT(f1)       f4
 
6671
0       1
 
6672
EXPLAIN
 
6673
SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
 
6674
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6675
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE noticed after reading const tables
 
6676
2       DEPENDENT SUBQUERY      t1      ALL     NULL    NULL    NULL    NULL    2       Using where
 
6677
SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
 
6678
COUNT(f1)       f4
 
6679
0       0
 
6680
drop table t1,t2;
 
6681
#
 
6682
# LP BUG#1002079 Server crashes in Item_singlerow_subselect::val_int with constant table,
 
6683
# HAVING, UNION in subquery
 
6684
#
 
6685
CREATE TABLE t1 (a INT);
 
6686
INSERT INTO t1 VALUES (7),(0);
 
6687
CREATE TABLE t2 (b INT);
 
6688
EXPLAIN
 
6689
SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7;
 
6690
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6691
1       PRIMARY t2      system  NULL    NULL    NULL    NULL    0       const row not found
 
6692
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       
 
6693
2       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
6694
3       UNION   NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
6695
NULL    UNION RESULT    <union2,3>      ALL     NULL    NULL    NULL    NULL    NULL    
 
6696
SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7;
 
6697
f1      f2
 
6698
drop table t1,t2;
 
6699
#
 
6700
# LP BUG#1008686 Server crashes in subselect_union_engine::no_rows on SELECT with impossible
 
6701
# WHERE and UNION in HAVING
 
6702
#
 
6703
CREATE TABLE t1 (a INT);
 
6704
INSERT INTO t1 VALUES (1),(7);
 
6705
EXPLAIN
 
6706
SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1;
 
6707
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6708
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE
 
6709
2       DEPENDENT SUBQUERY      NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
6710
3       DEPENDENT UNION NULL    NULL    NULL    NULL    NULL    NULL    NULL    No tables used
 
6711
NULL    UNION RESULT    <union2,3>      ALL     NULL    NULL    NULL    NULL    NULL    
 
6712
SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1;
 
6713
min_a   a
 
6714
EXPLAIN
 
6715
SELECT MIN(a) AS min_a, a FROM t1 WHERE 1=2 HAVING a NOT IN ( SELECT a from t1 UNION select a+1 from t1 ) OR min_a != 1;
 
6716
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6717
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE
 
6718
2       DEPENDENT SUBQUERY      t1      ALL     NULL    NULL    NULL    NULL    2       Using where
 
6719
3       DEPENDENT UNION t1      ALL     NULL    NULL    NULL    NULL    2       Using where
 
6720
NULL    UNION RESULT    <union2,3>      ALL     NULL    NULL    NULL    NULL    NULL    
 
6721
SELECT MIN(a) AS min_a, a FROM t1 WHERE 1=2 HAVING a NOT IN ( SELECT a from t1 UNION select a+1 from t1 ) OR min_a != 1;
 
6722
min_a   a
 
6723
drop table t1;
 
6724
#
 
6725
# MDEV-367: Different results with and without subquery_cache on
 
6726
# a query with a constant NOT IN condition
 
6727
#
 
6728
CREATE TABLE t1 (a INT) ENGINE=MyISAM;
 
6729
INSERT INTO t1 VALUES (1),(2),(3);
 
6730
set @mdev367_optimizer_switch = @@optimizer_switch;
 
6731
set optimizer_switch = 'subquery_cache=on';
 
6732
SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100;
 
6733
a
 
6734
SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1;
 
6735
a       ( 3, 3 ) NOT IN ( SELECT NULL, NULL )
 
6736
1       NULL
 
6737
2       NULL
 
6738
3       NULL
 
6739
set optimizer_switch=@mdev367_optimizer_switch;
 
6740
set optimizer_switch = 'subquery_cache=off';
 
6741
SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100;
 
6742
a
 
6743
SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1;
 
6744
a       ( 3, 3 ) NOT IN ( SELECT NULL, NULL )
 
6745
1       NULL
 
6746
2       NULL
 
6747
3       NULL
 
6748
set optimizer_switch=@mdev367_optimizer_switch;
 
6749
DROP TABLE t1;
 
6750
#
 
6751
# MDEV-521 single value subselect transformation problem
 
6752
#
 
6753
CREATE TABLE t1 (f1 char(2), PRIMARY KEY (f1)) ENGINE=MyISAM;
 
6754
INSERT INTO t1 VALUES ('u1'),('u2');
 
6755
SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) );
 
6756
f1
 
6757
u1
 
6758
u2
 
6759
FLUSH TABLES;
 
6760
SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) );
 
6761
f1
 
6762
u1
 
6763
u2
 
6764
DROP TABLE t1;
 
6765
# return optimizer switch changed in the beginning of this test
 
6766
set optimizer_switch=@subselect_tmp;
 
6767
#
 
6768
# lp:944706 Query with impossible or constant subquery in WHERE or HAVING is not
 
6769
# precomputed and thus not part of optimization
 
6770
#
 
6771
CREATE TABLE t1 ( a VARCHAR(16), KEY (a) );
 
6772
INSERT INTO t1 VALUES ('Abilene'),('Akron'),('Albany'),('Albuquerque'),('Alexandria'),('Allentown'),
 
6773
('Amarillo'),('Anaheim'),('Anchorage'),('Ann Arbor'),('Arden-Arcade');
 
6774
EXPLAIN
 
6775
SELECT MAX( alias2.a ) AS field
 
6776
FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
 
6777
WHERE alias1.a = alias2.a OR alias1.a = 'y'
 
6778
HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 );
 
6779
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6780
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible HAVING
 
6781
2       SUBQUERY        t1      index_subquery  a       a       19      const   1       Using index; Using where
 
6782
SELECT MAX( alias2.a ) AS field
 
6783
FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
 
6784
WHERE alias1.a = alias2.a OR alias1.a = 'y'
 
6785
HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 );
 
6786
field
 
6787
EXPLAIN
 
6788
SELECT MAX( alias2.a )
 
6789
FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
 
6790
WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 );
 
6791
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6792
1       PRIMARY alias1  index   a       a       19      NULL    11      Using where; Using index
 
6793
1       PRIMARY alias2  ref     a       a       19      test.alias1.a   2       Using index
 
6794
1       PRIMARY alias3  index   NULL    a       19      NULL    11      Using index; Using join buffer (flat, BNL join)
 
6795
2       SUBQUERY        t1      index_subquery  a       a       19      const   1       Using index; Using where
 
6796
SELECT MAX( alias2.a )
 
6797
FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
 
6798
WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 );
 
6799
MAX( alias2.a )
 
6800
Arden-Arcade
 
6801
drop table t1;
 
6802
#
 
6803
# MDEV-277 CHEAP SQ: Server crashes in st_join_table::get_examined_rows
 
6804
# with semijoin+materialization, IN and = subqueries
 
6805
#
 
6806
CREATE TABLE t1 (a1 INT);
 
6807
INSERT INTO t1 VALUES (4),(6);
 
6808
CREATE TABLE t2 (b1 INT);
 
6809
INSERT INTO t2 VALUES (1),(7);
 
6810
EXPLAIN
 
6811
SELECT * FROM t1
 
6812
WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2));
 
6813
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6814
1       PRIMARY t1      ALL     NULL    NULL    NULL    NULL    2       Using where
 
6815
2       SUBQUERY        t1      ALL     NULL    NULL    NULL    NULL    2       
 
6816
2       SUBQUERY        <subquery3>     eq_ref  distinct_key    distinct_key    4       func    1       
 
6817
3       MATERIALIZED    t1      ALL     NULL    NULL    NULL    NULL    2       
 
6818
3       MATERIALIZED    t2      ALL     NULL    NULL    NULL    NULL    2       Using join buffer (flat, BNL join)
 
6819
SELECT * FROM t1
 
6820
WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2));
 
6821
a1
 
6822
drop table t1, t2;
 
6823
#
 
6824
# MDEV-287 CHEAP SQ: A query with subquery in SELECT list, EXISTS,
 
6825
# inner joins takes hundreds times longer
 
6826
#
 
6827
CREATE TABLE t1 (a INT);
 
6828
INSERT INTO t1 VALUES (1),(7);
 
6829
CREATE TABLE t2 (b INT);
 
6830
INSERT INTO t2 VALUES (4),(5);
 
6831
CREATE TABLE t3 (c INT);
 
6832
INSERT INTO t3 VALUES (8),(3);
 
6833
set @@expensive_subquery_limit= 0;
 
6834
EXPLAIN
 
6835
SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
 
6836
FROM t2 alias1, t1 alias2, t1 alias3;
 
6837
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6838
1       PRIMARY alias1  ALL     NULL    NULL    NULL    NULL    2       
 
6839
1       PRIMARY alias2  ALL     NULL    NULL    NULL    NULL    2       Using join buffer (flat, BNL join)
 
6840
1       PRIMARY alias3  ALL     NULL    NULL    NULL    NULL    2       Using join buffer (flat, BNL join)
 
6841
2       DEPENDENT SUBQUERY      t1      ALL     NULL    NULL    NULL    NULL    2       Using where
 
6842
2       DEPENDENT SUBQUERY      t2      ALL     NULL    NULL    NULL    NULL    2       Using where; Using join buffer (flat, BNL join)
 
6843
3       SUBQUERY        t3      ALL     NULL    NULL    NULL    NULL    2       
 
6844
flush status;
 
6845
SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
 
6846
FROM t2 alias1, t1 alias2, t1 alias3;
 
6847
(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
 
6848
NULL
 
6849
NULL
 
6850
NULL
 
6851
NULL
 
6852
NULL
 
6853
NULL
 
6854
NULL
 
6855
NULL
 
6856
show status like "subquery_cache%";
 
6857
Variable_name   Value
 
6858
Subquery_cache_hit      6
 
6859
Subquery_cache_miss     2
 
6860
show status like '%Handler_read%';
 
6861
Variable_name   Value
 
6862
Handler_read_first      0
 
6863
Handler_read_key        8
 
6864
Handler_read_last       0
 
6865
Handler_read_next       0
 
6866
Handler_read_prev       0
 
6867
Handler_read_rnd        0
 
6868
Handler_read_rnd_deleted        0
 
6869
Handler_read_rnd_next   22
 
6870
set @@expensive_subquery_limit= default;
 
6871
EXPLAIN
 
6872
SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
 
6873
FROM t2 alias1, t1 alias2, t1 alias3;
 
6874
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6875
1       PRIMARY alias1  ALL     NULL    NULL    NULL    NULL    2       
 
6876
1       PRIMARY alias2  ALL     NULL    NULL    NULL    NULL    2       Using join buffer (flat, BNL join)
 
6877
1       PRIMARY alias3  ALL     NULL    NULL    NULL    NULL    2       Using join buffer (flat, BNL join)
 
6878
2       SUBQUERY        t1      ALL     NULL    NULL    NULL    NULL    2       
 
6879
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       Using where; Using join buffer (flat, BNL join)
 
6880
3       SUBQUERY        t3      ALL     NULL    NULL    NULL    NULL    2       
 
6881
flush status;
 
6882
SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
 
6883
FROM t2 alias1, t1 alias2, t1 alias3;
 
6884
(SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
 
6885
NULL
 
6886
NULL
 
6887
NULL
 
6888
NULL
 
6889
NULL
 
6890
NULL
 
6891
NULL
 
6892
NULL
 
6893
show status like "subquery_cache%";
 
6894
Variable_name   Value
 
6895
Subquery_cache_hit      0
 
6896
Subquery_cache_miss     0
 
6897
show status like '%Handler_read%';
 
6898
Variable_name   Value
 
6899
Handler_read_first      0
 
6900
Handler_read_key        0
 
6901
Handler_read_last       0
 
6902
Handler_read_next       0
 
6903
Handler_read_prev       0
 
6904
Handler_read_rnd        0
 
6905
Handler_read_rnd_deleted        0
 
6906
Handler_read_rnd_next   16
 
6907
drop table t1, t2, t3;
 
6908
#
 
6909
# MDEV-288 CHEAP SQ: Valgrind warnings "Memory lost" with IN and EXISTS nested subquery, materialization+semijoin
 
6910
#
 
6911
CREATE TABLE t1 (a INT);
 
6912
INSERT INTO t1 VALUES (0),(8);
 
6913
CREATE TABLE t2 (b INT PRIMARY KEY);
 
6914
INSERT INTO t2 VALUES (1),(2);
 
6915
EXPLAIN
 
6916
SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
 
6917
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6918
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE
 
6919
2       MATERIALIZED    NULL    NULL    NULL    NULL    NULL    NULL    NULL    Select tables optimized away
 
6920
3       SUBQUERY        t1      ALL     NULL    NULL    NULL    NULL    2       
 
6921
SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
 
6922
a
 
6923
drop table t1,t2;
 
6924
#
 
6925
# MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used
 
6926
#
 
6927
CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM;
 
6928
INSERT INTO t1 VALUES ('USA');
 
6929
CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM;
 
6930
INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia');
 
6931
CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM;
 
6932
INSERT INTO t3 VALUES (12),(22),(9),(45);
 
6933
create table t4 like t3;
 
6934
insert into t4 select * from t3;
 
6935
# This should not show range access for table t2
 
6936
explain
 
6937
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1 
 
6938
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
 
6939
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6940
1       PRIMARY t1      system  NULL    NULL    NULL    NULL    1       
 
6941
1       PRIMARY t2      ALL     b       NULL    NULL    NULL    2       Using where
 
6942
1       PRIMARY t3      ref     d       d       5       test.t2.b       2       Using where; Using index
 
6943
3       SUBQUERY        NULL    NULL    NULL    NULL    NULL    NULL    NULL    Select tables optimized away
 
6944
set @tmp_mdev410=@@global.userstat;
 
6945
set global userstat=on;
 
6946
flush table_statistics;
 
6947
flush index_statistics;
 
6948
SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1 
 
6949
WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
 
6950
MIN(b)
 
6951
NULL
 
6952
# The following shows that t2 was indeed scanned with a full scan.
 
6953
show table_statistics;
 
6954
Table_schema    Table_name      Rows_read       Rows_changed    Rows_changed_x_#indexes
 
6955
test    t1      2       0       0
 
6956
test    t2      3       0       0
 
6957
show index_statistics;
 
6958
Table_schema    Table_name      Index_name      Rows_read
 
6959
test    t2      b       1
 
6960
set global userstat=@tmp_mdev410;
 
6961
DROP TABLE t1,t2,t3,t4;
 
6962
#
 
6963
# MDEV-430: Server crashes in select_describe on EXPLAIN with 
 
6964
#    materialization+semijoin, 2 nested subqueries, aggregate functions
 
6965
#
 
6966
CREATE TABLE t1 (a INT, KEY(a));
 
6967
INSERT INTO t1 VALUES (1),(8);
 
6968
CREATE TABLE t2 (b INT, KEY(b));
 
6969
INSERT INTO t2 VALUES (45),(17),(20);
 
6970
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
 
6971
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6972
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE
 
6973
2       SUBQUERY        t1      index   a       a       5       NULL    2       Using where; Using index
 
6974
2       SUBQUERY        t2      ref     b       b       5       test.t1.a       2       Using index
 
6975
DROP TABLE t1,t2;
 
6976
#
 
6977
# MDEV-435: Expensive subqueries may be evaluated during optimization in merge_key_fields
 
6978
#
 
6979
CREATE TABLE t1 (a INT, KEY(a)) ENGINE=MyISAM;
 
6980
INSERT INTO t1 VALUES (8),(0);
 
6981
CREATE TABLE t2 (b INT, c VARCHAR(1)) ENGINE=MyISAM;
 
6982
INSERT INTO t2 VALUES (4,'j'),(6,'v');
 
6983
CREATE TABLE t3 (d VARCHAR(1)) ENGINE=MyISAM;
 
6984
INSERT INTO t3 VALUES ('b'),('c');
 
6985
EXPLAIN
 
6986
SELECT * FROM t1
 
6987
WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10;
 
6988
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
6989
1       PRIMARY t1      index   a       a       5       NULL    2       Using where; Using index
 
6990
2       SUBQUERY        <subquery3>     ALL     distinct_key    NULL    NULL    NULL    1       
 
6991
2       SUBQUERY        t2      ALL     NULL    NULL    NULL    NULL    2       Using where; Using join buffer (flat, BNL join)
 
6992
3       MATERIALIZED    t3      ALL     NULL    NULL    NULL    NULL    2       
 
6993
SELECT * FROM t1
 
6994
WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10;
 
6995
a
 
6996
drop table t1, t2, t3;
 
6997
#
 
6998
# MDEV-405: Server crashes in test_if_skip_sort_order on EXPLAIN with GROUP BY and HAVING in EXISTS subquery
 
6999
#
 
7000
CREATE TABLE t1 (a INT, KEY(a));
 
7001
INSERT INTO t1 VALUES (1),(8);
 
7002
CREATE TABLE t2 (b INT, KEY(b));
 
7003
INSERT INTO t2 VALUES (45),(17),(20);
 
7004
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
 
7005
id      select_type     table   type    possible_keys   key     key_len ref     rows    Extra
 
7006
1       PRIMARY NULL    NULL    NULL    NULL    NULL    NULL    NULL    Impossible WHERE
 
7007
2       SUBQUERY        t1      index   a       a       5       NULL    2       Using where; Using index
 
7008
2       SUBQUERY        t2      ref     b       b       5       test.t1.a       2       Using index
 
7009
DROP TABLE t1,t2;