📄 subselect.test
字号:
#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);drop table t1, t2;## row union#create table t1 (s1 char(5));-- error 1241select (select 'a','b' from t1 union select 'a','b' from t1) from t1;insert into t1 values ('tttt');select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1);explain extended (select * from t1);(select * from t1);drop table t1;## IN optimisation test results#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;select s1, s1 = ANY (SELECT s1 FROM t2) from t1;select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;drop table t1,t2;## correct ALL optimisation#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);explain extended select * from t3 where a >= all (select b from t2);select * from t3 where a >= some (select b from t2);explain extended select * from t3 where a >= some (select b from t2);select * from t3 where a >= all (select b from t2 group by 1);explain extended select * from t3 where a >= all (select b from t2 group by 1);select * from t3 where a >= some (select b from t2 group by 1);explain extended select * from t3 where a >= some (select b from t2 group by 1);select * from t3 where NULL >= any (select b from t2);explain extended select * from t3 where NULL >= any (select b from t2);select * from t3 where NULL >= any (select b from t2 group by 1);explain extended select * from t3 where NULL >= any (select b from t2 group by 1);select * from t3 where NULL >= some (select b from t2);explain extended select * from t3 where NULL >= some (select b from t2);select * from t3 where NULL >= some (select b from t2 group by 1);explain extended select * from t3 where NULL >= some (select b from t2 group by 1);## optimized static ALL/ANY with grouping#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);explain extended select * from t3 where a > all (select max(b) from t2 group by a);drop table t2, t3;## correct used_tables()#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` (`db_id` int(11) NOT NULL auto_increment,`name` varchar(200) NOT NULL default '',`primary_uid` smallint(6) NOT NULL default '0',`secondary_uid` smallint(6) NOT NULL default '0',PRIMARY KEY (`db_id`),UNIQUE KEY `name_2` (`name`),FULLTEXT KEY `name` (`name`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2147483647;INSERT INTO `t2` (`db_id`, `name`, `primary_uid`, `secondary_uid`) VALUES (18, 'Not Set 1', 0, 0),(19, 'Valid', 1, 2),(20, 'Valid 2', 1, 2),(21, 'Should Not Return', 1, 2),(26, 'Not Set 2', 0, 0),(-1, 'ALL DB\'S', 0, 0);CREATE TABLE `t3` (`taskgenid` mediumint(9) NOT NULL auto_increment,`dbid` int(11) NOT NULL default '0',`taskid` int(11) NOT NULL default '0',`mon` tinyint(4) NOT NULL default '1',`tues` tinyint(4) NOT NULL default '1',`wed` tinyint(4) NOT NULL default '1',`thur` tinyint(4) NOT NULL default '1',`fri` tinyint(4) NOT NULL default '1',`sat` tinyint(4) NOT NULL default '0',`sun` tinyint(4) NOT NULL default '0',`how_often` smallint(6) NOT NULL default '1',`userid` smallint(6) NOT NULL default '0',`active` tinyint(4) NOT NULL default '1',PRIMARY KEY (`taskgenid`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2 ;INSERT INTO `t3` (`taskgenid`, `dbid`, `taskid`, `mon`, `tues`,`wed`, `thur`, `fri`, `sat`, `sun`, `how_often`, `userid`, `active`) VALUES (1,-1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1);CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;SELECT dbid, name FROM t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND ((date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')) AND t4.task_id = taskid;drop table t1,t2,t3,t4;## cardinality check#CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;INSERT INTO t1 VALUES (1),(5);CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;INSERT INTO t2 VALUES (2),(6);-- error 1241select * from t1 where (1,2,6) in (select * from t2);DROP TABLE t1,t2;## DO and SET with errors#create table t1 (s1 int);insert into t1 values (1);insert into t1 values (2);-- error 1242set sort_buffer_size = (select s1 from t1);do (select * from t1);drop table t1;## optimized ALL/ANY with union#create table t1 (s1 char);insert into t1 values ('e');select * from t1 where 'f' > any (select s1 from t1);select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);drop table t1;## filesort in subquery (restoring join_tab)#CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874');CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1;INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');select c.number as phone,(select p.code from t2 p where c.number like concat(p.code, '%') order by length(p.code) desc limit 1) as code from t1 c;drop table t1, t2;## unresolved field error#create table t1 (s1 int); create table t2 (s1 int);-- error 1054select * from t1 where (select count(*) from t2 where t1.s2) = 1;-- error 1054select * from t1 where (select count(*) from t2 group by t1.s2) = 1;-- error 1054select count(*) from t2 group by t1.s2;drop table t1, t2;## fix_fields() in add_ref_to_table_cond()#CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB));CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA));INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365');INSERT INTO t2 VALUES (100, 200, 'C');SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1);DROP TABLE t1, t2;CREATE TABLE t1 (a int(1));INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;DROP TABLE t1;## Bug 2198#create table t1 (a int, b decimal(13, 3)); insert into t1 values (1, 0.123);select a, (select max(b) from t1) into outfile "subselect.out.file.1" from t1;delete from t1;load data infile "subselect.out.file.1" into table t1;select * from t1;drop table t1;## Bug 2479#CREATE TABLE `t1` ( `id` int(11) NOT NULL auto_increment, `id_cns` tinyint(3) unsigned NOT NULL default '0', `tipo` enum('','UNO','DUE') NOT NULL default '', `anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000', `particolare` mediumint(8) unsigned NOT NULL default '0', `generale` mediumint(8) unsigned NOT NULL default '0', `bis` tinyint(3) unsigned NOT NULL default '0', PRIMARY KEY (`id`), UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`), UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`));INSERT INTO `t1` VALUES (1,16,'UNO',1987,2048,9681,0),(2,50,'UNO',1987,1536,13987,0),(3,16,'UNO',1987,2432,14594,0),(4,16,'UNO',1987,1792,13422,0),(5,16,'UNO',1987,1025,10240,0),(6,16,'UNO',1987,1026,7089,0);CREATE TABLE `t2` ( `id` tinyint(3) unsigned NOT NULL auto_increment, `max_anno_dep` smallint(6) unsigned NOT NULL default '0', PRIMARY KEY (`id`));INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);SELECT cns.id, cns.max_anno_dep, cns.max_anno_dep = (SELECT s.anno_dep FROM t1 AS s WHERE s.id_cns = cns.id ORDER BY s.anno_dep DESC LIMIT 1) AS PIPPO FROM t2 AS cns;DROP TABLE t1, t2;## GLOBAL LIMIT#create table t1 (a int);insert into t1 values (1), (2), (3);SET SQL_SELECT_LIMIT=1;select sum(a) from (select * from t1) as a;select 2 in (select * from t1);SET SQL_SELECT_LIMIT=default;drop table t1;## Bug #3118: subselect + order by#CREATE TABLE t1 (a int, b int, INDEX (a));INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b;DROP TABLE t1;# Item_cond fix field#create table t1(val varchar(10));insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');select count(*) from t1 as w1 where w1.val in (select w2.val from t1 as w2 where w2.val like 'm%') and w1.val in (select w3.val from t1 as w3 where w3.val like 'e%');drop table t1;## ref_or_null replacing with ref#create table t1 (id int not null, text varchar(20) not null default '', primary key (id));insert into t1 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text11'), (12, 'text12');select * from t1 where id not in (select id from t1 where id < 8);select * from t1 as tt where not exists (select id from t1 where id < 8 and (id = tt.id or id is null) having id is not null);explain extended select * from t1 where id not in (select id from t1 where id < 8);explain extended select * from t1 as tt where not exists (select id from t1 where id < 8 and (id = tt.id or id is null) having id is not null);insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');create table t2 (id int not null, text varchar(20) not null default '', primary key (id));insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10');select * from t1 a left join t2 b on (a.id=b.id or b.id is null) join t1 c on (if(isnull(b.id), 1000, b.id)=c.id);explain extended select * from t1 a left join t2 b on (a.id=b.id or b.id is null) join t1 c on (if(isnull(b.id), 1000, b.id)=c.id);drop table t1,t2;## Static tables & rund() in subqueries#create table t1 (a int);insert into t1 values (1);explain select benchmark(1000, (select a from t1 where a=sha(rand())));drop table t1;## bug 3188#create table t1(id int);create table t2(id int);create table t3(flag int);-- error 1064select (select * from t3 where id not null) from t1, t2;drop table t1,t2,t3;## aggregate functions (Bug #3505)#CREATE TABLE t1 (id INT);CREATE TABLE t2 (id INT);INSERT INTO t1 VALUES (1), (2);INSERT INTO t2 VALUES (1);SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY t1.id;SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY id;DROP TABLE t1,t2;## ALL/ANY test#CREATE TABLE t1 ( a int, b int );INSERT INTO t1 VALUES (1,1),(2,2),(3,3);SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );# with indexALTER TABLE t1 ADD INDEX (a);SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -