📄 subselect.result
字号:
create table t2 (a int);create table t3 (b int);insert into t2 values (1);insert into t3 values (1),(2);INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));ERROR HY000: You can't specify target table 't1' for update in FROM clauseINSERT INTO t1 (x) VALUES ((SELECT b FROM t3));ERROR 21000: Subquery returns more than 1 rowINSERT INTO t1 (x) VALUES ((SELECT a FROM t2));select * from t1;x1insert into t2 values (1);INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));select * from t1;x12INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;select * from t1;x1233INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;select * from t1;x12331111INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));ERROR 42S22: Unknown column 'x' in 'field list'INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));select * from t1;x123311112drop table t1, t2, t3;CREATE TABLE t1 (x int not null, y int, primary key (x));create table t2 (a int);create table t3 (a int);insert into t2 values (1);insert into t3 values (1),(2);select * from t1;x yreplace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));ERROR HY000: You can't specify target table 't1' for update in FROM clausereplace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));ERROR 21000: Subquery returns more than 1 rowreplace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));select * from t1;x y1 2replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));select * from t1;x y1 3replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));select * from t1;x y1 34 1replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));select * from t1;x y1 34 2replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));select * from t1;x y1 34 22 1drop table t1, t2, t3;SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);ERROR HY000: No tables usedCREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;INSERT INTO t2 VALUES (1),(2);SELECT * FROM t2 WHERE id IN (SELECT 1);id1EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t2 ref id id 5 const 1 Using where; Using indexWarnings:Note 1249 Select 2 was reduced during optimizationNote 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where (`test`.`t2`.`id` = 1)SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);id1SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));id2EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t2 ref id id 5 const 1 Using where; Using indexWarnings:Note 1249 Select 3 was reduced during optimizationNote 1249 Select 2 was reduced during optimizationNote 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where (`test`.`t2`.`id` = (1 + 1))EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t2 index NULL id 5 NULL 2 Using where; Using index2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables usedNULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL Warnings:Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where <in_optimizer>(`test`.`t2`.`id`,<exists>(select 1 AS `Not_used` having (<cache>(`test`.`t2`.`id`) = <null_helper>(1)) union select 1 AS `Not_used` having (<cache>(`test`.`t2`.`id`) = <null_helper>(3))))SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);idSELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);id2INSERT INTO t2 VALUES ((SELECT * FROM t2));ERROR HY000: You can't specify target table 't2' for update in FROM clauseINSERT INTO t2 VALUES ((SELECT id FROM t2));ERROR HY000: You can't specify target table 't2' for update in FROM clauseSELECT * FROM t2;id12CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;INSERT INTO t1 values (1),(1);UPDATE t2 SET id=(SELECT * FROM t1);ERROR 21000: Subquery returns more than 1 rowdrop table t2, t1;create table t1 (a int);insert into t1 values (1),(2),(3);select 1 IN (SELECT * from t1);1 IN (SELECT * from t1)1select 10 IN (SELECT * from t1);10 IN (SELECT * from t1)0select NULL IN (SELECT * from t1);NULL IN (SELECT * from t1)NULLupdate t1 set a=NULL where a=2;select 1 IN (SELECT * from t1);1 IN (SELECT * from t1)1select 3 IN (SELECT * from t1);3 IN (SELECT * from t1)1select 10 IN (SELECT * from t1);10 IN (SELECT * from t1)NULLselect 1 > ALL (SELECT * from t1);1 > ALL (SELECT * from t1)0select 10 > ALL (SELECT * from t1);10 > ALL (SELECT * from t1)NULLselect 1 > ANY (SELECT * from t1);1 > ANY (SELECT * from t1)NULLselect 10 > ANY (SELECT * from t1);10 > ANY (SELECT * from t1)1drop table t1;create table t1 (a varchar(20));insert into t1 values ('A'),('BC'),('DEF');select 'A' IN (SELECT * from t1);'A' IN (SELECT * from t1)1select 'XYZS' IN (SELECT * from t1);'XYZS' IN (SELECT * from t1)0select NULL IN (SELECT * from t1);NULL IN (SELECT * from t1)NULLupdate t1 set a=NULL where a='BC';select 'A' IN (SELECT * from t1);'A' IN (SELECT * from t1)1select 'DEF' IN (SELECT * from t1);'DEF' IN (SELECT * from t1)1select 'XYZS' IN (SELECT * from t1);'XYZS' IN (SELECT * from t1)NULLselect 'A' > ALL (SELECT * from t1);'A' > ALL (SELECT * from t1)0select 'XYZS' > ALL (SELECT * from t1);'XYZS' > ALL (SELECT * from t1)NULLselect 'A' > ANY (SELECT * from t1);'A' > ANY (SELECT * from t1)NULLselect 'XYZS' > ANY (SELECT * from t1);'XYZS' > ANY (SELECT * from t1)1drop table t1;create table t1 (a float);insert into t1 values (1.5),(2.5),(3.5);select 1.5 IN (SELECT * from t1);1.5 IN (SELECT * from t1)1select 10.5 IN (SELECT * from t1);10.5 IN (SELECT * from t1)0select NULL IN (SELECT * from t1);NULL IN (SELECT * from t1)NULLupdate t1 set a=NULL where a=2.5;select 1.5 IN (SELECT * from t1);1.5 IN (SELECT * from t1)1select 3.5 IN (SELECT * from t1);3.5 IN (SELECT * from t1)1select 10.5 IN (SELECT * from t1);10.5 IN (SELECT * from t1)NULLselect 1.5 > ALL (SELECT * from t1);1.5 > ALL (SELECT * from t1)0select 10.5 > ALL (SELECT * from t1);10.5 > ALL (SELECT * from t1)NULLselect 1.5 > ANY (SELECT * from t1);1.5 > ANY (SELECT * from t1)NULLselect 10.5 > ANY (SELECT * from t1);10.5 > ANY (SELECT * from t1)1explain extended select (select a+1) from t1;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Warnings:Note 1276 Field or reference 'a' of SELECT #2 was resolved in SELECT #1Note 1249 Select 2 was reduced during optimizationNote 1003 select (`test`.`t1`.`a` + 1) AS `(select a+1)` from `test`.`t1`select (select a+1) from t1;(select a+1)2.5NULL4.5drop table t1;CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY (a));CREATE TABLE t2 (a int(11) default '0', INDEX (a));INSERT INTO t1 VALUES (1),(2),(3),(4);INSERT INTO t2 VALUES (1),(2),(3);SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;a t1.a in (select t2.a from t2)1 12 13 14 0explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 Using index2 DEPENDENT SUBQUERY t2 index_subquery a a 5 func 2 Using indexWarnings:Note 1003 select `test`.`t1`.`a` AS `a`,<in_optimizer>(`test`.`t1`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in t2 on a checking NULL))) AS `t1.a in (select t2.a from t2)` from `test`.`t1`CREATE TABLE t3 (a int(11) default '0');INSERT INTO t3 VALUES (1),(2),(3);SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;a t1.a in (select t2.a from t2,t3 where t3.a=t2.a)1 12 13 14 0explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 Using index2 DEPENDENT SUBQUERY t2 ref_or_null a a 5 func 2 Using where; Using index2 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 Using whereWarnings: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`drop table t1,t2,t3;create table t1 (a float);select 10.5 IN (SELECT * from t1 LIMIT 1);ERROR 42000: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);ERROR 42000: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'drop table t1;create table t1 (a int, b int, c varchar(10));create table t2 (a int);insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');insert into t2 values (1),(2),(NULL);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;a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a') (select c from t1 where a=t2.a)1 1 a2 0 bNULL 0 NULLselect 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;a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b') (select c from t1 where a=t2.a)1 0 a2 1 bNULL NULL NULLselect 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;a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c') (select c from t1 where a=t2.a)1 0 a2 0 bNULL 0 NULLdrop table t1,t2;create table t1 (a int, b real, c varchar(10));insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');select ROW(1, 1, 'a') IN (select a,b,c from t1);ROW(1, 1, 'a') IN (select a,b,c from t1)1select ROW(1, 2, 'a') IN (select a,b,c from t1);ROW(1, 2, 'a') IN (select a,b,c from t1)0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -