⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subselect.test

📁 这是linux下运行的mysql软件包,可用于linux 下安装 php + mysql + apach 的网络配置
💻 TEST
📖 第 1 页 / 共 5 页
字号:
insert into t1 values (0, 10),(1, 11),(2, 12);insert into t2 values (1, 21),(2, 22),(3, 23);select * from t1;-- error 1093update t1 set b= (select b from t1);-- error 1242update t1 set b= (select b from t2);update t1 set b= (select b from t2 where t1.a = t2.a);select * from t1;drop table t1, t2;#delete with subselectscreate table t1 (a int NOT NULL, b int, primary key (a));create table t2 (a int NOT NULL, b int, primary key (a));insert into t1 values (0, 10),(1, 11),(2, 12);insert into t2 values (1, 21),(2, 12),(3, 23);select * from t1;select * from t1 where b = (select b from t2 where t1.a = t2.a);-- error 1093delete from t1 where b = (select b from t1);-- error 1242delete from t1 where b = (select b from t2);delete from t1 where b = (select b from t2 where t1.a = t2.a);select * from t1;drop table t1, t2;#multi-delete with subselectscreate table t11 (a int NOT NULL, b int, primary key (a));create table t12 (a int NOT NULL, b int, primary key (a));create table t2 (a int NOT NULL, b int, primary key (a));insert into t11 values (0, 10),(1, 11),(2, 12);insert into t12 values (33, 10),(22, 11),(2, 12);insert into t2 values (1, 21),(2, 12),(3, 23);select * from t11;select * from t12;-- error 1093delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);-- error 1242delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);select * from t11;select * from t12;drop table t11, t12, t2;#insert with subselectsCREATE TABLE t1 (x int);create table t2 (a int);create table t3 (b int);insert into t2 values (1);insert into t3 values (1),(2);-- error 1093INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));-- error 1242INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));select * from t1;insert into t2 values (1);INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));-- sleep 1select * from t1;INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;select * from t1;# After this, only data based on old t1 records should have been added.INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;select * from t1;-- error 1054INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));-- sleep 1select * from t1;##TODO: should be uncommented after bug 380 fix pushed#INSERT INTO t1 (x) SELECT (SELECT SUM(a)+b FROM t2) from t3;#select * from t1;drop table t1, t2, t3;#replace with subselectsCREATE 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;-- error 1093replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));-- error 1242replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));select * from t1;replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));select * from t1;replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));-- sleep 1select * from t1;replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));-- sleep 1select * from t1;replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));select * from t1;drop table t1, t2, t3;-- error 1096SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);CREATE 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);EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);-- error 1093INSERT INTO t2 VALUES ((SELECT * FROM t2));-- error 1093INSERT INTO t2 VALUES ((SELECT id FROM t2));SELECT * FROM t2;CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;INSERT INTO t1 values (1),(1);-- error 1242UPDATE t2 SET id=(SELECT * FROM t1);drop table t2, t1;#NULL testcreate table t1 (a int);insert into t1 values (1),(2),(3);select 1 IN (SELECT * from t1);select 10 IN (SELECT * from t1);select NULL IN (SELECT * from t1);update t1 set a=NULL where a=2;select 1 IN (SELECT * from t1);select 3 IN (SELECT * from t1);select 10 IN (SELECT * from t1);select 1 > ALL (SELECT * from t1);select 10 > ALL (SELECT * from t1);select 1 > ANY (SELECT * from t1);select 10 > ANY (SELECT * from t1);drop table t1;create table t1 (a varchar(20));insert into t1 values ('A'),('BC'),('DEF');select 'A' IN (SELECT * from t1);select 'XYZS' IN (SELECT * from t1);select NULL IN (SELECT * from t1);update t1 set a=NULL where a='BC';select 'A' IN (SELECT * from t1);select 'DEF' IN (SELECT * from t1);select 'XYZS' IN (SELECT * from t1);select 'A' > ALL (SELECT * from t1);select 'XYZS' > ALL (SELECT * from t1);select 'A' > ANY (SELECT * from t1);select 'XYZS' > ANY (SELECT * from t1);drop table t1;create table t1 (a float);insert into t1 values (1.5),(2.5),(3.5);select 1.5 IN (SELECT * from t1);select 10.5 IN (SELECT * from t1);select NULL IN (SELECT * from t1);update t1 set a=NULL where a=2.5;select 1.5 IN (SELECT * from t1);select 3.5 IN (SELECT * from t1);select 10.5 IN (SELECT * from t1);select 1.5 > ALL (SELECT * from t1);select 10.5 > ALL (SELECT * from t1);select 1.5 > ANY (SELECT * from t1);select 10.5 > ANY (SELECT * from t1);explain extended select (select a+1) from t1;select (select a+1) from t1;drop table t1;## Null with keys#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;explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM 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;explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;drop table t1,t2,t3;#LIMIT is not supported nowcreate table t1 (a float);-- error 1235select 10.5 IN (SELECT * from t1 LIMIT 1);-- error 1235select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);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;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;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;drop 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);select ROW(1, 2, 'a') IN (select a,b,c from t1);select ROW(1, 1, 'a') IN (select b,a,c from t1);select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');-- error 1235select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);drop table t1;## DO & SET#create table t1 (a int);insert into t1 values (1);do @a:=(SELECT a from t1);select @a;set @a:=2;set @a:=(SELECT a from t1);select @a;drop table t1;-- error 1146do (SELECT a from t1);-- error 1146set @a:=(SELECT a from t1);CREATE TABLE t1 (a int, KEY(a)); HANDLER t1 OPEN;-- error 1064HANDLER t1 READ a=((SELECT 1));HANDLER t1 CLOSE;drop table t1;create table t1 (a int);create table t2 (b int);insert into t1 values (1),(2);insert into t2 values (1);select a from t1 where a in (select a from t1 where a in (select b from t2));drop table t1, t2;create table t1 (a int, b int);create table t2 like t1;insert into t1 values (1,2),(1,3),(1,4),(1,5);insert into t2 values (1,2),(1,3);select * from t1 where row(a,b) in (select a,b from t2);drop table t1, t2;CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY  (`i`)) ENGINE=MyISAM CHARSET=latin1;INSERT INTO t1 VALUES (1);UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));select * from t1;drop table t1;#test of uncacheable subqueriesCREATE TABLE t1 (a int(1));EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1;EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;drop table t1;CREATE TABLE `t1` (  `mot` varchar(30) character set latin1 NOT NULL default '',  `topic` mediumint(8) unsigned NOT NULL default '0',  `date` date NOT NULL default '0000-00-00',  `pseudo` varchar(35) character set latin1 NOT NULL default '',  PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),  KEY `pseudo` (`pseudo`,`date`,`topic`),  KEY `topic` (`topic`)) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;CREATE TABLE `t2` (  `mot` varchar(30) character set latin1 NOT NULL default '',  `topic` mediumint(8) unsigned NOT NULL default '0',  `date` date NOT NULL default '0000-00-00',  `pseudo` varchar(35) character set latin1 NOT NULL default '',  PRIMARY KEY  (`mot`,`pseudo`,`date`,`topic`),  KEY `pseudo` (`pseudo`,`date`,`topic`),  KEY `topic` (`topic`)) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -