100
97
select * from t3 where a < any (select b from t2);
101
98
select * from t3 where a < all (select b from t2);
102
99
select * from t3 where a >= any (select b from t2);
104
100
explain extended select * from t3 where a >= any (select b from t2);
105
101
select * from t3 where a >= all (select b from t2);
106
102
delete from t2 where a=100;
107
--error ER_OPERAND_COLUMNS
108
104
select * from t3 where a in (select a,b from t2);
109
--error ER_OPERAND_COLUMNS
110
106
select * from t3 where a in (select * from t2);
111
107
insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10);
113
109
select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b);
114
110
insert into t2 values (2,10);
115
111
select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b);
116
112
delete from t2 where a=2 and b=10;
117
113
select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
118
create temporary table t5 (a int) ENGINE=MyISAM;
114
create table t5 (a int);
119
115
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
120
116
insert into t5 values (5);
121
117
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
122
118
insert into t5 values (2);
123
119
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
125
120
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;
126
--error ER_SUBQUERY_NO_1_ROW
127
122
select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
128
create temporary table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq)) ENGINE=MyISAM;
129
create temporary table t7( uq int primary key, name char(25)) ENGINE=MyISAM;
123
create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
124
create table t7( uq int primary key, name char(25));
130
125
insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
131
126
insert into t6 values (1,1),(1,2),(2,2),(1,3);
132
127
select * from t6 where exists (select * from t7 where uq = clinic_uq);
133
128
explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq);
135
--echo # not unique fields
136
--error ER_NON_UNIQ_ERROR
137
132
select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
139
--echo # different tipes & group functions
134
# different tipes & group functions
140
135
drop table t1,t2,t3;
142
137
CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0');
143
138
INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b');
144
139
CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0');
145
140
INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2');
146
CREATE TABLE t1 (a varchar(20),b date NULL);
141
CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00');
147
142
INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13');
148
143
SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1);
149
144
SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2);
188
182
SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
189
183
SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
190
184
SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1;
191
--error ER_SUBQUERY_NO_1_ROW
192
186
SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1;
193
187
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1);
196
--echo #forumconthardwarefr7 searchconthardwarefr7
190
#forumconthardwarefr7 searchconthardwarefr7
197
191
CREATE TABLE `t1` (
198
192
`numeropost` bigint NOT NULL auto_increment,
199
193
`maxnumrep` int NOT NULL default '0',
200
194
PRIMARY KEY (`numeropost`),
201
195
UNIQUE KEY `maxnumrep` (`maxnumrep`)
196
) ENGINE=MyISAM ROW_FORMAT=FIXED;
204
198
INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2);
206
200
CREATE TABLE `t2` (
207
201
`mot` varchar(30) NOT NULL default '',
208
202
`topic` bigint NOT NULL default '0',
203
`date` date NOT NULL default '0000-00-00',
210
204
`pseudo` varchar(35) NOT NULL default '',
211
205
PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`)
206
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
214
208
INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce');
215
209
select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
216
210
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;
217
--error ER_BAD_FIELD_ERROR
218
212
SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b;
219
--error ER_BAD_FIELD_ERROR
220
214
SELECT 1 IN (SELECT 1 FROM t2 HAVING a);
222
216
SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic);
271
265
PRIMARY KEY (`numeropost`,`numreponse`),
272
266
UNIQUE KEY `numreponse` (`numreponse`),
273
267
KEY `pseudo` (`pseudo`,`numeropost`)
275
--error ER_ILLEGAL_REFERENCE
276
270
SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
277
--error ER_BAD_FIELD_ERROR
278
272
SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
279
273
SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
280
274
INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test');
281
--error ER_SUBQUERY_NO_1_ROW
282
276
EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
284
277
EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1';
286
278
EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1');
289
CREATE TEMPORARY TABLE t1 (a int) ENGINE=MyISAM;
281
CREATE TABLE t1 (a int);
290
282
INSERT INTO t1 VALUES (1);
291
283
SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1;
294
--echo #update with subselects
286
#update with subselects
295
287
create table t1 (a int NOT NULL, b int, primary key (a));
296
288
create table t2 (a int NOT NULL, b int, primary key (a));
297
289
insert into t1 values (0, 10),(1, 11),(2, 12);
298
290
insert into t2 values (1, 21),(2, 22),(3, 23);
299
291
select * from t1;
300
--error ER_UPDATE_TABLE_USED
301
293
update t1 set b= (select b from t1);
302
--error ER_SUBQUERY_NO_1_ROW
303
295
update t1 set b= (select b from t2);
304
296
update t1 set b= (select b from t2 where t1.a = t2.a);
305
297
select * from t1;
306
298
drop table t1, t2;
308
--echo #delete with subselects
300
#delete with subselects
309
301
create table t1 (a int NOT NULL, b int, primary key (a));
310
302
create table t2 (a int NOT NULL, b int, primary key (a));
311
303
insert into t1 values (0, 10),(1, 11),(2, 12);
312
304
insert into t2 values (1, 21),(2, 12),(3, 23);
313
305
select * from t1;
314
306
select * from t1 where b = (select b from t2 where t1.a = t2.a);
315
--error ER_UPDATE_TABLE_USED
316
308
delete from t1 where b = (select b from t1);
317
--error ER_SUBQUERY_NO_1_ROW
318
310
delete from t1 where b = (select b from t2);
319
311
delete from t1 where b = (select b from t2 where t1.a = t2.a);
320
312
select * from t1;
321
313
drop table t1, t2;
323
--echo #insert with subselects
315
#multi-delete with subselects
317
create table t11 (a int NOT NULL, b int, primary key (a));
318
create table t12 (a int NOT NULL, b int, primary key (a));
319
create table t2 (a int NOT NULL, b int, primary key (a));
320
insert into t11 values (0, 10),(1, 11),(2, 12);
321
insert into t12 values (33, 10),(22, 11),(2, 12);
322
insert into t2 values (1, 21),(2, 12),(3, 23);
327
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
329
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
330
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
334
drop table t11, t12, t2;
336
#insert with subselects
324
337
CREATE TABLE t1 (x int);
325
338
create table t2 (a int);
326
339
create table t3 (b int);
327
340
insert into t2 values (1);
328
341
insert into t3 values (1),(2);
329
--error ER_UPDATE_TABLE_USED
330
343
INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));
331
--error ER_SUBQUERY_NO_1_ROW
332
345
INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));
333
346
INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));
334
347
select * from t1;
335
348
insert into t2 values (1);
336
INSERT INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
349
INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
337
351
select * from t1;
338
352
INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;
339
353
select * from t1;
340
--echo # After this, only data based on old t1 records should have been added.
354
# After this, only data based on old t1 records should have been added.
341
355
INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;
342
356
select * from t1;
343
--error ER_BAD_FIELD_ERROR
344
INSERT INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
345
INSERT INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
358
INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
359
INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
346
361
select * from t1;
348
--echo #TODO: should be uncommented after bug 380 fix pushed
349
--echo #INSERT INTO t1 (x) SELECT (SELECT SUM(a)+b FROM t2) from t3;
350
--echo #select * from t1;
363
#TODO: should be uncommented after bug 380 fix pushed
364
#INSERT INTO t1 (x) SELECT (SELECT SUM(a)+b FROM t2) from t3;
351
366
drop table t1, t2, t3;
353
--echo #replace with subselects
368
#replace with subselects
354
369
CREATE TABLE t1 (x int not null, y int, primary key (x));
355
370
create table t2 (a int);
356
create temporary table t3 (a int) ENGINE=MyISAM;
371
create table t3 (a int);
357
372
insert into t2 values (1);
358
373
insert into t3 values (1),(2);
359
374
select * from t1;
360
--error ER_UPDATE_TABLE_USED
361
376
replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));
362
--error ER_SUBQUERY_NO_1_ROW
363
378
replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));
364
379
replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));
365
380
select * from t1;
366
381
replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));
367
382
select * from t1;
368
replace into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
383
replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
370
386
select * from t1;
371
replace into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
387
replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
372
389
select * from t1;
373
replace into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
390
replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
375
392
select * from t1;
376
393
drop table t1, t2, t3;
378
--error ER_NO_TABLES_USED
379
396
SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);
381
CREATE TABLE t2 (id int default NULL, KEY id (id));
398
CREATE TABLE t2 (id int default NULL, KEY id (id)) ENGINE=MyISAM;
382
399
INSERT INTO t2 VALUES (1),(2);
383
400
SELECT * FROM t2 WHERE id IN (SELECT 1);
384
401
EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);
446
463
select (select a+1) from t1;
450
--echo # Null with keys
453
CREATE TEMPORARY TABLE t1 (a int NOT NULL default '0', PRIMARY KEY (a)) ENGINE=MyISAM;
454
CREATE TEMPORARY TABLE t2 (a int default '0', INDEX (a)) ENGINE=MyISAM;
470
CREATE TABLE t1 (a int NOT NULL default '0', PRIMARY KEY (a));
471
CREATE TABLE t2 (a int default '0', INDEX (a));
455
472
INSERT INTO t1 VALUES (1),(2),(3),(4);
456
473
INSERT INTO t2 VALUES (1),(2),(3);
457
474
SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
458
475
explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
459
CREATE TEMPORARY TABLE t3 (a int default '0') ENGINE=MyISAM;
476
CREATE TABLE t3 (a int default '0');
460
477
INSERT INTO t3 VALUES (1),(2),(3);
461
478
SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
462
479
explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
463
480
drop table t1,t2,t3;
465
--echo #LIMIT is not supported now
466
--echo #create table t1 (a float) ENGINE=MyISAM;
467
--echo #--error ER_NOT_SUPPORTED_YET
468
--echo #select 10.5 IN (SELECT * from t1 LIMIT 1);
469
--echo #--error ER_NOT_SUPPORTED_YET
470
--echo #select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
471
--echo #drop table t1;
473
--echo #create table t1 (a int, b int, c varchar(10)) ENGINE=MyISAM;
474
--echo #create table t2 (a int) ENGINE=MyISAM;
475
--echo #insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
476
--echo #insert into t2 values (1),(2),(NULL);
477
--echo #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;
478
--echo #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;
479
--echo #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;
480
--echo #drop table t1,t2;
482
--echo #create table t1 (a int, b real, c varchar(10)) ENGINE=MyISAM;
483
--echo #insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
484
--echo #select ROW(1, 1, 'a') IN (select a,b,c from t1);
485
--echo #select ROW(1, 2, 'a') IN (select a,b,c from t1);
486
--echo #select ROW(1, 1, 'a') IN (select b,a,c from t1);
487
--echo #select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
488
--echo #select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
489
--echo #select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
490
--echo #select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
491
--echo #select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
492
--echo #select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
493
--echo #--error ER_NOT_SUPPORTED_YET
494
--echo #select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
495
--echo #drop table t1;
482
#LIMIT is not supported now
483
#create table t1 (a float);
485
#select 10.5 IN (SELECT * from t1 LIMIT 1);
487
#select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
490
#create table t1 (a int, b int, c varchar(10));
491
#create table t2 (a int);
492
#insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
493
#insert into t2 values (1),(2),(NULL);
494
#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;
495
#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;
496
#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;
499
#create table t1 (a int, b real, c varchar(10));
500
#insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
501
#select ROW(1, 1, 'a') IN (select a,b,c from t1);
502
#select ROW(1, 2, 'a') IN (select a,b,c from t1);
503
#select ROW(1, 1, 'a') IN (select b,a,c from t1);
504
#select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
505
#select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
506
#select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
507
#select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
508
#select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
509
#select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
511
#select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
497
--echo #test of uncacheable subqueries
514
#test of uncacheable subqueries
498
515
CREATE TABLE t1 (a int);
499
516
EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
500
517
EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
504
CREATE TEMPORARY TABLE `t1` (
505
`mot` varchar(30) NOT NULL default '',
506
`topic` bigint NOT NULL default '0',
507
`date` date NULL DEFAULT '2009-01-20',
508
`pseudo` varchar(35) NOT NULL default '',
509
PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
510
KEY `pseudo` (`pseudo`,`date`,`topic`),
511
KEY `topic` (`topic`)
512
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
514
CREATE TEMPORARY TABLE `t2` (
515
`mot` varchar(30) NOT NULL default '',
516
`topic` bigint NOT NULL default '0',
517
`date` date NULL default '1997-08-29',
518
`pseudo` varchar(35) NOT NULL default '',
519
PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
520
KEY `pseudo` (`pseudo`,`date`,`topic`),
521
KEY `topic` (`topic`)
522
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
524
CREATE TEMPORARY TABLE `t3` (
522
`mot` varchar(30) NOT NULL default '',
523
`topic` bigint NOT NULL default '0',
524
`date` date NOT NULL default '0000-00-00',
525
`pseudo` varchar(35) NOT NULL default '',
526
PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
527
KEY `pseudo` (`pseudo`,`date`,`topic`),
528
KEY `topic` (`topic`)
529
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
532
`mot` varchar(30) NOT NULL default '',
533
`topic` bigint NOT NULL default '0',
534
`date` date NOT NULL default '0000-00-00',
535
`pseudo` varchar(35) NOT NULL default '',
536
PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
537
KEY `pseudo` (`pseudo`,`date`,`topic`),
538
KEY `topic` (`topic`)
539
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
525
542
`numeropost` bigint NOT NULL auto_increment,
526
543
`maxnumrep` int NOT NULL default '0',
527
544
PRIMARY KEY (`numeropost`),
689
721
drop table t1, t2;
692
--echo # reduced subselect in ORDER BY & GROUP BY clauses
724
# reduced subselect in ORDER BY & GROUP BY clauses
695
CREATE TEMPORARY TABLE `t1` (
696
728
`id` bigint NOT NULL auto_increment,
697
729
`pseudo` varchar(35) NOT NULL default '',
698
730
`email` varchar(60) NOT NULL default '',
699
731
PRIMARY KEY (`id`),
700
732
UNIQUE KEY `email` (`email`),
701
733
UNIQUE KEY `pseudo` (`pseudo`)
702
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
734
) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
703
735
INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');
704
736
SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1);
705
737
drop table if exists t1;
707
739
(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);
710
--echo # IN subselect optimization test
712
create temporary table t1 (a int not null, b int, primary key (a)) ENGINE=MyISAM;
713
create temporary table t2 (a int not null, primary key (a)) ENGINE=MyISAM;
714
create temporary table t3 (a int not null, b int, primary key (a)) ENGINE=MyISAM;
742
# IN subselect optimization test
744
create table t1 (a int not null, b int, primary key (a));
745
create table t2 (a int not null, primary key (a));
746
create table t3 (a int not null, b int, primary key (a));
715
747
insert into t1 values (1,10), (2,20), (3,30), (4,40);
716
748
insert into t2 values (2), (3), (4), (5);
717
749
insert into t3 values (10,3), (20,4), (30,5);
753
785
explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
754
786
drop table t1, t2, t3;
757
--echo # alloc_group_fields() working
759
create temporary table t1 (a int, b int) ENGINE=MyISAM;
760
create temporary table t2 (a int, b int) ENGINE=MyISAM;
761
create temporary table t3 (a int, b int) ENGINE=MyISAM;
789
# alloc_group_fields() working
791
create table t1 (a int, b int);
792
create table t2 (a int, b int);
793
create table t3 (a int, b int);
762
794
insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
763
795
insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
764
796
insert into t3 values (3,3), (2,2), (1,1);
765
797
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;
766
798
drop table t1,t2,t3;
769
--echo # aggregate functions in HAVING test
771
create temporary table t1 (s1 int) ENGINE=MyISAM;
772
create temporary table t2 (s1 int) ENGINE=MyISAM;
801
# aggregate functions in HAVING test
803
create table t1 (s1 int);
804
create table t2 (s1 int);
773
805
insert into t1 values (1);
774
806
insert into t2 values (1);
775
807
select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
776
808
drop table t1,t2;
779
--echo # update subquery with wrong field (to force name resolving
780
--echo # in UPDATE name space)
782
create temporary table t1 (s1 int) ENGINE=MyISAM;
783
create temporary table t2 (s1 int) ENGINE=MyISAM;
811
# update subquery with wrong field (to force name resolving
812
# in UPDATE name space)
814
create table t1 (s1 int);
815
create table t2 (s1 int);
784
816
insert into t1 values (1);
785
817
insert into t2 values (1);
786
--error ER_BAD_FIELD_ERROR
787
819
update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
788
820
DROP TABLE t1, t2;
791
--echo # collation test
793
--echo #CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
794
--echo # s2 CHAR(5) COLLATE latin1_swedish_ci) ENGINE=MyISAM;
795
--echo #INSERT INTO t1 VALUES ('z','?');
796
--echo #--error ER_CANT_AGGREGATE_2COLLATIONS
797
--echo #select * from t1 where s1 > (select max(s2) from t1);
798
--echo #--error ER_CANT_AGGREGATE_2COLLATIONS
799
--echo #select * from t1 where s1 > any (select max(s2) from t1);
800
--echo #drop table t1;
825
#CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
826
# s2 CHAR(5) COLLATE latin1_swedish_ci);
827
#INSERT INTO t1 VALUES ('z','?');
829
#select * from t1 where s1 > (select max(s2) from t1);
831
#select * from t1 where s1 > any (select max(s2) from t1);
803
--echo # aggregate functions reinitialization
835
# aggregate functions reinitialization
805
837
create table t1(toid int,rd int);
806
838
create table t2(userid int,pmnew int,pmtotal int);
807
839
insert into t2 values(1,0,0),(2,0,0);
861
892
explain extended select * from t3 where NULL >= some (select b from t2);
862
893
select * from t3 where NULL >= some (select b from t2 group by 1);
863
894
explain extended select * from t3 where NULL >= some (select b from t2 group by 1);
865
--echo # optimized static ALL/ANY with grouping
896
# optimized static ALL/ANY with grouping
867
898
insert into t2 values (2,2), (2,1), (3,3), (3,1);
868
899
select * from t3 where a > all (select max(b) from t2 group by a);
869
900
explain extended select * from t3 where a > all (select max(b) from t2 group by a);
870
901
drop table t2, t3;
873
--echo # correct used_tables()
904
# correct used_tables()
876
CREATE TEMPORARY TABLE `t1` ( `id` bigint NOT NULL auto_increment, `taskid` bigint NOT NULL default '0', `dbid` int NOT NULL default '0', `create_date` datetime, `last_update` datetime, PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=3 ;
907
CREATE TABLE `t1` ( `id` bigint NOT NULL auto_increment, `taskid` bigint NOT NULL default '0', `dbid` int 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 AUTO_INCREMENT=3 ;
877
908
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());
878
CREATE TEMPORARY TABLE `t2` (`db_id` int NOT NULL auto_increment,`name` varchar(200) NOT NULL default '',`primary_uid` int NOT NULL default '0',`secondary_uid` int NOT NULL default '0',PRIMARY KEY (`db_id`),UNIQUE KEY `name_2` (`name`)) ENGINE=MyISAM AUTO_INCREMENT=2147483647;
909
CREATE TABLE `t2` (`db_id` int NOT NULL auto_increment,`name` varchar(200) NOT NULL default '',`primary_uid` int NOT NULL default '0',`secondary_uid` int NOT NULL default '0',PRIMARY KEY (`db_id`),UNIQUE KEY `name_2` (`name`)) ENGINE=MyISAM AUTO_INCREMENT=2147483647;
879
910
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);
880
CREATE TEMPORARY TABLE `t3` (`taskgenid` bigint NOT NULL auto_increment,`dbid` int NOT NULL default '0',`taskid` int NOT NULL default '0',`mon` int NOT NULL default '1',`tues` int NOT NULL default '1',`wed` int NOT NULL default '1',`thur` int NOT NULL default '1',`fri` int NOT NULL default '1',`sat` int NOT NULL default '0',`sun` int NOT NULL default '0',`how_often` int NOT NULL default '1',`userid` int NOT NULL default '0',`active` int NOT NULL default '1',PRIMARY KEY (`taskgenid`)) ENGINE=MyISAM AUTO_INCREMENT=2 ;
911
CREATE TABLE `t3` (`taskgenid` bigint NOT NULL auto_increment,`dbid` int NOT NULL default '0',`taskid` int NOT NULL default '0',`mon` int NOT NULL default '1',`tues` int NOT NULL default '1',`wed` int NOT NULL default '1',`thur` int NOT NULL default '1',`fri` int NOT NULL default '1',`sat` int NOT NULL default '0',`sun` int NOT NULL default '0',`how_often` int NOT NULL default '1',`userid` int NOT NULL default '0',`active` int NOT NULL default '1',PRIMARY KEY (`taskgenid`)) ENGINE=MyISAM AUTO_INCREMENT=2 ;
881
912
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);
882
CREATE TEMPORARY TABLE `t4` (`task_id` int NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM;
913
CREATE TABLE `t4` (`task_id` int NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM;
883
914
INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
884
915
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;
885
916
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;
886
917
drop table t1,t2,t3,t4;
889
--echo # cardinality check
891
CREATE TEMPORARY TABLE t1 (id int default NULL) ENGINE=MyISAM;
922
CREATE TABLE t1 (id int default NULL) ENGINE=MyISAM;
892
923
INSERT INTO t1 VALUES (1),(5);
893
CREATE TEMPORARY TABLE t2 (id int default NULL) ENGINE=MyISAM;
924
CREATE TABLE t2 (id int default NULL) ENGINE=MyISAM;
894
925
INSERT INTO t2 VALUES (2),(6);
895
--error ER_OPERAND_COLUMNS
896
927
select * from t1 where (1,2,6) in (select * from t2);
897
928
DROP TABLE t1,t2;
900
--echo # optimized ALL/ANY with union
931
# optimized ALL/ANY with union
902
933
create table t1 (s1 char);
903
934
insert into t1 values ('e');
904
935
select * from t1 where 'f' > any (select s1 from t1);
905
936
select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
907
937
explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
911
--echo # filesort in subquery (restoring join_tab)
913
CREATE TEMPORARY TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM;
941
# filesort in subquery (restoring join_tab)
943
CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM;
914
944
INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874');
915
CREATE TEMPORARY TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM;
945
CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM;
916
946
INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');
917
947
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;
918
948
drop table t1, t2;
921
--echo # unresolved field error
923
create temporary table t1 (s1 int) ENGINE=MyISAM;
924
create temporary table t2 (s1 int) ENGINE=MyISAM;
925
--error ER_BAD_FIELD_ERROR
951
# unresolved field error
953
create table t1 (s1 int);
954
create table t2 (s1 int);
926
956
select * from t1 where (select count(*) from t2 where t1.s2) = 1;
927
--error ER_BAD_FIELD_ERROR
928
958
select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
929
--error ER_BAD_FIELD_ERROR
930
960
select count(*) from t2 group by t1.s2;
931
961
drop table t1, t2;
934
--echo # fix_fields() in add_ref_to_table_cond()
936
CREATE TEMPORARY TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB)) ENGINE=MyISAM;
937
CREATE TEMPORARY TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA)) ENGINE=MyISAM;
964
# fix_fields() in add_ref_to_table_cond()
966
CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB));
967
CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA));
938
968
INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365');
939
969
INSERT INTO t2 VALUES (100, 200, 'C');
940
970
SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1);
941
971
DROP TABLE t1, t2;
943
CREATE TEMPORARY TABLE t1 (a int) ENGINE=MyISAM;
973
CREATE TABLE t1 (a int);
944
974
INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);
945
975
SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;
952
CREATE TEMPORARY TABLE `t1` (
953
983
`id` int NOT NULL auto_increment,
954
984
`id_cns` int NOT NULL default '0',
955
985
`tipo` enum('','UNO','DUE') NOT NULL default '',
959
989
`bis` int NOT NULL default '0',
960
990
PRIMARY KEY (`id`),
961
991
UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
962
UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`))
992
UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
964
994
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);
965
CREATE TEMPORARY TABLE `t2` (
966
996
`id` int NOT NULL auto_increment,
967
997
`max_anno_dep` int NOT NULL default '0',
968
PRIMARY KEY (`id`)) ENGINE=MyISAM;
969
1000
INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
971
1002
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;
973
1004
DROP TABLE t1, t2;
976
--echo # GLOBAL LIMIT
978
create temporary table t1 (a int) ENGINE=MyISAM;
1009
create table t1 (a int);
979
1010
insert into t1 values (1), (2), (3);
980
1011
SET SQL_SELECT_LIMIT=1;
981
1012
select sum(a) from (select * from t1) as a;
983
1014
SET SQL_SELECT_LIMIT=default;
987
--echo # Bug #3118: subselect + order by
1018
# Bug #3118: subselect + order by
990
1021
CREATE TABLE t1 (a int, b int, INDEX (a));
991
1022
INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);
992
1023
SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b;
995
--echo # Item_cond fix field
1026
# Item_cond fix field
997
1028
create table t1(val varchar(10));
998
1029
insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');
999
1030
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%');
1003
--echo # ref_or_null replacing with ref
1034
# ref_or_null replacing with ref
1005
1036
create table t1 (id int not null, text varchar(20) not null default '', primary key (id));
1006
1037
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');
1007
1038
select * from t1 where id not in (select id from t1 where id < 8);
1008
1039
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);
1009
--replace_column 9 #
1010
1040
explain extended select * from t1 where id not in (select id from t1 where id < 8);
1011
--replace_column 9 #
1012
1041
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);
1013
1042
insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
1014
create temporary table t2 (id int not null, text varchar(20) not null default '', primary key (id)) ENGINE=MyISAM;
1043
create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
1015
1044
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');
1016
1045
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);
1017
--replace_column 9 #
1018
1046
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);
1019
1047
drop table t1,t2;
1022
--echo # Static tables & rund() in subqueries
1024
create temporary table t1 (a int) ENGINE=MyISAM;
1050
# Static tables & rund() in subqueries
1052
create table t1 (a int);
1025
1053
insert into t1 values (1);
1026
1054
explain select benchmark(1000, (select a from t1 where a=rand()));
1032
create temporary table t1(id int) ENGINE=MyISAM;
1033
create temporary table t2(id int) ENGINE=MyISAM;
1034
create temporary table t3(flag int) ENGINE=MyISAM;
1035
--error ER_PARSE_ERROR
1060
create table t1(id int);
1061
create table t2(id int);
1062
create table t3(flag int);
1036
1064
select (select * from t3 where id not null) from t1, t2;
1037
1065
drop table t1,t2,t3;
1040
--echo # aggregate functions (Bug #3505)
1068
# aggregate functions (Bug #3505)
1042
1070
CREATE TABLE t1 (id INT);
1043
1071
CREATE TABLE t2 (id INT);
1044
1072
INSERT INTO t1 VALUES (1), (2);
1119
1147
SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1120
1148
SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1121
1149
SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
1123
--echo # < > >= <= and = ALL/ <> ANY do not support row operation
1124
--error ER_OPERAND_COLUMNS
1151
# < > >= <= and = ALL/ <> ANY do not support row operation
1125
1153
SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2);
1126
--error ER_OPERAND_COLUMNS
1127
1155
SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2);
1128
--error ER_OPERAND_COLUMNS
1129
1157
SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2);
1130
--error ER_OPERAND_COLUMNS
1131
1159
SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2);
1132
--error ER_OPERAND_COLUMNS
1133
1161
SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2);
1134
--error ER_OPERAND_COLUMNS
1135
1163
SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2);
1136
--error ER_OPERAND_COLUMNS
1137
1165
SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2);
1138
--error ER_OPERAND_COLUMNS
1139
1167
SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2);
1140
--echo # following should be converted to IN
1141
--error ER_OPERAND_COLUMNS
1168
# following should be converted to IN
1142
1170
SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2);
1143
--error ER_OPERAND_COLUMNS
1144
1172
SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2);
1145
1173
SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2);
1146
--error ER_OPERAND_COLUMNS
1147
1175
SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
1148
--error ER_OPERAND_COLUMNS
1149
1177
SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
1150
1178
SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
1151
1179
SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
1241
1268
HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a);
1242
1269
DROP TABLE t1, t2;
1245
--echo # Test problem with NULL and derived tables (Bug #4097)
1272
# Test problem with NULL and derived tables (Bug #4097)
1248
CREATE TEMPORARY TABLE t1 (id int default NULL,name varchar(10) default NULL) ENGINE=MyISAM;
1275
CREATE TABLE t1 (id int default NULL,name varchar(10) default NULL);
1249
1276
INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL);
1250
CREATE TEMPORARY TABLE t2 (id int default NULL, pet varchar(10) default NULL) ENGINE=MyISAM;
1277
CREATE TABLE t2 (id int default NULL, pet varchar(10) default NULL);
1251
1278
INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix');
1252
1279
SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id;
1253
1280
drop table t1,t2;
1256
--echo # Aggregate function comparation with ALL/ANY/SOME subselect
1258
CREATE TEMPORARY TABLE `t1` ( `a` int default NULL) ENGINE=MyISAM;
1283
# Aggregate function comparation with ALL/ANY/SOME subselect
1285
CREATE TABLE `t1` ( `a` int default NULL) ENGINE=MyISAM;
1259
1286
insert into t1 values (1);
1260
CREATE TEMPORARY TABLE `t2` ( `b` int default NULL, `a` int default NULL) ENGINE=MyISAM;
1287
CREATE TABLE `t2` ( `b` int default NULL, `a` int default NULL) ENGINE=MyISAM;
1261
1288
insert into t2 values (1,2);
1262
1289
select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1);
1263
1290
drop table t1,t2;
1266
--echo # BUG#5003 - like in subselect
1293
# BUG#5003 - like in subselect
1268
1295
CREATE TABLE t1(`IZAVORGANG_ID` VARCHAR(11) COLLATE utf8_bin,`KUERZEL` VARCHAR(10) COLLATE utf8_bin,`IZAANALYSEART_ID` VARCHAR(11) COLLATE utf8_bin,`IZAPMKZ_ID` VARCHAR(11) COLLATE utf8_bin);
1269
1296
CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel);
1270
1297
INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001');
1307
1334
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;
1311
--echo # Test of correct maybe_null flag returning by subquwery for temporary table
1314
CREATE TEMPORARY TABLE `t1` ( `master` int NOT NULL default '0', `map` int NOT NULL default '0', `slave` int NOT NULL default '0', `access` int NOT NULL default '0', UNIQUE KEY `access_u` (`master`,`map`,`slave`)) ENGINE=MyISAM;
1338
# Test of correct maybe_null flag returning by subquwery for temporary table
1341
CREATE TABLE `t1` ( `master` int NOT NULL default '0', `map` int NOT NULL default '0', `slave` int NOT NULL default '0', `access` int NOT NULL default '0', UNIQUE KEY `access_u` (`master`,`map`,`slave`));
1315
1342
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);
1316
CREATE TEMPORARY TABLE `t2` ( `id` int NOT NULL default '0', `pid` int NOT NULL default '0', `map` int NOT NULL default '0', `level` int NOT NULL default '0', `title` varchar(255) default NULL, PRIMARY KEY (`id`,`pid`,`map`), KEY `level` (`level`), KEY `id` (`id`,`map`)) ENGINE=MyISAM ;
1343
CREATE TABLE `t2` ( `id` int NOT NULL default '0', `pid` int NOT NULL default '0', `map` int NOT NULL default '0', `level` int NOT NULL default '0', `title` varchar(255) default NULL, PRIMARY KEY (`id`,`pid`,`map`), KEY `level` (`level`), KEY `id` (`id`,`map`)) ;
1317
1344
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');
1318
--error ER_BAD_FIELD_ERROR
1319
1346
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;
1320
1347
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;
1321
1348
drop tables t1,t2;
1324
--echo # Subselect in non-select command just after connection
1351
# Subselect in non-select command just after connection
1326
1353
connect (root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK);
1327
1354
connection root;
1328
1355
set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ;
1331
--echo # primary query with temporary table and subquery with groupping
1333
create temporary table t1 (a int, b int) ENGINE=MyISAM;
1334
create temporary table t2 (a int, b int) ENGINE=MyISAM;
1358
# primary query with temporary table and subquery with groupping
1360
create table t1 (a int, b int);
1361
create table t2 (a int, b int);
1335
1362
insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5);
1336
1363
insert into t2 values (1,3),(2,1);
1337
1364
select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b;
1338
1365
drop table t1, t2;
1341
--echo # Equal operation under row and empty subquery
1368
# Equal operation under row and empty subquery
1343
1370
create table t1 (s1 int,s2 int);
1344
1371
insert into t1 values (20,15);
1345
1372
select * from t1 where (('a',null) <=> (select 'a',s2 from t1 where s1 = 0));
1349
--echo # ALL/ANY with NULL
1351
1378
create table t1 (s1 int);
1352
1379
insert into t1 values (1),(null);
1353
1380
select * from t1 where s1 < all (select s1 from t1);
1354
1381
select s1, s1 < all (select s1 from t1) from t1;
1358
--echo # reference on changable fields from subquery
1385
# reference on changable fields from subquery
1360
1387
CREATE TABLE t1 (
1361
1388
Code char(3) NOT NULL default '',
1362
1389
Name char(52) NOT NULL default '',
1384
1411
Population < 200);
1388
--echo # Test for BUG#7885: Server crash when 'any' subselect compared to
1389
--echo # non-existant field.
1391
create temporary table t1 (a1 int) ENGINE=MyISAM;
1392
create temporary table t2 (b1 int) ENGINE=MyISAM;
1393
--error ER_BAD_FIELD_ERROR
1415
# Test for BUG#7885: Server crash when 'any' subselect compared to
1416
# non-existant field.
1418
create table t1 (a1 int);
1419
create table t2 (b1 int);
1394
1421
select * from t1 where a2 > any(select b1 from t2);
1395
1422
select * from t1 where a1 > any(select b1 from t2);
1396
1423
drop table t1,t2;
1400
--echo # Comparison subquery with * and row
1402
create temporary table t1 (a integer, b integer) ENGINE=MyISAM;
1427
# Comparison subquery with * and row
1429
create table t1 (a integer, b integer);
1403
1430
select (select * from t1) = (select 1,2);
1404
1431
select (select 1,2) = (select * from t1);
1405
--echo # queries whih can be converted to IN
1432
# queries whih can be converted to IN
1406
1433
select row(1,2) = ANY (select * from t1);
1407
1434
select row(1,2) != ALL (select * from t1);
1411
--echo # Comparison subquery and row with nested rows
1413
create temporary table t1 (a integer, b integer) ENGINE=MyISAM;
1414
--error ER_OPERAND_COLUMNS
1438
# Comparison subquery and row with nested rows
1440
create table t1 (a integer, b integer);
1415
1442
select row(1,(2,2)) in (select * from t1 );
1416
--error ER_OPERAND_COLUMNS
1417
1444
select row(1,(2,2)) = (select * from t1 );
1418
--error ER_OPERAND_COLUMNS
1419
1446
select (select * from t1) = row(1,(2,2));
1423
--echo # Forward reference detection
1450
# Forward reference detection
1425
1452
create table t1 (a integer);
1426
1453
insert into t1 values (1);
1427
--error ER_ILLEGAL_REFERENCE
1428
1455
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
1429
--error ER_ILLEGAL_REFERENCE
1430
1457
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
1431
1458
select 1 as xx, 1 = ALL ( select 1 from t1 where 1 = xx );
1432
--error ER_ILLEGAL_REFERENCE
1433
1460
select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
1437
--echo # Test for BUG#8218
1439
CREATE TEMPORARY TABLE t1 (
1440
1467
categoryId int NOT NULL,
1441
1468
courseId int NOT NULL,
1442
1469
startDate datetime NOT NULL,
1443
1470
endDate datetime NOT NULL,
1444
1471
createDate datetime NOT NULL,
1445
1472
modifyDate timestamp NOT NULL,
1446
attributes text NOT NULL)
1473
attributes text NOT NULL
1449
1475
INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
1450
1476
(1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1451
1477
(1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
1539
1562
drop table t1, t2, t3, t4, t5;
1542
--echo # Transformation in left expression of subquery (BUG#8888)
1544
create temporary table t1 (a int) ENGINE=MyISAM;
1565
# Transformation in left expression of subquery (BUG#8888)
1567
create table t1 (a int);
1545
1568
insert into t1 values (1), (2), (3);
1546
1569
SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1);
1550
--echo # single row subqueries and row operations (code covarage improvement)
1552
create temporary table t1 (a int, b int) ENGINE=MyISAM;
1573
# single row subqueries and row operations (code covarage improvement)
1575
create table t1 (a int, b int);
1553
1576
insert into t1 values (1,2);
1554
--error ER_OPERAND_COLUMNS
1555
1578
select 1 = (select * from t1);
1556
--error ER_OPERAND_COLUMNS
1557
1580
select (select * from t1) = 1;
1558
--error ER_OPERAND_COLUMNS
1559
1582
select (1,2) = (select a from t1);
1560
--error ER_OPERAND_COLUMNS
1561
1584
select (select a from t1) = (1,2);
1562
--error ER_OPERAND_COLUMNS
1563
1586
select (1,2,3) = (select * from t1);
1564
--error ER_OPERAND_COLUMNS
1565
1588
select (select * from t1) = (1,2,3);
1569
--echo # Item_int_with_ref check (BUG#10020)
1571
--echo #CREATE TABLE `t1` (
1572
--echo # `itemid` bigint NOT NULL auto_increment,
1573
--echo # `sessionid` bigint default NULL,
1574
--echo # `time` int NOT NULL default '0',
1575
--echo # `data` text collate latin1_general_ci NOT NULL,
1576
--echo # PRIMARY KEY (`itemid`)
1578
--echo #INSERT INTO `t1` VALUES (1, 1, 1, '');
1579
--echo #CREATE TABLE `t2` (
1580
--echo # `sessionid` bigint NOT NULL auto_increment,
1581
--echo # `pid` int NOT NULL default '0',
1582
--echo # `date` int NOT NULL default '0',
1583
--echo # `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
1584
--echo # PRIMARY KEY (`sessionid`)
1586
--echo #INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
1587
--echo #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;
1588
--echo #drop tables t1,t2;
1592
# Item_int_with_ref check (BUG#10020)
1594
#CREATE TABLE `t1` (
1595
# `itemid` bigint NOT NULL auto_increment,
1596
# `sessionid` bigint default NULL,
1597
# `time` int NOT NULL default '0',
1598
# `data` text collate latin1_general_ci NOT NULL,
1599
# PRIMARY KEY (`itemid`)
1601
#INSERT INTO `t1` VALUES (1, 1, 1, '');
1602
#CREATE TABLE `t2` (
1603
# `sessionid` bigint NOT NULL auto_increment,
1604
# `pid` int NOT NULL default '0',
1605
# `date` int NOT NULL default '0',
1606
# `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
1607
# PRIMARY KEY (`sessionid`)
1609
#INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
1610
#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;
1590
--echo # BUG#11821 : Select from subselect using aggregate function on an enum
1592
create temporary table t1 (fld enum('0','1')) ENGINE=MyISAM;
1613
# BUG#11821 : Select from subselect using aggregate function on an enum
1615
create table t1 (fld enum('0','1'));
1593
1616
insert into t1 values ('1');
1594
1617
select * from (select max(fld) from t1) as foo;
1598
--echo # Bug #11867: queries with ROW(,elems>) IN (SELECT DISTINCT <cols> FROM ...)
1621
# Bug #11867: queries with ROW(,elems>) IN (SELECT DISTINCT <cols> FROM ...)
1601
1624
CREATE TABLE t1 (one int, two int, flag char(1));
1602
1625
CREATE TABLE t2 (one int, two int, flag char(1));
2074
2096
('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8);
2076
2098
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
2077
--replace_column 9 #
2079
2100
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
2081
2102
ALTER TABLE t1 ADD INDEX(a);
2083
2104
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
2084
--replace_column 9 #
2086
2106
SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
2091
--echo # Bug#17366: Unchecked Item_int results in server crash
2111
# Bug#17366: Unchecked Item_int results in server crash
2093
2113
create table t1( f1 int,f2 int);
2094
2114
insert into t1 values (1,1),(2,2);
2095
2115
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';
2099
--echo # Bug #18306: server crash on delete using subquery.
2119
# Bug #18306: server crash on delete using subquery.
2102
create temporary table t1 (c int, key(c)) ENGINE=MyISAM;
2122
create table t1 (c int, key(c));
2103
2123
insert into t1 values (1142477582), (1142455969);
2104
create temporary table t2 (a int, b int) ENGINE=MyISAM;
2124
create table t2 (a int, b int);
2105
2125
insert into t2 values (2, 1), (1, 0);
2106
2126
delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
2107
2127
drop table t1, t2;
2110
--echo # Bug#19077: A nested materialized derived table is used before being populated.
2130
# Bug#19077: A nested materialized derived table is used before being populated.
2112
2132
create table t1 (i int, j bigint);
2113
2133
insert into t1 values (1, 2), (2, 2), (3, 2);
2114
2134
select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
2118
--echo # Bug#19700: subselect returning BIGINT always returned it as SIGNED
2120
CREATE TEMPORARY TABLE t1 (i BIGINT) ENGINE=MyISAM;
2138
# Bug#19700: subselect returning BIGINT always returned it as SIGNED
2140
CREATE TABLE t1 (i BIGINT);
2121
2141
INSERT INTO t1 VALUES (10000000000000000); # > MAX SIGNED BIGINT 9323372036854775807
2122
2142
INSERT INTO t1 VALUES (1);
2124
CREATE TEMPORARY TABLE t2 (i BIGINT) ENGINE=MyISAM;
2144
CREATE TABLE t2 (i BIGINT);
2125
2145
INSERT INTO t2 VALUES (10000000000000000); # same as first table
2126
2146
INSERT INTO t2 VALUES (1);
2633
2652
DROP TABLE t1,t2;
2637
--echo # Bug #27333: subquery grouped for aggregate of outer query / no aggregate
2638
--echo # of subquery
2640
CREATE TEMPORARY TABLE t1 (a INTEGER, b INTEGER) ENGINE=MyISAM;
2641
CREATE TEMPORARY TABLE t2 (x INTEGER) ENGINE=MyISAM;
2656
# Bug #27333: subquery grouped for aggregate of outer query / no aggregate
2659
CREATE TABLE t1 (a INTEGER, b INTEGER);
2660
CREATE TABLE t2 (x INTEGER);
2642
2661
INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
2643
2662
INSERT INTO t2 VALUES (1), (2);
2645
--echo # wasn't failing, but should
2664
# wasn't failing, but should
2646
2665
--error ER_SUBQUERY_NO_1_ROW
2647
2666
SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
2649
--echo # fails as it should
2668
# fails as it should
2650
2669
--error ER_SUBQUERY_NO_1_ROW
2651
2670
SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
2653
2672
SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
2654
2673
DROP TABLE t1,t2;
2656
--echo # second test case from 27333
2675
# second test case from 27333
2657
2676
CREATE TABLE t1 (a INT, b INT);
2658
2677
INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2);
2660
--echo # returns no rows, when it should
2679
# returns no rows, when it should
2661
2680
SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1
2662
2681
AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a)
2666
--echo #test cases from 29297
2667
CREATE TEMPORARY TABLE t1 (a INT) ENGINE=MyISAM;
2668
CREATE TEMPORARY TABLE t2 (a INT) ENGINE=MyISAM;
2685
#test cases from 29297
2686
CREATE TABLE t1 (a INT);
2687
CREATE TABLE t2 (a INT);
2669
2688
INSERT INTO t1 VALUES (1),(2);
2670
2689
INSERT INTO t2 VALUES (1),(2);
2671
2690
SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;