subselect.test 3.19 KB
Newer Older
1

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2
select (select 2);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3
drop table if exists t1,t2,t3,t4,t5,attend,clinic;
4 5 6 7 8 9 10
create table t1 (a int);
create table t2 (a int, b int);
create table t3 (a int);
create table t4 (a int, b int);
insert into t1 values (2);
insert into t2 values (1,7),(2,7);
insert into t4 values (4,8),(3,8),(5,9);
11 12
select (select a from t1 where t1.a=t2.a), a from t2;
select (select a from t1 where t1.a=t2.b), a from t2;
13 14 15 16
select (select a from t1), a from t2;
select (select a from t3), a from t2;
select * from t2 where t2.a=(select a from t1);
insert into t3 values (6),(7),(3);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
17 18
select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1);
select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1) 
19
union (select * from t4 order by a limit 2) limit 3;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
20 21
select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1) 
union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
22 23
explain select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1) 
union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
24 25 26
select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from 
(select * from t2 where a>1) as tt;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
27 28
explain select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from 
(select * from t2 where a>1) as tt;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
29 30 31 32
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3) order by 1 desc limit 1);
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a > t1.a) order by 1 desc limit 1);
select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1);
select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
33
explain select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
34 35
select * from t3 where exists (select * from t2 where t2.b=t3.a);
select * from t3 where not exists (select * from t2 where t2.b=t3.a);
36 37 38 39 40
insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9);
select b,max(a) as ma from t4 group by b having b < (select max(t2.a)
from t2 where t2.b=t4.b);
select b,max(a) as ma from t4 group by b having b >= (select max(t2.a)
from t2 where t2.b=t4.b);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
41 42 43 44 45 46
create table t5 (a int);
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
insert into t5 values (5);
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
insert into t5 values (2);
select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
47
explain select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
48 49
-- error 1230
select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
50 51 52 53 54 55
create table attend (patient_uq int, clinic_uq int, index i1 (clinic_uq));
create table clinic( uq int primary key, name char(25));
insert into clinic values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
insert into attend values (1,1),(1,2),(2,2),(1,3);
select * from attend where exists (select * from clinic where uq = clinic_uq);

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
56
drop table t1,t2,t3,t4,t5,attend,clinic;