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

📄 subselect.test

📁 这是linux下运行的mysql软件包,可用于linux 下安装 php + mysql + apach 的网络配置
💻 TEST
📖 第 1 页 / 共 5 页
字号:
CREATE TABLE `t3` (  `numeropost` mediumint(8) unsigned NOT NULL auto_increment,  `maxnumrep` int(10) unsigned NOT NULL default '0',  PRIMARY KEY  (`numeropost`),  UNIQUE KEY `maxnumrep` (`maxnumrep`)) ENGINE=MyISAM CHARSET=latin1;INSERT INTO t1 VALUES ('joce','1','','joce'),('test','2','','test');INSERT INTO t2 VALUES ('joce','1','','joce'),('test','2','','test');INSERT INTO t3 VALUES (1,1);SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHEREnumeropost=topic);select * from t1;DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOTEXISTS(SELECT * FROM t3 WHERE numeropost=topic));select * from t1;drop table t1, t2, t3;SELECT * FROM (SELECT 1 as a,(SELECT a)) a;CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;SHOW CREATE TABLE t1;drop table t1;CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;SHOW CREATE TABLE t1;drop table t1;CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;SHOW CREATE TABLE t1;drop table t1;CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;select * from t1;SHOW CREATE TABLE t1;drop table t1;create table t1 (a int);insert into t1 values (1), (2), (3);explain extended select a,(select (select rand() from t1 limit 1)  from t1 limit 1)from t1;drop table t1;## error in IN#-- error 1146select 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);## complex subquery#CREATE TABLE t1 (  ID int(11) NOT NULL auto_increment,  name char(35) NOT NULL default '',  t2 char(3) NOT NULL default '',  District char(20) NOT NULL default '',  Population int(11) NOT NULL default '0',  PRIMARY KEY  (ID)) ENGINE=MyISAM;INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);CREATE TABLE t2 (  Code char(3) NOT NULL default '',  Name char(52) NOT NULL default '',  Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',  Region char(26) NOT NULL default '',  SurfaceArea float(10,2) NOT NULL default '0.00',  IndepYear smallint(6) default NULL,  Population int(11) NOT NULL default '0',  LifeExpectancy float(3,1) default NULL,  GNP float(10,2) default NULL,  GNPOld float(10,2) default NULL,  LocalName char(45) NOT NULL default '',  GovernmentForm char(45) NOT NULL default '',  HeadOfState char(60) default NULL,  Capital int(11) default NULL,  Code2 char(2) NOT NULL default '',  PRIMARY KEY  (Code)) ENGINE=MyISAM;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');INSERT INTO t2 VALUES ('AZE','Azerbaijan','Asia','Middle East',86600.00,1991,7734000,62.9,4127.00,4100.00,'Az鋜baycan','Federal Republic','Heyd鋜 膌iyev',144,'AZ');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); drop table t1, t2;## constants in IN#CREATE TABLE `t1` (  `id` mediumint(8) unsigned NOT NULL auto_increment,  `pseudo` varchar(35) character set latin1 NOT NULL default '',  PRIMARY KEY  (`id`),  UNIQUE KEY `pseudo` (`pseudo`)) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;INSERT INTO t1 (pseudo) VALUES ('test');SELECT 0 IN (SELECT 1 FROM t1 a);EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);INSERT INTO t1 (pseudo) VALUES ('test1');SELECT 0 IN (SELECT 1 FROM t1 a);EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);drop table t1;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+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));-- error 1054UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);select * from t1;drop table t1;## Multi update test#CREATE TABLE t1 (  id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);CREATE TABLE t2 (  id int(11) default NULL,  name varchar(15) default NULL) ENGINE=MyISAM CHARSET=latin1;INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita');update t1, t2 set t2.name='lenka' where t2.id in (select id from t1);select * from t2;drop table t1,t2;## correct NULL in <CONSTANT> IN (SELECT ...)#create table t1 (a int, unique index indexa (a)); insert into t1 values (-1), (-4), (-2), (NULL); select -10 IN (select a from t1 FORCE INDEX (indexa)); drop table t1;## Test optimization for sub selects#create table t1 (id int not null auto_increment primary key, salary int, key(salary));insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1);drop table t1;CREATE TABLE t1 (  ID int(10) unsigned NOT NULL auto_increment,  SUB_ID int(3) unsigned NOT NULL default '0',  REF_ID int(10) unsigned default NULL,  REF_SUB int(3) unsigned default '0',  PRIMARY KEY (ID,SUB_ID),  UNIQUE KEY t1_PK (ID,SUB_ID),  KEY t1_FK (REF_ID,REF_SUB),  KEY t1_REFID (REF_ID)) ENGINE=MyISAM CHARSET=cp1251;INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);DROP TABLE t1;## uninterruptable update#create table t1 (a int, b int);create table t2 (a int, b int);insert into t1 values (1,0), (2,0), (3,0);insert into t2 values (1,1), (2,1), (3,1), (2,2);update ignore t1 set b=(select b from t2 where t1.a=t2.a);select * from t1;drop table t1, t2;## reduced subselect in ORDER BY & GROUP BY clauses#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);drop table if exists t1;(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);## IN subselect optimization test#create 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);explain extended select * from t2 where t2.a in (select a from t1);select * from t2 where t2.a in (select a from t1 where t1.b <> 30);explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.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);disable_query_log;# making table large enoughlet $1 = 10000;while ($1) {  eval insert into t1 values (rand()*100000+200,rand()*100000);   dec $1; }enable_query_log;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);explain extended select * from t2 where t2.a in (select a from t1);select * from t2 where t2.a in (select a from t1 where t1.b <> 30);explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);insert into t1 values (3,31);select * from t2 where t2.a in (select a from t1 where t1.b <> 30);select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31);explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);drop table t1, t2, t3;## alloc_group_fields() working#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;drop table t1,t2,t3;## aggregate functions in HAVING test#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);drop table t1,t2;## update subquery with wrong field (to force name resolving# in UPDATE name space)#create table t1 (s1 int);create table t2 (s1 int);insert into t1 values (1);insert into t2 values (1);-- error 1054update t1 set  s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);DROP TABLE t1, t2;## collation test#CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,                 s2 CHAR(5) COLLATE latin1_swedish_ci);INSERT INTO t1 VALUES ('z','?');-- error 1267select * from t1 where s1 > (select max(s2) from t1);-- error 1267select * from t1 where s1 > any (select max(s2) from t1);drop table t1;## aggregate functions reinitialization

⌨️ 快捷键说明

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