📄 subselect.result
字号:
select * from t1;a b1 12 NULL3 1drop table t1, t2;CREATE TABLE `t1` (`id` mediumint(8) unsigned NOT NULL auto_increment,`pseudo` varchar(35) NOT NULL default '',`email` varchar(60) NOT NULL default '',PRIMARY KEY (`id`),UNIQUE KEY `email` (`email`),UNIQUE KEY `pseudo` (`pseudo`)) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC;INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1);a btest testtest1 test1drop table if exists t1;(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);a1create table t1 (a int not null, b int, primary key (a));create table t2 (a int not null, primary key (a));create table t3 (a int not null, b int, primary key (a));insert into t1 values (1,10), (2,20), (3,30), (4,40);insert into t2 values (2), (3), (4), (5);insert into t3 values (10,3), (20,4), (30,5);select * from t2 where t2.a in (select a from t1);a234explain extended select * from t2 where t2.a in (select a from t1);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t2 index NULL PRIMARY 4 NULL 4 Using where; Using index2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 Using indexWarnings:Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`a`,<exists>(<primary_index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on PRIMARY)))select * from t2 where t2.a in (select a from t1 where t1.b <> 30);a24explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t2 index NULL PRIMARY 4 NULL 4 Using where; Using index2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 Using index; Using whereWarnings:Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`a`,<exists>(<primary_index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on PRIMARY where (`test`.`t1`.`b` <> 30))))select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);a23explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t2 index NULL PRIMARY 4 NULL 4 Using where; Using index2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 func 1 Using where2 DEPENDENT SUBQUERY t3 eq_ref PRIMARY PRIMARY 4 test.t1.b 1 Using where; Using indexWarnings:Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`a`,<exists>(select 1 AS `Not_used` from `test`.`t1` join `test`.`t3` where ((`test`.`t3`.`a` = `test`.`t1`.`b`) and (<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`))))drop table t1, t2, t3;create table t1 (a int, b int, index a (a,b));create table t2 (a int, index a (a));create table t3 (a int, b int, index a (a));insert into t1 values (1,10), (2,20), (3,30), (4,40);insert into t2 values (2), (3), (4), (5);insert into t3 values (10,3), (20,4), (30,5);select * from t2 where t2.a in (select a from t1);a234explain extended select * from t2 where t2.a in (select a from t1);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t2 index NULL a 5 NULL 4 Using where; Using index2 DEPENDENT SUBQUERY t1 index_subquery a a 5 func 1001 Using indexWarnings:Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on a)))select * from t2 where t2.a in (select a from t1 where t1.b <> 30);a24explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t2 index NULL a 5 NULL 4 Using where; Using index2 DEPENDENT SUBQUERY t1 index_subquery a a 5 func 1001 Using index; Using whereWarnings:Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on a where (`test`.`t1`.`b` <> 30))))select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);a23explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t2 index NULL a 5 NULL 4 Using where; Using index2 DEPENDENT SUBQUERY t3 index a a 5 NULL 3 Using index2 DEPENDENT SUBQUERY t1 ref a a 10 func,test.t3.a 1167 Using where; Using indexWarnings:Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`a`,<exists>(select 1 AS `Not_used` from `test`.`t1` join `test`.`t3` where ((`test`.`t1`.`b` = `test`.`t3`.`a`) and (<cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`))))insert into t1 values (3,31);select * from t2 where t2.a in (select a from t1 where t1.b <> 30);a234select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31);a24explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t2 index NULL a 5 NULL 4 Using where; Using index2 DEPENDENT SUBQUERY t1 index_subquery a a 5 func 1001 Using index; Using whereWarnings:Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on a where (`test`.`t1`.`b` <> 30))))drop table t1, t2, t3;create table t1 (a int, b int);create table t2 (a int, b int);create table t3 (a int, b int);insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);insert into t3 values (3,3), (2,2), (1,1);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;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)3 12 21 2drop table t1,t2,t3;create table t1 (s1 int);create table t2 (s1 int);insert into t1 values (1);insert into t2 values (1);select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);s11drop table t1,t2;create table t1 (s1 int);create table t2 (s1 int);insert into t1 values (1);insert into t2 values (1);update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);ERROR 42S22: Unknown column 'x.s1' in 'field list'DROP TABLE t1, t2;CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,s2 CHAR(5) COLLATE latin1_swedish_ci);INSERT INTO t1 VALUES ('z','?');select * from t1 where s1 > (select max(s2) from t1);ERROR HY000: Illegal mix of collations (latin1_german1_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '>'select * from t1 where s1 > any (select max(s2) from t1);ERROR HY000: Illegal mix of collations (latin1_german1_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '>'drop table t1;create table t1(toid int,rd int);create table t2(userid int,pmnew int,pmtotal int);insert into t2 values(1,0,0),(2,0,0);insert into t1 values(1,0),(1,0),(1,0),(1,12),(1,15),(1,123),(1,12312),(1,12312),(1,123),(2,0),(2,0),(2,1),(2,2);select userid,pmtotal,pmnew, (select count(rd) from t1 where toid=t2.userid) calc_total, (select count(rd) from t1 where rd=0 and toid=t2.userid) calc_new from t2 where userid in (select distinct toid from t1);userid pmtotal pmnew calc_total calc_new1 0 0 9 32 0 0 4 2drop table t1, t2;create table t1 (s1 char(5));select (select 'a','b' from t1 union select 'a','b' from t1) from t1;ERROR 21000: Operand should contain 1 column(s)insert into t1 values ('tttt');select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1);s1ttttexplain extended (select * from t1);id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 system NULL NULL NULL NULL 1 Warnings:Note 1003 (select `test`.`t1`.`s1` AS `s1` from `test`.`t1`)(select * from t1);s1ttttdrop table t1;create table t1 (s1 char(5), index s1(s1));create table t2 (s1 char(5), index s1(s1));insert into t1 values ('a1'),('a2'),('a3');insert into t2 values ('a1'),('a2');select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;s1 s1 NOT IN (SELECT s1 FROM t2)a1 0a2 0a3 1select s1, s1 = ANY (SELECT s1 FROM t2) from t1;s1 s1 = ANY (SELECT s1 FROM t2)a1 1a2 1a3 0select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;s1 s1 <> ALL (SELECT s1 FROM t2)a1 0a2 0a3 1select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;s1 s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')a1 0a2 1a3 1explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t1 index NULL s1 6 NULL 3 Using index2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 Using indexWarnings: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)))) AS `s1 NOT IN (SELECT s1 FROM t2)` from `test`.`t1`explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t1 index NULL s1 6 NULL 3 Using index2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 Using indexWarnings: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))) AS `s1 = ANY (SELECT s1 FROM t2)` from `test`.`t1`explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t1 index NULL s1 6 NULL 3 Using index2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 Using indexWarnings: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)))) AS `s1 <> ALL (SELECT s1 FROM t2)` from `test`.`t1`explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t1 index NULL s1 6 NULL 3 Using index2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 1 Using index; Using whereWarnings: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` < _latin1'a2'))))) AS `s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')` from `test`.`t1`drop table t1,t2;create table t2 (a int, b int);create table t3 (a int);insert into t3 values (6),(7),(3);select * from t3 where a >= all (select b from t2);a673explain extended select * from t3 where a >= all (select b from t2);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t3 ALL NULL NULL NULL NULL 3 Using where2 SUBQUERY t2 system NULL NULL NULL NULL 0 const row not foundWarnings:Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>((`test`.`t3`.`a` < (select max(`test`.`t2`.`b`) from `test`.`t2`)))select * from t3 where a >= some (select b from t2);aexplain extended select * from t3 where a >= some (select b from t2);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t3 ALL NULL NULL NULL NULL 3 Using where2 SUBQUERY t2 system NULL NULL NULL NULL 0 const row not foundWarnings:Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>((`test`.`t3`.`a` >= (select min(`test`.`t2`.`b`) from `test`.`t2`)))select * from t3 where a >= all (select b from t2 group by 1);a673explain extended select * from t3 where a >= all (select b from t2 group by 1);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t3 ALL NULL NULL NULL NULL 3 Using where2 SUBQUERY t2 system NULL NULL NULL NULL 0 const row not foundWarnings:Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>((`test`.`t3`.`a` < <max>(select `test`.`t2`.`b` AS `b` from `test`.`t2` group by 1)))select * from t3 where a >= some (select b from t2 group by 1);aexplain extended select * from t3 where a >= some (select b from t2 group by 1);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t3 ALL NULL NULL NULL NULL 3 Using where2 SUBQUERY t2 system NULL NULL NULL NULL 0 const row not foundWarnings:Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>((`test`.`t3`.`a` >= <min>(select `test`.`t2`.`b` AS `b` from `test`.`t2` group by 1)))select * from t3 where NULL >= any (select b from t2);aexplain extended select * from t3 where NULL >= any (select b from t2);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE2 SUBQUERY t2 system NULL NULL NULL NULL 0 const row not foundWarnings:Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3`select * from t3 where NULL >= any (select b from t2 group by 1);aexplain extended select * from t3 where NULL >= any (select b from t2 group by 1);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE2 SUBQUERY t2 system NULL NULL NULL NULL 0 const row not foundWarnings:Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3`select * from t3 where NULL >= some (select b from t2);aexplain extended select * from t3 where NULL >= some (select b from t2);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE2 SUBQUERY t2 system NULL NULL NULL NULL 0 const row not foundWarnings:Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3`select * from t3 where NULL >= some (select b from t2 group by 1);aexplain extended select * from t3 where NULL >= some (select b from t2 group by 1);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE2 SUBQUERY t2 system NULL NULL NULL NULL 0 const row not foundWarnings:Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3`insert into t2 values (2,2), (2,1), (3,3), (3,1);select * from t3 where a > all (select max(b) from t2 group by a);a67explain extended select * from t3 where a > all (select max(b) from t2 group by a);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t3 ALL NULL NULL NULL NULL 3 Using where2 SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using temporary; Using filesortWarnings: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`)))drop table t2, t3;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 ;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());CREATE TABLE `t2` (`
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -