910
899
2 DEPENDENT SUBQUERY t2 ref_or_null a a 5 func 2 100.00 Using index
911
900
2 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer
913
Note 1003 select "test"."t1"."a" AS "a",<in_optimizer>("test"."t1"."a",<exists>(select 1 AS "Not_used" 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"
902
Note 1003 select `test`.`t1`.`a` AS `a`,<in_optimizer>(`test`.`t1`.`a`,<exists>(select 1 AS `Not_used` 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`
914
903
drop table t1,t2,t3;
915
create table t1 (a float);
916
select 10.5 IN (SELECT * from t1 LIMIT 1);
917
ERROR 42000: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
918
select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
919
ERROR 42000: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
921
create table t1 (a int, b int, c varchar(10));
922
create table t2 (a int);
923
insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
924
insert into t2 values (1),(2),(NULL);
925
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;
926
a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a') (select c from t1 where a=t2.a)
930
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;
931
a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b') (select c from t1 where a=t2.a)
935
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;
936
a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c') (select c from t1 where a=t2.a)
941
create table t1 (a int, b real, c varchar(10));
942
insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
943
select ROW(1, 1, 'a') IN (select a,b,c from t1);
944
ROW(1, 1, 'a') IN (select a,b,c from t1)
946
select ROW(1, 2, 'a') IN (select a,b,c from t1);
947
ROW(1, 2, 'a') IN (select a,b,c from t1)
949
select ROW(1, 1, 'a') IN (select b,a,c from t1);
950
ROW(1, 1, 'a') IN (select b,a,c from t1)
952
select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
953
ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null)
955
select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
956
ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null)
958
select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
959
ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null)
961
select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
962
ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a')
964
select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
965
ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a')
967
select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
968
ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a')
970
select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
971
ERROR 42000: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
973
CREATE TABLE t1 (a int(1));
904
#LIMIT is not supported now
905
#create table t1 (a float) ENGINE=MyISAM;
907
#select 10.5 IN (SELECT * from t1 LIMIT 1);
909
#select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
912
#create table t1 (a int, b int, c varchar(10)) ENGINE=MyISAM;
913
#create table t2 (a int) ENGINE=MyISAM;
914
#insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
915
#insert into t2 values (1),(2),(NULL);
916
#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;
917
#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;
918
#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;
921
#create table t1 (a int, b real, c varchar(10)) ENGINE=MyISAM;
922
#insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
923
#select ROW(1, 1, 'a') IN (select a,b,c from t1);
924
#select ROW(1, 2, 'a') IN (select a,b,c from t1);
925
#select ROW(1, 1, 'a') IN (select b,a,c from t1);
926
#select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
927
#select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
928
#select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
929
#select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
930
#select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
931
#select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
933
#select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
935
#test of uncacheable subqueries
936
CREATE TABLE t1 (a int);
974
937
EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
975
938
id select_type table type possible_keys key key_len ref rows filtered Extra
976
1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 const row not found
977
2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 const row not found
979
Note 1003 select (select rand() AS "RAND()" from "test"."t1") AS "(SELECT RAND() FROM t1)" from "test"."t1"
980
EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1;
981
id select_type table type possible_keys key key_len ref rows filtered Extra
982
1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 const row not found
983
2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 const row not found
985
Note 1003 select (select encrypt('test') AS "ENCRYPT('test')" from "test"."t1") AS "(SELECT ENCRYPT('test') FROM t1)" from "test"."t1"
939
1 PRIMARY t1 ALL NULL NULL NULL NULL 1 100.00
940
2 SUBQUERY t1 ALL NULL NULL NULL NULL 1 100.00
942
Note 1003 select (select rand() AS `RAND()` from `test`.`t1`) AS `(SELECT RAND() FROM t1)` from `test`.`t1`
986
943
EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
987
944
id select_type table type possible_keys key key_len ref rows filtered Extra
988
1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 const row not found
989
2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 const row not found
945
1 PRIMARY t1 ALL NULL NULL NULL NULL 1 100.00
946
2 SUBQUERY t1 ALL NULL NULL NULL NULL 1 100.00
991
Note 1003 select (select benchmark(1,1) AS "BENCHMARK(1,1)" from "test"."t1") AS "(SELECT BENCHMARK(1,1) FROM t1)" from "test"."t1"
948
Note 1003 select (select benchmark(1,1) AS `BENCHMARK(1,1)` from `test`.`t1`) AS `(SELECT BENCHMARK(1,1) FROM t1)` from `test`.`t1`
994
`mot` varchar(30) character set latin1 NOT NULL default '',
995
`topic` mediumint(8) unsigned NOT NULL default '0',
996
`date` date NOT NULL default '0000-00-00',
997
`pseudo` varchar(35) character set latin1 NOT NULL default '',
998
PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
999
KEY `pseudo` (`pseudo`,`date`,`topic`),
1000
KEY `topic` (`topic`)
1001
) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
1003
`mot` varchar(30) character set latin1 NOT NULL default '',
1004
`topic` mediumint(8) unsigned NOT NULL default '0',
1005
`date` date NOT NULL default '0000-00-00',
1006
`pseudo` varchar(35) character set latin1 NOT NULL default '',
1007
PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
1008
KEY `pseudo` (`pseudo`,`date`,`topic`),
1009
KEY `topic` (`topic`)
1010
) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
1012
`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
1013
`maxnumrep` int(10) unsigned NOT NULL default '0',
950
CREATE TEMPORARY TABLE `t1` (
951
`mot` varchar(30) NOT NULL default '',
952
`topic` bigint NOT NULL default '0',
953
`date` date NULL DEFAULT '2009-01-20',
954
`pseudo` varchar(35) NOT NULL default '',
955
PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
956
KEY `pseudo` (`pseudo`,`date`,`topic`),
957
KEY `topic` (`topic`)
958
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
959
CREATE TEMPORARY TABLE `t2` (
960
`mot` varchar(30) NOT NULL default '',
961
`topic` bigint NOT NULL default '0',
962
`date` date NULL default '1997-08-29',
963
`pseudo` varchar(35) NOT NULL default '',
964
PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
965
KEY `pseudo` (`pseudo`,`date`,`topic`),
966
KEY `topic` (`topic`)
967
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
968
CREATE TEMPORARY TABLE `t3` (
969
`numeropost` bigint NOT NULL auto_increment,
970
`maxnumrep` int NOT NULL default '0',
1014
971
PRIMARY KEY (`numeropost`),
1015
972
UNIQUE KEY `maxnumrep` (`maxnumrep`)
1016
) ENGINE=MyISAM CHARSET=latin1;
1017
974
INSERT INTO t1 (mot, topic, pseudo) VALUES ('joce','1','joce'),('test','2','test');
1018
975
INSERT INTO t2 (mot, topic, pseudo) VALUES ('joce','1','joce'),('test','2','test');
1019
976
INSERT INTO t3 VALUES (1,1);
1024
981
select * from t1;
1025
982
mot topic date pseudo
1026
joce 1 0000-00-00 joce
1027
test 2 0000-00-00 test
983
joce 1 2009-01-20 joce
984
test 2 2009-01-20 test
1028
985
DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT
1029
986
EXISTS(SELECT * FROM t3 WHERE numeropost=topic));
1030
987
select * from t1;
1031
988
mot topic date pseudo
1032
joce 1 0000-00-00 joce
989
joce 1 2009-01-20 joce
1033
990
drop table t1, t2, t3;
1034
991
SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
1037
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
1038
SHOW CREATE TABLE t1;
1040
t1 CREATE TABLE "t1" (
1041
"a" int(1) NOT NULL ON UPDATE CURRENT_TIMESTAMP,
1042
"(SELECT 1)" int(1) NOT NULL ON UPDATE CURRENT_TIMESTAMP
1043
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1045
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
1046
SHOW CREATE TABLE t1;
1048
t1 CREATE TABLE "t1" (
1049
"a" int(1) NOT NULL ON UPDATE CURRENT_TIMESTAMP,
1050
"(SELECT a)" int(1) NOT NULL ON UPDATE CURRENT_TIMESTAMP
1051
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1053
CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
1054
SHOW CREATE TABLE t1;
1056
t1 CREATE TABLE "t1" (
1057
"a" int(1) NOT NULL ON UPDATE CURRENT_TIMESTAMP,
1058
"(SELECT a+0)" int(3) NOT NULL ON UPDATE CURRENT_TIMESTAMP
1059
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1061
CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
994
CREATE TEMPORARY TABLE t1 ENGINE=MyISAM SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
995
SHOW CREATE TABLE t1;
997
t1 CREATE TEMPORARY TABLE `t1` (
998
`a` int NOT NULL DEFAULT '0',
999
`(SELECT 1)` int NOT NULL DEFAULT '0'
1002
CREATE TEMPORARY TABLE t1 ENGINE=MyISAM SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
1003
SHOW CREATE TABLE t1;
1005
t1 CREATE TEMPORARY TABLE `t1` (
1006
`a` int NOT NULL DEFAULT '0',
1007
`(SELECT a)` int NOT NULL DEFAULT '0'
1010
CREATE TEMPORARY TABLE t1 ENGINE=MyISAM SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
1011
SHOW CREATE TABLE t1;
1013
t1 CREATE TEMPORARY TABLE `t1` (
1014
`a` int NOT NULL DEFAULT '0',
1015
`(SELECT a+0)` int NOT NULL DEFAULT '0'
1018
CREATE TEMPORARY TABLE t1 ENGINE=MyISAM SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
1062
1019
select * from t1;
1065
1022
SHOW CREATE TABLE t1;
1066
1023
Table Create Table
1067
t1 CREATE TABLE "t1" (
1068
"a" bigint(20) NOT NULL ON UPDATE CURRENT_TIMESTAMP
1069
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1024
t1 CREATE TEMPORARY TABLE `t1` (
1071
1028
create table t1 (a int);
1072
1029
insert into t1 values (1), (2), (3);
1073
1030
explain extended select a,(select (select rand() from t1 limit 1) from t1 limit 1)
1075
1032
id select_type table type possible_keys key key_len ref rows filtered Extra
1076
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
1077
2 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00
1078
3 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00
1033
1 PRIMARY t1 ALL NULL NULL NULL NULL # 100.00
1034
2 SUBQUERY t1 ALL NULL NULL NULL NULL # 100.00
1035
3 SUBQUERY t1 ALL NULL NULL NULL NULL # 100.00
1080
Note 1003 select "test"."t1"."a" AS "a",(select (select rand() AS "rand()" from "test"."t1" limit 1) AS "(select rand() from t1 limit 1)" from "test"."t1" limit 1) AS "(select (select rand() from t1 limit 1) from t1 limit 1)" from "test"."t1"
1037
Note 1003 select `test`.`t1`.`a` AS `a`,(select (select rand() AS `rand()` from `test`.`t1` limit 1) AS `(select rand() from t1 limit 1)` from `test`.`t1` limit 1) AS `(select (select rand() from t1 limit 1) from t1 limit 1)` from `test`.`t1`
1082
1042
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);
1083
1043
ERROR 42S02: Table 'test.t1' doesn't exist
1084
1047
CREATE TABLE t1 (
1085
ID int(11) NOT NULL auto_increment,
1048
ID int NOT NULL auto_increment,
1086
1049
name char(35) NOT NULL default '',
1087
1050
t2 char(3) NOT NULL default '',
1088
1051
District char(20) NOT NULL default '',
1089
Population int(11) NOT NULL default '0',
1052
Population int NOT NULL default '0',
1090
1053
PRIMARY KEY (ID)
1092
1055
INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);
1093
1056
INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);
1094
1057
INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);
1098
1061
Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
1099
1062
Region char(26) NOT NULL default '',
1100
1063
SurfaceArea float(10,2) NOT NULL default '0.00',
1101
IndepYear smallint(6) default NULL,
1102
Population int(11) NOT NULL default '0',
1064
IndepYear int default NULL,
1065
Population int NOT NULL default '0',
1103
1066
LifeExpectancy float(3,1) default NULL,
1104
1067
GNP float(10,2) default NULL,
1105
1068
GNPOld float(10,2) default NULL,
1106
1069
LocalName char(45) NOT NULL default '',
1107
1070
GovernmentForm char(45) NOT NULL default '',
1108
1071
HeadOfState char(60) default NULL,
1109
Capital int(11) default NULL,
1072
Capital int default NULL,
1110
1073
Code2 char(2) NOT NULL default '',
1111
1074
PRIMARY KEY (Code)
1113
1076
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');
1114
1077
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');
1115
1078
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);
1116
1079
Continent Name Population
1117
1080
Oceania Sydney 3276207
1118
1081
drop table t1, t2;
1120
`id` mediumint(8) unsigned NOT NULL auto_increment,
1121
`pseudo` varchar(35) character set latin1 NOT NULL default '',
1085
CREATE TEMPORARY TABLE `t1` (
1086
`id` bigint NOT NULL auto_increment,
1087
`pseudo` varchar(35) NOT NULL default '',
1122
1088
PRIMARY KEY (`id`),
1123
1089
UNIQUE KEY `pseudo` (`pseudo`)
1124
) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
1090
) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
1125
1091
INSERT INTO t1 (pseudo) VALUES ('test');
1126
1092
SELECT 0 IN (SELECT 1 FROM t1 a);
1127
1093
0 IN (SELECT 1 FROM t1 a)
1160
id int(11) default NULL
1161
) ENGINE=MyISAM CHARSET=latin1;
1162
INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);
1164
id int(11) default NULL,
1165
name varchar(15) default NULL
1166
) ENGINE=MyISAM CHARSET=latin1;
1167
INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita');
1168
update t1, t2 set t2.name='lenka' where t2.id in (select id from t1);
1176
create table t1 (a int, unique index indexa (a));
1126
# correct NULL in <CONSTANT> IN (SELECT ...)
1128
create temporary table t1 (a int, unique index indexa (a)) ENGINE=MyISAM;
1177
1129
insert into t1 values (-1), (-4), (-2), (NULL);
1178
1130
select -10 IN (select a from t1 FORCE INDEX (indexa));
1179
1131
-10 IN (select a from t1 FORCE INDEX (indexa))
1135
# Test optimization for sub selects
1182
1137
create table t1 (id int not null auto_increment primary key, salary int, key(salary));
1183
1138
insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);
1184
1139
explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1);
1185
1140
id select_type table type possible_keys key key_len ref rows filtered Extra
1186
1 PRIMARY t1 ref salary salary 5 const 1 100.00 Using index condition
1187
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
1141
1 PRIMARY t1 ref salary salary 5 const # 100.00 Using where; Using index
1142
2 SUBQUERY NULL NULL NULL NULL NULL NULL # NULL Select tables optimized away
1189
Note 1003 select "test"."t1"."id" AS "id" from "test"."t1" where ("test"."t1"."salary" = (select max("test"."t1"."salary") AS "MAX(salary)" from "test"."t1"))
1144
Note 1003 select `test`.`t1`.`id` AS `id` from `test`.`t1` where (`test`.`t1`.`salary` = (select max(`test`.`t1`.`salary`) AS `MAX(salary)` from `test`.`t1`))
1191
1146
CREATE TABLE t1 (
1192
ID int(10) unsigned NOT NULL auto_increment,
1193
SUB_ID int(3) unsigned NOT NULL default '0',
1194
REF_ID int(10) unsigned default NULL,
1195
REF_SUB int(3) unsigned default '0',
1147
ID int NOT NULL auto_increment,
1148
SUB_ID int NOT NULL default '0',
1149
REF_ID int default NULL,
1150
REF_SUB int default '0',
1196
1151
PRIMARY KEY (ID,SUB_ID),
1197
1152
UNIQUE KEY t1_PK (ID,SUB_ID),
1198
1153
KEY t1_FK (REF_ID,REF_SUB),
1199
1154
KEY t1_REFID (REF_ID)
1200
) ENGINE=MyISAM CHARSET=cp1251;
1201
1156
INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);
1202
1157
SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);
1205
create table t1 (a int, b int);
1206
create table t2 (a int, b int);
1161
# uninterruptable update
1163
create temporary table t1 (a int, b int) ENGINE=MyISAM;
1164
create temporary table t2 (a int, b int) ENGINE=MyISAM;
1207
1165
insert into t1 values (1,0), (2,0), (3,0);
1208
1166
insert into t2 values (1,1), (2,1), (3,1), (2,2);
1209
1167
update ignore t1 set b=(select b from t2 where t1.a=t2.a);
1413
1400
explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
1414
1401
id select_type table type possible_keys key key_len ref rows filtered Extra
1415
1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
1416
2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key
1402
1 PRIMARY t1 index NULL s1 23 NULL 3 100.00 Using index
1403
2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 23 func 2 100.00 Using index; Full scan on NULL key
1418
Note 1003 select "test"."t1"."s1" AS "s1",(not(<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"
1405
Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<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`
1419
1406
explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
1420
1407
id select_type table type possible_keys key key_len ref rows filtered Extra
1421
1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
1422
2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key
1408
1 PRIMARY t1 index NULL s1 23 NULL 3 100.00 Using index
1409
2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 23 func 2 100.00 Using index; Full scan on NULL key
1424
Note 1003 select "test"."t1"."s1" AS "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"
1411
Note 1003 select `test`.`t1`.`s1` AS `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`
1425
1412
explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
1426
1413
id select_type table type possible_keys key key_len ref rows filtered Extra
1427
1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
1428
2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key
1414
1 PRIMARY t1 index NULL s1 23 NULL 3 100.00 Using index
1415
2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 23 func 2 100.00 Using index; Full scan on NULL key
1430
Note 1003 select "test"."t1"."s1" AS "s1",(not(<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"
1417
Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<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`
1431
1418
explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
1432
1419
id select_type table type possible_keys key key_len ref rows filtered Extra
1433
1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
1434
2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Using where; Full scan on NULL key
1420
1 PRIMARY t1 index NULL s1 23 NULL 3 100.00 Using index
1421
2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 23 func 2 100.00 Using index; Using where; Full scan on NULL key
1436
Note 1003 select "test"."t1"."s1" AS "s1",(not(<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"
1423
Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<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`
1437
1424
drop table t1,t2;
1438
create table t2 (a int, b int);
1439
create table t3 (a int);
1426
# correct ALL optimisation
1428
create temporary table t2 (a int, b int) ENGINE=MyISAM;
1429
create temporary table t3 (a int) ENGINE=MyISAM;
1440
1430
insert into t3 values (6),(7),(3);
1441
1431
select * from t3 where a >= all (select b from t2);
1518
1511
1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
1519
1512
2 SUBQUERY t2 ALL NULL NULL NULL NULL 4 100.00 Using temporary; Using filesort
1521
Note 1003 select "test"."t3"."a" AS "a" from "test"."t3" where <not>(("test"."t3"."a" <= <max>(select max("test"."t2"."b") AS "max(b)" from "test"."t2" group by "test"."t2"."a")))
1514
Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>((`test`.`t3`.`a` <= <max>(select max(`test`.`t2`.`b`) AS `max(b)` from `test`.`t2` group by `test`.`t2`.`a`)))
1522
1515
drop table t2, t3;
1523
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 ;
1517
# correct used_tables()
1519
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 ;
1524
1520
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());
1525
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;
1521
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;
1526
1522
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);
1527
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 ;
1523
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 ;
1528
1524
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);
1529
CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
1525
CREATE TEMPORARY TABLE `t4` (`task_id` int NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM;
1530
1526
INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
1531
1527
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;
1532
1528
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')
1603
`id` int(11) NOT NULL auto_increment,
1604
`id_cns` tinyint(3) unsigned NOT NULL default '0',
1616
CREATE TEMPORARY TABLE `t1` (
1617
`id` int NOT NULL auto_increment,
1618
`id_cns` int NOT NULL default '0',
1605
1619
`tipo` enum('','UNO','DUE') NOT NULL default '',
1606
`anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000',
1607
`particolare` mediumint(8) unsigned NOT NULL default '0',
1608
`generale` mediumint(8) unsigned NOT NULL default '0',
1609
`bis` tinyint(3) unsigned NOT NULL default '0',
1620
`anno_dep` int NOT NULL default '0',
1621
`particolare` bigint NOT NULL default '0',
1622
`generale` bigint NOT NULL default '0',
1623
`bis` int NOT NULL default '0',
1610
1624
PRIMARY KEY (`id`),
1611
1625
UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
1612
UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
1626
UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`))
1614
1628
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);
1616
`id` tinyint(3) unsigned NOT NULL auto_increment,
1617
`max_anno_dep` smallint(6) unsigned NOT NULL default '0',
1629
CREATE TEMPORARY TABLE `t2` (
1630
`id` int NOT NULL auto_increment,
1631
`max_anno_dep` int NOT NULL default '0',
1632
PRIMARY KEY (`id`)) ENGINE=MyISAM;
1620
1633
INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
1621
1634
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;
1622
1635
id max_anno_dep PIPPO
1668
1692
explain extended select * from t1 where id not in (select id from t1 where id < 8);
1669
1693
id select_type table type possible_keys key key_len ref rows filtered Extra
1670
1 PRIMARY t1 ALL NULL NULL NULL NULL 12 100.00 Using where
1671
2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 100.00 Using index; Using where
1694
1 PRIMARY t1 ALL NULL NULL NULL NULL # 100.00 Using where
1695
2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func # 100.00 Using index; Using where
1673
Note 1003 select "test"."t1"."id" AS "id","test"."t1"."text" AS "text" from "test"."t1" where (not(<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")))))))
1697
Note 1003 select `test`.`t1`.`id` AS `id`,`test`.`t1`.`text` AS `text` from `test`.`t1` where (not(<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`)))))))
1674
1698
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);
1675
1699
id select_type table type possible_keys key key_len ref rows filtered Extra
1676
1 PRIMARY tt ALL NULL NULL NULL NULL 12 100.00 Using where
1677
2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index
1700
1 PRIMARY tt ALL NULL NULL NULL NULL # 100.00 Using where
1701
2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id # 100.00 Using where; Using index
1679
1703
Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1
1680
Note 1003 select "test"."tt"."id" AS "id","test"."tt"."text" AS "text" from "test"."t1" "tt" where (not(exists(select "test"."t1"."id" AS "id" from "test"."t1" where (("test"."t1"."id" < 8) and ("test"."t1"."id" = "test"."tt"."id")) having ("test"."t1"."id" is not null))))
1704
Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(exists(select `test`.`t1`.`id` AS `id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null))))
1681
1705
insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
1682
create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
1706
create temporary table t2 (id int not null, text varchar(20) not null default '', primary key (id)) ENGINE=MyISAM;
1683
1707
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');
1684
1708
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);
1685
1709
id text id text id text
2324
2378
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;
2325
2379
(select b.x from t1 as b where b.x=a.x)
2327
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`));
2382
# Test of correct maybe_null flag returning by subquwery for temporary table
2385
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;
2328
2386
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);
2329
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`)) ;
2387
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 ;
2330
2388
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');
2331
2389
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;
2332
2390
ERROR 42S22: Unknown column 'b.sc' in 'field list'
2436
# reference on changable fields from subquery
2373
2438
CREATE TABLE t1 (
2374
2439
Code char(3) NOT NULL default '',
2375
2440
Name char(52) NOT NULL default '',
2376
2441
Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
2377
2442
Region char(26) NOT NULL default '',
2378
2443
SurfaceArea float(10,2) NOT NULL default '0.00',
2379
IndepYear smallint(6) default NULL,
2380
Population int(11) NOT NULL default '0',
2444
IndepYear int default NULL,
2445
Population int NOT NULL default '0',
2381
2446
LifeExpectancy float(3,1) default NULL,
2382
2447
GNP float(10,2) default NULL,
2383
2448
GNPOld float(10,2) default NULL,
2384
2449
LocalName char(45) NOT NULL default '',
2385
2450
GovernmentForm char(45) NOT NULL default '',
2386
2451
HeadOfState char(60) default NULL,
2387
Capital int(11) default NULL,
2452
Capital int default NULL,
2388
2453
Code2 char(2) NOT NULL default ''
2390
2455
INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX');
2391
2456
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');
2392
2457
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');
2393
2458
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');
2394
2459
/*!40000 ALTER TABLE t1 ENABLE KEYS */;
2461
Note 1031 Table storage engine for 't1' doesn't have this option
2395
2462
SELECT DISTINCT Continent AS c FROM t1 outr WHERE
2396
2463
Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND
2397
2464
Population < 200);
2401
create table t1 (a1 int);
2402
create table t2 (b1 int);
2469
# Test for BUG#7885: Server crash when 'any' subselect compared to
2470
# non-existant field.
2472
create temporary table t1 (a1 int) ENGINE=MyISAM;
2473
create temporary table t2 (b1 int) ENGINE=MyISAM;
2403
2474
select * from t1 where a2 > any(select b1 from t2);
2404
2475
ERROR 42S22: Unknown column 'a2' in 'IN/ALL/ANY subquery'
2405
2476
select * from t1 where a1 > any(select b1 from t2);
2407
2478
drop table t1,t2;
2408
create table t1 (a integer, b integer);
2480
# Comparison subquery with * and row
2482
create temporary table t1 (a integer, b integer) ENGINE=MyISAM;
2409
2483
select (select * from t1) = (select 1,2);
2410
2484
(select * from t1) = (select 1,2)
2412
2486
select (select 1,2) = (select * from t1);
2413
2487
(select 1,2) = (select * from t1)
2489
# queries whih can be converted to IN
2415
2490
select row(1,2) = ANY (select * from t1);
2416
2491
row(1,2) = ANY (select * from t1)
2459
2541
(2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
2460
2542
(3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
2461
2543
(5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18','');
2463
userId int(11) NOT NULL,
2464
courseId int(11) NOT NULL,
2465
date datetime NOT NULL
2544
CREATE TEMPORARY TABLE t2 (
2545
userId int NOT NULL,
2546
courseId int NOT NULL,
2547
date datetime NOT NULL)
2467
2549
INSERT INTO t2 VALUES (5141,71,'2003-11-18'),
2468
2550
(5141,72,'2003-11-25'),(5141,41,'2004-08-06'),
2469
2551
(5141,52,'2004-08-06'),(5141,53,'2004-08-06'),
2470
2552
(5141,12,'2004-08-06'),(5141,86,'2004-10-21'),
2471
2553
(5141,87,'2004-10-21'),(5141,88,'2004-10-21'),
2472
2554
(5141,89,'2004-10-22'),(5141,51,'2004-10-26');
2474
groupId int(11) NOT NULL,
2475
parentId int(11) NOT NULL,
2555
CREATE TEMPORARY TABLE t3 (
2556
groupId int NOT NULL,
2557
parentId int NOT NULL,
2476
2558
startDate datetime NOT NULL,
2477
2559
endDate datetime NOT NULL,
2478
2560
createDate datetime NOT NULL,
2479
2561
modifyDate timestamp NOT NULL,
2482
2564
INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL);
2484
id int(11) NOT NULL,
2485
groupTypeId int(11) NOT NULL,
2565
CREATE TEMPORARY TABLE t4 (
2567
groupTypeId int NOT NULL,
2486
2568
groupKey varchar(50) NOT NULL,
2489
2571
description text,
2490
2572
createDate datetime NOT NULL,
2491
modifyDate timestamp NOT NULL
2573
modifyDate timestamp NOT NULL)
2493
2575
INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'),
2494
2576
(12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29');
2496
userId int(11) NOT NULL,
2497
groupId int(11) NOT NULL,
2577
CREATE TEMPORARY TABLE t5 (
2578
userId int NOT NULL,
2579
groupId int NOT NULL,
2498
2580
createDate datetime NOT NULL,
2499
modifyDate timestamp NOT NULL
2581
modifyDate timestamp NOT NULL) ENGINE=MyISAM;
2501
2582
INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06');
2503
2584
count(distinct t2.userid) pass,
2564
2651
select (select * from t1) = (1,2,3);
2565
2652
ERROR 21000: Operand should contain 2 column(s)
2568
`itemid` bigint(20) unsigned NOT NULL auto_increment,
2569
`sessionid` bigint(20) unsigned default NULL,
2570
`time` int(10) unsigned NOT NULL default '0',
2571
`type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
2573
`data` text collate latin1_general_ci NOT NULL,
2574
PRIMARY KEY (`itemid`)
2575
) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2576
INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
2578
`sessionid` bigint(20) unsigned NOT NULL auto_increment,
2579
`pid` int(10) unsigned NOT NULL default '0',
2580
`date` int(10) unsigned NOT NULL default '0',
2581
`ip` varchar(15) collate latin1_general_ci NOT NULL default '',
2582
PRIMARY KEY (`sessionid`)
2583
) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
2584
INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
2585
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;
2586
ip count( e.itemid )
2589
create table t1 (fld enum('0','1'));
2655
# Item_int_with_ref check (BUG#10020)
2657
#CREATE TABLE `t1` (
2658
# `itemid` bigint NOT NULL auto_increment,
2659
# `sessionid` bigint default NULL,
2660
# `time` int NOT NULL default '0',
2661
# `data` text collate latin1_general_ci NOT NULL,
2662
# PRIMARY KEY (`itemid`)
2664
#INSERT INTO `t1` VALUES (1, 1, 1, '');
2665
#CREATE TABLE `t2` (
2666
# `sessionid` bigint NOT NULL auto_increment,
2667
# `pid` int NOT NULL default '0',
2668
# `date` int NOT NULL default '0',
2669
# `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
2670
# PRIMARY KEY (`sessionid`)
2672
#INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
2673
#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;
2675
# BUG#11821 : Select from subselect using aggregate function on an enum
2677
create temporary table t1 (fld enum('0','1')) ENGINE=MyISAM;
2590
2678
insert into t1 values ('1');
2591
2679
select * from (select max(fld) from t1) as foo;
2684
# Bug #11867: queries with ROW(,elems>) IN (SELECT DISTINCT <cols> FROM ...)
2595
2686
CREATE TABLE t1 (one int, two int, flag char(1));
2596
2687
CREATE TABLE t2 (one int, two int, flag char(1));
2597
2688
INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
2662
2753
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00
2663
2754
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where
2665
Note 1003 select "test"."t1"."one" AS "one","test"."t1"."two" AS "two",<in_optimizer>(("test"."t1"."one","test"."t1"."two"),<exists>(select "test"."t2"."one" AS "one","test"."t2"."two" AS "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"
2756
Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one` AS `one`,`test`.`t2`.`two` AS `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`
2666
2757
explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
2667
2758
id select_type table type possible_keys key key_len ref rows filtered Extra
2668
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 Start temporary
2669
1 PRIMARY t2 ALL NULL NULL NULL NULL 9 100.00 Using where; End temporary; Using join buffer
2759
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 Using where
2760
2 SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where
2671
Note 1003 select "test"."t1"."one" AS "one","test"."t1"."two" AS "two" from "test"."t1" semi join ("test"."t2") where (("test"."t2"."two" = "test"."t1"."two") and ("test"."t2"."one" = "test"."t1"."one") and ("test"."t2"."flag" = 'N'))
2762
Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` where <in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),(`test`.`t1`.`one`,`test`.`t1`.`two`) in ( <materialize> (select `test`.`t2`.`one` AS `one`,`test`.`t2`.`two` AS `two` from `test`.`t2` where (`test`.`t2`.`flag` = 'N') ), <primary_index_lookup>(`test`.`t1`.`one` in <temporary table> on distinct_key)))
2672
2763
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;
2673
2764
id select_type table type possible_keys key key_len ref rows filtered Extra
2674
2765
1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00
2675
2766
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where; Using temporary; Using filesort
2677
Note 1003 select "test"."t1"."one" AS "one","test"."t1"."two" AS "two",<in_optimizer>(("test"."t1"."one","test"."t1"."two"),<exists>(select "test"."t2"."one" AS "one","test"."t2"."two" AS "two" from "test"."t2" where ("test"."t2"."flag" = '0') group by "test"."t2"."one","test"."t2"."two" having (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"))) and trigcond(<is_not_null_test>("test"."t2"."one")) and trigcond(<is_not_null_test>("test"."t2"."two"))))) AS "test" from "test"."t1"
2768
Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one` AS `one`,`test`.`t2`.`two` AS `two` from `test`.`t2` where (`test`.`t2`.`flag` = '0') group by `test`.`t2`.`one`,`test`.`t2`.`two` having (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`))) and trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`))))) AS `test` from `test`.`t1`
2678
2769
DROP TABLE t1,t2;
2679
CREATE TABLE t1 (a char(5), b char(5));
2771
# Bug #12392: where cond with IN predicate for rows and NULL values in table
2773
CREATE TEMPORARY TABLE t1 (a char(5), b char(5)) ENGINE=MyISAM;
2680
2774
INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa');
2681
2775
SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb'));
2685
CREATE TABLE t1 (a int);
2686
CREATE TABLE t2 (a int, b int);
2687
CREATE TABLE t3 (b int NOT NULL);
2780
# Bug #11479: subquery over left join with an empty inner table
2782
CREATE TEMPORARY TABLE t1 (a int) ENGINE=MyISAM;
2783
CREATE TEMPORARY TABLE t2 (a int, b int) ENGINE=MyISAM;
2784
CREATE TEMPORARY TABLE t3 (b int NOT NULL) ENGINE=MyISAM;
2688
2785
INSERT INTO t1 VALUES (1), (2), (3), (4);
2689
2786
INSERT INTO t2 VALUES (1,10), (3,30);
2690
2787
SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b
2781
2867
0048 1 2006-01-06 12:37:50
2782
2868
0059 1 2006-01-06 12:37:50
2871
# Bug #21180: Subselect with index for both WHERE and ORDER BY
2872
# produces empty result
2784
2874
create table t1(a int, primary key (a));
2785
2875
insert into t1 values (10);
2786
2876
create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b));
2787
2877
insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989');
2788
explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
2878
explain SELECT t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
2789
2879
ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
2790
2880
ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
2791
2881
id select_type table type possible_keys key key_len ref rows Extra
2792
1 PRIMARY t1 system PRIMARY NULL NULL NULL 1
2793
1 PRIMARY r const PRIMARY PRIMARY 4 const 1
2794
2 DEPENDENT SUBQUERY t2 range b b 40 NULL 2 Using index condition
2795
SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
2882
1 PRIMARY t1 const PRIMARY PRIMARY 4 const # Using index
2883
1 PRIMARY r const PRIMARY PRIMARY 4 const #
2884
2 DEPENDENT SUBQUERY t2 ref b b 5 # Using where; Using index
2885
SELECT t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
2796
2886
ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
2797
2887
ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
2800
explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
2890
explain SELECT t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
2801
2891
ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
2802
2892
ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
2803
2893
id select_type table type possible_keys key key_len ref rows Extra
2804
1 PRIMARY t1 system PRIMARY NULL NULL NULL 1
2894
1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1 Using index
2805
2895
1 PRIMARY r const PRIMARY PRIMARY 4 const 1
2806
2 DEPENDENT SUBQUERY t2 range b b 40 NULL 2 Using index condition; Using MRR
2807
SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
2896
2 DEPENDENT SUBQUERY t2 ref b b 5 1 Using where; Using index
2897
SELECT t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
2808
2898
ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
2809
2899
ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
2812
2902
drop table t1,t2;
2904
# Bug #21853: assert failure for a grouping query with
2905
# an ALL/ANY quantified subquery in HAVING
2907
CREATE TEMPORARY TABLE t1 (
2814
2908
field1 int NOT NULL,
2815
2909
field2 int NOT NULL,
2816
2910
field3 int NOT NULL,
2817
PRIMARY KEY (field1,field2,field3)
2911
PRIMARY KEY (field1,field2,field3))
2913
CREATE TEMPORARY TABLE t2 (
2820
2914
fieldA int NOT NULL,
2821
2915
fieldB int NOT NULL,
2822
PRIMARY KEY (fieldA,fieldB)
2916
PRIMARY KEY (fieldA,fieldB))
2824
2918
INSERT INTO t1 VALUES
2825
2919
(1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1);
2826
2920
INSERT INTO t2 VALUES (1,1), (1,2), (1,3);
3277
create table t1 (c int, key(c));
3376
# Bug #18306: server crash on delete using subquery.
3378
create temporary table t1 (c int, key(c)) ENGINE=MyISAM;
3278
3379
insert into t1 values (1142477582), (1142455969);
3279
create table t2 (a int, b int);
3380
create temporary table t2 (a int, b int) ENGINE=MyISAM;
3280
3381
insert into t2 values (2, 1), (1, 0);
3281
3382
delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
3282
3383
drop table t1, t2;
3385
# Bug#19077: A nested materialized derived table is used before being populated.
3283
3387
create table t1 (i int, j bigint);
3284
3388
insert into t1 values (1, 2), (2, 2), (3, 2);
3285
3389
select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
3289
CREATE TABLE t1 (i BIGINT UNSIGNED);
3290
INSERT INTO t1 VALUES (10000000000000000000);
3394
# Bug#19700: subselect returning BIGINT always returned it as SIGNED
3396
CREATE TEMPORARY TABLE t1 (i BIGINT) ENGINE=MyISAM;
3397
INSERT INTO t1 VALUES (10000000000000000);
3291
3398
INSERT INTO t1 VALUES (1);
3292
CREATE TABLE t2 (i BIGINT UNSIGNED);
3293
INSERT INTO t2 VALUES (10000000000000000000);
3399
CREATE TEMPORARY TABLE t2 (i BIGINT) ENGINE=MyISAM;
3400
INSERT INTO t2 VALUES (10000000000000000);
3294
3401
INSERT INTO t2 VALUES (1);
3295
3402
/* simple test */
3296
3403
SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i;
3298
10000000000000000000
3300
3407
/* subquery test */
3301
3408
SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
3303
10000000000000000000
3304
3411
/* subquery test with cast*/
3305
SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);
3412
SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
3307
10000000000000000000
3311
id bigint(20) unsigned NOT NULL auto_increment,
3418
# Bug#20519: subselect with LIMIT M, N
3420
CREATE TEMPORARY TABLE t1 (
3421
id bigint NOT NULL auto_increment,
3312
3422
name varchar(255) NOT NULL,
3315
3425
INSERT INTO t1 VALUES
3316
3426
(1, 'Balazs'), (2, 'Joe'), (3, 'Frank');
3317
3427
CREATE TABLE t2 (
3318
id bigint(20) unsigned NOT NULL auto_increment,
3319
mid bigint(20) unsigned NOT NULL,
3428
id bigint NOT NULL auto_increment,
3429
mid bigint NOT NULL,
3320
3430
date date NOT NULL,
3323
3432
INSERT INTO t2 VALUES
3324
3433
(1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'),
3325
3434
(4, 2, '2006-04-20'), (5, 1, '2006-05-01');
3388
3498
i1 i2 t i1 i2 t
3389
3499
24 1 2005-05-27 12:40:30 24 1 2006-06-20 12:29:40
3390
3500
DROP TABLE t1, t2;
3502
# Bug#14654 : Cannot select from the same table twice within a UNION
3391
3505
CREATE TABLE t1 (i INT);
3392
3506
(SELECT i FROM t1) UNION (SELECT i FROM t1);
3394
SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS
3508
SELECT * FROM t1 WHERE NOT EXISTS
3396
3510
(SELECT i FROM t1) UNION
3397
3511
(SELECT i FROM t1)
3400
3515
SELECT * FROM t1
3401
3516
WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
3402
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION (SELECT i FROM t1)))' at line 1
3517
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'UNION (SELECT i FROM t1)))' at line 1
3403
3519
explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12))
3405
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'union (select t12.i from t1 t12))
3521
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'union (select t12.i from t1 t12))
3406
3522
from t1' at line 1
3407
3523
explain select * from t1 where not exists
3408
3524
((select t11.i from t1 t11) union (select t12.i from t1 t12));
3409
3525
id select_type table type possible_keys key key_len ref rows Extra
3410
1 PRIMARY t1 system NULL NULL NULL NULL 0 const row not found
3411
2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
3412
3 UNION NULL NULL NULL NULL NULL NULL NULL no matching row in const table
3413
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
3415
CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b));
3416
insert into t1 (a) values (FLOOR(rand() * 100));
3417
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3418
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3419
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3420
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3421
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3422
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3423
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3424
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3425
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3426
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3427
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3428
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3429
insert into t1 (a) select FLOOR(rand() * 100) from t1;
3431
(SELECT REPEAT(' ',250) FROM t1 i1
3432
WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a
3433
FROM t1 ORDER BY a LIMIT 5;
3526
1 PRIMARY t1 ALL NULL NULL NULL NULL #
3527
2 SUBQUERY t11 ALL NULL NULL NULL NULL #
3528
3 UNION t12 ALL NULL NULL NULL NULL #
3529
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL #
3532
# Bug #21540: Subqueries with no from and aggregate functions return
3441
3534
CREATE TABLE t1 (a INT, b INT);
3442
3535
CREATE TABLE t2 (a INT);
3443
3536
INSERT INTO t2 values (1);
3928
4050
DROP TABLE t1, t2;
3929
CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5));
3930
INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43);
3931
SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
3933
SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
3935
CREATE INDEX I1 ON t1 (s1);
3936
CREATE INDEX I2 ON t1 (s2);
3937
SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
3939
SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
3942
INSERT INTO t1 VALUES (0x41,0x41);
3943
SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1);
3946
CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1));
3947
CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2));
3948
CREATE TABLE t3 (a3 BINARY(2) default '0');
3949
INSERT INTO t1 VALUES (1),(2),(3),(4);
3950
INSERT INTO t2 VALUES (1),(2),(3);
3951
INSERT INTO t3 VALUES (1),(2),(3);
3952
SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2;
3957
SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1;
3958
a1 t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2)
3963
DROP TABLE t1,t2,t3;
3964
CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3));
3965
CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY);
3966
CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY);
3967
INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40);
3968
INSERT INTO t2 VALUES (2), (3), (4), (5);
3969
INSERT INTO t3 VALUES (10), (20), (30);
3970
SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3;
3975
SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3);
3977
DROP TABLE t1, t2, t3;
4052
# Bug #30788: Inconsistent retrieval of char/varchar
3978
4054
CREATE TABLE t1 (a CHAR(1), b VARCHAR(10));
3979
4055
INSERT INTO t1 VALUES ('a', 'aa');
3980
4056
INSERT INTO t1 VALUES ('a', 'aaa');
3981
4057
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
3983
4061
CREATE INDEX I1 ON t1 (a);
3984
4062
CREATE INDEX I2 ON t1 (b);
3985
4063
EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
3986
4064
id select_type table type possible_keys key key_len ref rows Extra
3987
1 PRIMARY t1 index I1 I1 2 NULL 2 Using index; LooseScan
3988
1 PRIMARY t1 ref I2 I2 13 test.t1.a 2 Using index condition
4065
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
4066
2 SUBQUERY t1 index NULL I1 7 NULL # Using index
3989
4067
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
3991
4071
CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
3992
4072
INSERT INTO t2 SELECT * FROM t1;
3993
4073
CREATE INDEX I1 ON t2 (a);
3994
4074
CREATE INDEX I2 ON t2 (b);
3995
4075
EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
3996
4076
id select_type table type possible_keys key key_len ref rows Extra
3997
1 PRIMARY t2 index I1 I1 4 NULL 2 Using index; LooseScan
3998
1 PRIMARY t2 ref I2 I2 13 test.t2.a 2 Using index condition
4077
1 PRIMARY t2 ALL NULL NULL NULL NULL # Using where
4078
2 SUBQUERY t2 index NULL I1 7 NULL # Using index
3999
4079
SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
4002
4084
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
4003
4085
id select_type table type possible_keys key key_len ref rows Extra
4004
1 PRIMARY t1 index I1 I1 2 NULL 2 Using where; Using index; LooseScan
4005
1 PRIMARY t1 ref I2 I2 13 test.t1.a 2 Using index condition
4086
1 PRIMARY t1 ALL NULL NULL NULL NULL # Using where
4087
2 SUBQUERY t1 index NULL I1 7 NULL # Using where; Using index
4006
4088
SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
4008
4092
DROP TABLE t1,t2;
4094
# Bug #32400: Complex SELECT query returns correct result only on some
4009
4097
CREATE TABLE t1(a INT, b INT);
4010
4098
INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4);
4046
4137
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
4048
4139
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
4049
Note 1003 select 2 AS "2" from "test"."t1" where exists(select 1 AS "1" from "test"."t2" where ("test"."t1"."a" = "test"."t2"."a"))
4140
Note 1003 select 2 AS `2` from `test`.`t1` where exists(select 1 AS `1` from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`))
4050
4141
EXPLAIN EXTENDED
4051
4142
SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
4052
4143
(SELECT 1 FROM t2 WHERE t1.a = t2.a));
4053
4144
id select_type table type possible_keys key key_len ref rows filtered Extra
4054
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
4055
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
4056
3 DEPENDENT UNION t2 ALL NULL NULL NULL NULL 2 100.00 Using where
4057
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
4145
1 PRIMARY t1 ALL NULL NULL NULL NULL # 100.00 Using where
4146
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL # 100.00 Using where
4147
3 DEPENDENT UNION t2 ALL NULL NULL NULL NULL # 100.00 Using where
4148
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL # NULL
4059
4150
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
4060
4151
Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1
4061
Note 1003 select 2 AS "2" from "test"."t1" where exists((select 1 AS "1" from "test"."t2" where ("test"."t1"."a" = "test"."t2"."a")) union (select 1 AS "1" from "test"."t2" where ("test"."t1"."a" = "test"."t2"."a")))
4152
Note 1003 select 2 AS `2` from `test`.`t1` where exists((select 1 AS `1` from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 AS `1` from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)))
4062
4153
DROP TABLE t1,t2;
4155
# BUG#33794 "MySQL crashes executing specific query on specific dump"
4157
CREATE TEMPORARY TABLE t4 (
4064
4158
f7 varchar(32) collate utf8_bin NOT NULL default '',
4065
4159
f10 varchar(32) collate utf8_bin default NULL,
4068
4162
INSERT INTO t4 VALUES(1,1), (2,null);
4069
4163
CREATE TABLE t2 (
4070
4164
f4 varchar(32) collate utf8_bin NOT NULL default '',
4071
4165
f2 varchar(50) collate utf8_bin default NULL,
4072
4166
f3 varchar(10) collate utf8_bin default NULL,
4073
4167
PRIMARY KEY (f4),
4168
UNIQUE KEY uk1 (f2));
4076
4169
INSERT INTO t2 VALUES(1,1,null), (2,2,null);
4170
CREATE TEMPORARY TABLE t1 (
4078
4171
f8 varchar(32) collate utf8_bin NOT NULL default '',
4079
4172
f1 varchar(10) collate utf8_bin default NULL,
4080
4173
f9 varchar(32) collate utf8_bin default NULL,
4083
4176
INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2);
4177
CREATE TEMPORARY TABLE t3 (
4085
4178
f6 varchar(32) collate utf8_bin NOT NULL default '',
4086
4179
f5 varchar(50) collate utf8_bin default NULL,
4089
4182
INSERT INTO t3 VALUES (1,null), (2,null);
4091
4184
IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4,
4479
4593
SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a);
4480
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO @a)' at line 1
4594
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'INTO @a)' at line 1
4481
4595
SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE 'file' );
4482
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
4596
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
4483
4597
SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE 'file' );
4484
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
4598
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
4485
4599
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO @a);
4486
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO @a)' at line 1
4600
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'INTO @a)' at line 1
4487
4601
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
4488
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
4602
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
4489
4603
SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
4490
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
4604
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
4491
4605
SELECT ( SELECT 1 INTO @v );
4492
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO @v )' at line 1
4606
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'INTO @v )' at line 1
4493
4607
SELECT ( SELECT 1 INTO OUTFILE 'file' );
4494
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
4608
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
4495
4609
SELECT ( SELECT 1 INTO DUMPFILE 'file' );
4496
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
4610
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
4497
4611
SELECT ( SELECT 1 UNION SELECT 1 INTO @v );
4498
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO @v )' at line 1
4612
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'INTO @v )' at line 1
4499
4613
SELECT ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
4500
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
4614
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
4501
4615
SELECT ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
4502
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
4616
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your Drizzle server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
4617
# Make sure context is popped when we leave the nested select
4503
4618
SELECT ( SELECT a FROM t1 WHERE a = 1 ), a FROM t1;
4504
4619
( SELECT a FROM t1 WHERE a = 1 ) a