|
|
drop table if exists t1,t2,t3;CREATE TABLE t1 (id int(6) DEFAULT '0' NOT NULL,idservice int(5),clee char(20) NOT NULL,flag char(1),KEY id (id),PRIMARY KEY (clee));INSERT INTO t1 VALUES (2,4,'6067169d','Y');INSERT INTO t1 VALUES (2,5,'606716d1','Y');INSERT INTO t1 VALUES (2,1,'606717c1','Y');INSERT INTO t1 VALUES (3,1,'6067178d','Y');INSERT INTO t1 VALUES (2,6,'60671515','Y');INSERT INTO t1 VALUES (2,7,'60671569','Y');INSERT INTO t1 VALUES (2,3,'dd','Y');CREATE TABLE t2 (id int(6) NOT NULL auto_increment,description varchar(40) NOT NULL,idform varchar(40),ordre int(6) unsigned DEFAULT '0' NOT NULL,image varchar(60),PRIMARY KEY (id),KEY id (id,ordre));INSERT INTO t2 VALUES (1,'Emettre un appel d''offres','en_construction.html',10,'emettre.gif');INSERT INTO t2 VALUES (2,'Emettre des soumissions','en_construction.html',20,'emettre.gif');INSERT INTO t2 VALUES (7,'Liste des t2','t2_liste_form.phtml',51060,'link.gif');INSERT INTO t2 VALUES (8,'Consulter les soumissions','consulter_soumissions.phtml',200,'link.gif');INSERT INTO t2 VALUES (9,'Ajouter un type de materiel','typeMateriel_ajoute_form.phtml',51000,'link.gif');INSERT INTO t2 VALUES (10,'Lister/modifier un type de materiel','typeMateriel_liste_form.phtml',51010,'link.gif');INSERT INTO t2 VALUES (3,'Créer une fiche de client','clients_ajoute_form.phtml',40000,'link.gif');INSERT INTO t2 VALUES (4,'Modifier des clients','en_construction.html',40010,'link.gif');INSERT INTO t2 VALUES (5,'Effacer des clients','en_construction.html',40020,'link.gif');INSERT INTO t2 VALUES (6,'Ajouter un service','t2_ajoute_form.phtml',51050,'link.gif');select t1.id,t1.idservice,t2.ordre,t2.description from t1, t2 where t1.id = 2 and t1.idservice = t2.id order by t2.ordre;id idservice ordre description2 1 10 Emettre un appel d'offres2 3 40000 Créer une fiche de client2 4 40010 Modifier des clients2 5 40020 Effacer des clients2 6 51050 Ajouter un service2 7 51060 Liste des t2drop table t1,t2;create table t1 (first char(10),last char(10));insert into t1 values ("Michael","Widenius");insert into t1 values ("Allan","Larsson");insert into t1 values ("David","Axmark");select concat(first," ",last) as name from t1 order by name;nameAllan LarssonDavid AxmarkMichael Wideniusselect concat(last," ",first) as name from t1 order by name;nameAxmark DavidLarsson AllanWidenius Michaeldrop table t1;create table t1 (i int);insert into t1 values(1),(2),(1),(2),(1),(2),(3);select distinct i from t1;i123select distinct i from t1 order by rand(5);i132select distinct i from t1 order by i desc;i321select distinct i from t1 order by 1-i;i321select distinct i from t1 order by mod(i,2),i;i213drop table t1;create table t1 ( pk int primary key, name varchar(255) not null, number varchar(255) not null);insert into t1 values (1, 'Gamma', '123'), (2, 'Gamma Ext', '123a'), (3, 'Alpha', '001'), (4, 'Beta', '200c');select distinct t1.name as 'Building Name',t1.number as 'Building Number' from t1 order by t1.name asc;Building Name Building NumberAlpha 001Beta 200cGamma 123Gamma Ext 123adrop table t1;create table t1 (id int not null,col1 int not null,col2 int not null,index(col1));insert into t1 values(1,2,2),(2,2,1),(3,1,2),(4,1,1),(5,1,4),(6,2,3),(7,3,1),(8,2,4);select * from t1 order by col1,col2;id col1 col24 1 13 1 25 1 42 2 11 2 26 2 38 2 47 3 1select col1 from t1 order by id;col122111232select col1 as id from t1 order by id;id11122223select concat(col1) as id from t1 order by id;id11122223drop table t1;CREATE TABLE t1 (id int auto_increment primary key,aika varchar(40),aikakentta timestamp);insert into t1 (aika) values ('Keskiviikko');insert into t1 (aika) values ('Tiistai');insert into t1 (aika) values ('Maanantai');insert into t1 (aika) values ('Sunnuntai');SELECT FIELD(SUBSTRING(t1.aika,1,2),'Ma','Ti','Ke','To','Pe','La','Su') AS test FROM t1 ORDER by test;test1237drop table t1;CREATE TABLE t1(a int unsigned NOT NULL,b int unsigned NOT NULL,c int unsigned NOT NULL,UNIQUE(a),INDEX(b),INDEX(c));CREATE TABLE t2(c int unsigned NOT NULL,i int unsigned NOT NULL,INDEX(c));CREATE TABLE t3(c int unsigned NOT NULL,v varchar(64),INDEX(c));INSERT INTO t1 VALUES (1,1,1);INSERT INTO t1 VALUES (2,1,2);INSERT INTO t1 VALUES (3,2,1);INSERT INTO t1 VALUES (4,2,2);INSERT INTO t2 VALUES (1,50);INSERT INTO t2 VALUES (2,25);INSERT INTO t3 VALUES (1,'123 Park Place');INSERT INTO t3 VALUES (2,'453 Boardwalk');SELECT a,b,if(b = 1,i,if(b = 2,v,''))FROM t1LEFT JOIN t2 USING(c)LEFT JOIN t3 ON t3.c = t1.c;a b if(b = 1,i,if(b = 2,v,''))1 1 502 1 253 2 123 Park Place4 2 453 BoardwalkSELECT a,b,if(b = 1,i,if(b = 2,v,''))FROM t1LEFT JOIN t2 ON t1.c = t2.cLEFT JOIN t3 ON t3.c = t1.c;a b if(b = 1,i,if(b = 2,v,''))1 1 502 1 253 2 123 Park Place4 2 453 BoardwalkSELECT a,b,if(b = 1,i,if(b = 2,v,''))FROM t1LEFT JOIN t2 USING(c)LEFT JOIN t3 ON t3.c = t1.cORDER BY a;a b if(b = 1,i,if(b = 2,v,''))1 1 502 1 253 2 123 Park Place4 2 453 BoardwalkSELECT a,b,if(b = 1,i,if(b = 2,v,''))FROM t1LEFT JOIN t2 ON t1.c = t2.cLEFT JOIN t3 ON t3.c = t1.cORDER BY a;a b if(b = 1,i,if(b = 2,v,''))1 1 502 1 253 2 123 Park Place4 2 453 Boardwalkdrop table t1,t2,t3;create table t1 (ID int not null primary key, TransactionID int not null);insert into t1 (ID, TransactionID) values (1, 87), (2, 89), (3, 92), (4, 94), (5, 486), (6, 490), (7, 753), (9, 828), (10, 832), (11, 834), (12, 840);create table t2 (ID int not null primary key, GroupID int not null);insert into t2 (ID, GroupID) values (87, 87), (89, 89), (92, 92), (94, 94), (486, 486), (490, 490),(753, 753), (828, 828), (832, 832), (834, 834), (840, 840);create table t3 (ID int not null primary key, DateOfAction date not null);insert into t3 (ID, DateOfAction) values (87, '1999-07-19'), (89, '1999-07-19'), (92, '1999-07-19'), (94, '1999-07-19'), (486, '1999-07-18'), (490, '2000-03-27'), (753, '2000-03-28'), (828, '1999-07-27'), (832, '1999-07-27'),(834, '1999-07-27'), (840, '1999-07-27');select t3.DateOfAction, t1.TransactionID from t1 join t2 join t3 where t2.ID = t1.TransactionID and t3.ID = t2.GroupID order by t3.DateOfAction, t1.TransactionID;DateOfAction TransactionID1999-07-18 4861999-07-19 871999-07-19 891999-07-19 921999-07-19 941999-07-27 8281999-07-27 8321999-07-27 8341999-07-27 8402000-03-27 4902000-03-28 753select t3.DateOfAction, t1.TransactionID from t1 join t2 join t3 where t2.ID = t1.TransactionID and t3.ID = t2.GroupID order by t1.TransactionID,t3.DateOfAction;DateOfAction TransactionID1999-07-19 871999-07-19 891999-07-19 921999-07-19 941999-07-18 4862000-03-27 4902000-03-28 7531999-07-27 8281999-07-27 8321999-07-27 8341999-07-27 840drop table t1,t2,t3;CREATE TABLE t1 (member_id int(11) NOT NULL auto_increment,inschrijf_datum varchar(20) NOT NULL default '',lastchange_datum varchar(20) NOT NULL default '',nickname varchar(20) NOT NULL default '',password varchar(8) NOT NULL default '',voornaam varchar(30) NOT NULL default '',tussenvoegsels varchar(10) NOT NULL default '',achternaam varchar(50) NOT NULL default '',straat varchar(100) NOT NULL default '',postcode varchar(10) NOT NULL default '',wijk varchar(40) NOT NULL default '',plaats varchar(50) NOT NULL default '',telefoon varchar(10) NOT NULL default '',geboortedatum date NOT NULL default '0000-00-00',geslacht varchar(5) NOT NULL default '',email varchar(80) NOT NULL default '',uin varchar(15) NOT NULL default '',homepage varchar(100) NOT NULL default '',internet varchar(15) NOT NULL default '',scherk varchar(30) NOT NULL default '',favo_boek varchar(50) NOT NULL default '',favo_tijdschrift varchar(50) NOT NULL default '',favo_tv varchar(50) NOT NULL default '',favo_eten varchar(50) NOT NULL default '',favo_muziek varchar(30) NOT NULL default '',info text NOT NULL default '',ipnr varchar(30) NOT NULL default '',PRIMARY KEY (member_id)) ENGINE=MyISAM PACK_KEYS=1;Warnings:Warning 1101 BLOB/TEXT column 'info' can't have a default valueinsert into t1 (member_id) values (1),(2),(3);select member_id, nickname, voornaam FROM t1ORDER by lastchange_datum DESC LIMIT 2;member_id nickname voornaam1 2 drop table t1;create table t1 (a int not null, b int, c varchar(10), key (a, b, c));insert into t1 values (1, NULL, NULL), (1, NULL, 'b'), (1, 1, NULL), (1, 1, 'b'), (1, 1, 'b'), (2, 1, 'a'), (2, 1, 'b'), (2, 2, 'a'), (2, 2, 'b'), (2, 3, 'c'),(1,3,'b');explain select * from t1 where (a = 1 and b is null and c = 'b') or (a > 2) order by a desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 index a a 22 NULL 11 Using where; Using indexselect * from t1 where (a = 1 and b is null and c = 'b') or (a > 2) order by a desc;a b c1 NULL bexplain select * from t1 where a >= 1 and a < 3 order by a desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 range a a 4 NULL 10 Using where; Using indexselect * from t1 where a >= 1 and a < 3 order by a desc;a b c2 3 c2 2 b2 2 a2 1 b2 1 a1 3 b1 1 b1 1 b1 1 NULL1 NULL b1 NULL NULLexplain select * from t1 where a = 1 order by a desc, b desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ref a a 4 const 5 Using where; Using indexselect * from t1 where a = 1 order by a desc, b desc;a b c1 3 b1 1 b1 1 b1 1 NULL1 NULL b1 NULL NULLexplain select * from t1 where a = 1 and b is null order by a desc, b desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ref a a 9 const,const 2 Using where; Using index; Using filesortselect * from t1 where a = 1 and b is null order by a desc, b desc;a b c1 NULL NULL1 NULL bexplain select * from t1 where a >= 1 and a < 3 and b >0 order by a desc,b desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 range a a 9 NULL 8 Using where; Using indexexplain select * from t1 where a = 2 and b >0 order by a desc,b desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 range a a 9 NULL 5 Using where; Using indexexplain select * from t1 where a = 2 and b is null order by a desc,b desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ref a a 9 const,const 1 Using where; Using index; Using filesortexplain select * from t1 where a = 2 and (b is null or b > 0) order by adesc,b desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 range a a 9 NULL 6 Using where; Using indexexplain select * from t1 where a = 2 and b > 0 order by a desc,b desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 range a a 9 NULL 5 Using where; Using indexexplain select * from t1 where a = 2 and b < 2 order by a desc,b desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 range a a 9 NULL 2 Using where; Using indexexplain select * from t1 where a = 1 order by b desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ref a a 4 const 5 Using where; Using indexselect * from t1 where a = 1 order by b desc;a b c1 3 b1 1 b1 1 b1 1 NULL1 NULL b1 NULL NULLalter table t1 modify b int not null, modify c varchar(10) not null;Warnings:Warning 1265 Data truncated for column 'b' at row 1Warning 1265 Data truncated for column 'c' at row 1Warning 1265 Data truncated for column 'b' at row 2Warning 1265 Data truncated for column 'c' at row 3explain select * from t1 order by a, b, c;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 index NULL a 20 NULL 11 Using indexselect * from t1 order by a, b, c;a b c1 0 1 0 b1 1 1 1 b1 1 b1 3 b2 1 a2 1 b2 2 a2 2 b2 3 cexplain select * from t1 order by a desc, b desc, c desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 index NULL a 20 NULL 11 Using indexselect * from t1 order by a desc, b desc, c desc;a b c2 3 c2 2 b2 2 a2 1 b2 1 a1 3 b1 1 b1 1 b1 1 1 0 b1 0 explain select * from t1 where (a = 1 and b = 1 and c = 'b') or (a > 2) order by a desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 range a a 20 NULL 3 Using where; Using indexselect * from t1 where (a = 1 and b = 1 and c = 'b') or (a > 2) order by a desc;a b c1 1 b1 1 bexplain select * from t1 where a < 2 and b <= 1 order by a desc, b desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 range a a 4 NULL 6 Using where; Using indexselect * from t1 where a < 2 and b <= 1 order by a desc, b desc;a b c1 1 b1 1 b1 1 1 0 b1 0 select count(*) from t1 where a < 5 and b > 0;count(*)9select * from t1 where a < 5 and b > 0 order by a desc,b desc;a b c2 3 c2 2 b2 2 a2 1 b2 1 a1 3 b1 1 b1 1 b1 1 explain select * from t1 where a between 1 and 3 and b <= 1 order by a desc, b desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 range a a 8 NULL 10 Using where; Using indexselect * from t1 where a between 1 and 3 and b <= 1 order by a desc, b desc;a b c2 1 b2 1 a1 1 b1 1 b1 1 1 0 b1 0 explain select * from t1 where a between 0 and 1 order by a desc, b desc;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 range a a 4 NULL 5 Using where; Using indexselect * from t1 where a between 0 and 1 order by a desc, b desc;a b c1 3 b1 1 b1 1 b1 1 1 0 b1 0 drop table t1;CREATE TABLE t1 (gid int(10) unsigned NOT NULL auto_increment,cid smallint(5) unsigned NOT NULL default '0',PRIMARY KEY (gid),KEY component_id (cid)) ENGINE=MyISAM;INSERT INTO t1 VALUES (103853,108),(103867,108),(103962,108),(104505,108),(104619,108),(104620,108);ALTER TABLE t1 add skr int(10) not null;CREATE TABLE t2 (gid int(10) unsigned NOT NULL default '0',uid smallint(5) unsigned NOT NULL default '1',sid tinyint(3) unsigned NOT NULL default '1',PRIMARY KEY (gid),KEY uid (uid),KEY status_id (sid)) ENGINE=MyISAM;INSERT INTO t2 VALUES (103853,250,5),(103867,27,5),(103962,27,5),(104505,117,5),(104619,75,5),(104620,15,5);CREATE TABLE t3 (uid smallint(6) NOT NULL auto_increment,PRIMARY KEY (uid)) ENGINE=MyISAM;INSERT INTO t3 VALUES (1),(15),(27),(75),(117),(250);ALTER TABLE t3 add skr int(10) not null;select t1.gid, t2.sid, t3.uid from t2, t1, t3 where t2.gid = t1.gid and t2.uid = t3.uid order by t3.uid, t1.gid;gid sid uid104620 5 15103867 5 27103962 5 27104619 5 75104505 5 117103853 5 250select t1.gid, t2.sid, t3.uid from t3, t2, t1 where t2.gid = t1.gid and t2.uid = t3.uid order by t3.uid, t1.gid;gid sid uid104620 5 15103867 5 27103962 5 27104619 5 75104505 5 117103853 5 250EXPLAIN select t1.gid, t2.sid, t3.uid from t3, t2, t1 where t2.gid = t1.gid and t2.uid = t3.uid order by t1.gid, t3.uid;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 index PRIMARY PRIMARY 4 NULL 6 Using index1 SIMPLE t2 eq_ref PRIMARY,uid PRIMARY 4 test.t1.gid 1 1 SIMPLE t3 eq_ref PRIMARY PRIMARY 2 test.t2.uid 1 Using where; Using indexEXPLAIN SELECT t1.gid, t3.uid from t1, t3 where t1.gid = t3.uid order by t1.gid,t3.skr;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t3 ALL PRIMARY NULL NULL NULL 6 Using temporary; Using filesort1 SIMPLE t1 eq_ref PRIMARY PRIMARY 4 test.t3.uid 1 Using where; Using indexEXPLAIN SELECT t1.gid, t2.sid, t3.uid from t2, t1, t3 where t2.gid = t1.gid and t2.uid = t3.uid order by t3.uid, t1.gid;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 index PRIMARY PRIMARY 4 NULL 6 Using index; Using temporary; Using filesort1 SIMPLE t2 eq_ref PRIMARY,uid PRIMARY 4 test.t1.gid 1 1 SIMPLE t3 eq_ref PRIMARY PRIMARY 2 test.t2.uid 1 Using where; Using indexEXPLAIN SELECT t1.gid, t3.uid from t1, t3 where t1.gid = t3.uid order by t3.skr,t1.gid;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t3 ALL PRIMARY NULL NULL NULL 6 Using temporary; Using filesort1 SIMPLE t1 eq_ref PRIMARY PRIMARY 4 test.t3.uid 1 Using where; Using indexEXPLAIN SELECT t1.gid, t3.uid from t1, t3 where t1.skr = t3.uid order by t1.gid,t3.skr;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ALL NULL NULL NULL NULL 6 Using temporary; Using filesort1 SIMPLE t3 eq_ref PRIMARY PRIMARY 2 test.t1.skr 1 Using wheredrop table t1,t2,t3;CREATE TABLE t1 (`titre` char(80) NOT NULL default '',`numeropost` mediumint(8) unsigned NOT NULL auto_increment,`date` datetime NOT NULL default '0000-00-00 00:00:00',`auteur` char(35) NOT NULL default '',`icone` tinyint(2) unsigned NOT NULL default '0',`lastauteur` char(35) NOT NULL default '',`nbrep` smallint(6) unsigned NOT NULL default '0',`dest` char(35) NOT NULL default '',`lu` tinyint(1) unsigned NOT NULL default '0',`vue` mediumint(8) unsigned NOT NULL default '0',`ludest` tinyint(1) unsigned NOT NULL default '0',`ouvert` tinyint(1) unsigned NOT NULL default '1',PRIMARY KEY (`numeropost`),KEY `date` (`date`),KEY `dest` (`dest`,`ludest`),KEY `auteur` (`auteur`,`lu`),KEY `auteur_2` (`auteur`,`date`),KEY `dest_2` (`dest`,`date`)) CHECKSUM=1;CREATE TABLE t2 (`numeropost` mediumint(8) unsigned NOT NULL default '0',`pseudo` char(35) NOT NULL default '',PRIMARY KEY (`numeropost`,`pseudo`),KEY `pseudo` (`pseudo`));INSERT INTO t1 (titre,auteur,dest) VALUES ('test','joce','bug');INSERT INTO t2 (numeropost,pseudo) VALUES (1,'joce'),(1,'bug');SELECT titre,t1.numeropost,auteur,icone,nbrep,0,date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30;titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur desttest 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bugSELECT titre,numeropost,auteur,icone,nbrep,0,date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30;titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur desttest 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bugSELECT titre,t1.numeropost,auteur,icone,nbrep,'0',date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30;titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur desttest 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bugSELECT titre,numeropost,auteur,icone,nbrep,'0',date,vue,ouvert,lastauteur,dest FROM t2 LEFT JOIN t1 USING(numeropost) WHERE t2.pseudo='joce' ORDER BY date DESC LIMIT 0,30;titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur desttest 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bugdrop table t1,t2;CREATE TABLE t1 (a int, b int);INSERT INTO t1 VALUES (1, 2);INSERT INTO t1 VALUES (3, 4);INSERT INTO t1 VALUES (5, NULL);SELECT * FROM t1 ORDER BY b;a b5 NULL1 23 4SELECT * FROM t1 ORDER BY b DESC;a b3 41 25 NULLSELECT * FROM t1 ORDER BY (a + b);a b5 NULL1 23 4SELECT * FROM t1 ORDER BY (a + b) DESC;a b3 41 25 NULLDROP TABLE t1;create table t1(id int not null auto_increment primary key, t char(12));explain select id,t from t1 order by id;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ALL NULL NULL NULL NULL 1000 Using filesortexplain select id,t from t1 force index (primary) order by id;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 index NULL PRIMARY 4 NULL 1000 drop table t1;CREATE TABLE t1 (FieldKey varchar(36) NOT NULL default '',LongVal bigint(20) default NULL,StringVal mediumtext,KEY FieldKey (FieldKey),KEY LongField (FieldKey,LongVal),KEY StringField (FieldKey,StringVal(32)));INSERT INTO t1 VALUES ('0',3,'0'),('0',2,'1'),('0',1,'2'),('1',2,'1'),('1',1,'3'), ('1',0,'2'),('2',3,'0'),('2',2,'1'),('2',1,'2'),('2',3,'0'),('2',2,'1'),('2',1,'2'),('3',2,'1'),('3',1,'2'),('3','3','3');EXPLAIN SELECT * FROM t1 WHERE FieldKey = '1' ORDER BY LongVal;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ref FieldKey,LongField,StringField LongField 38 const 3 Using whereSELECT * FROM t1 WHERE FieldKey = '1' ORDER BY LongVal;FieldKey LongVal StringVal1 0 21 1 31 2 1EXPLAIN SELECT * FROM t1 WHERE FieldKey > '2' ORDER BY LongVal;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 range FieldKey,LongField,StringField FieldKey 38 NULL 4 Using where; Using filesortSELECT * FROM t1 WHERE FieldKey > '2' ORDER BY LongVal;FieldKey LongVal StringVal3 1 23 2 13 3 3EXPLAIN SELECT * FROM t1 WHERE FieldKey > '2' ORDER BY FieldKey, LongVal;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 range FieldKey,LongField,StringField LongField 38 NULL 4 Using whereSELECT * FROM t1 WHERE FieldKey > '2' ORDER BY FieldKey, LongVal;FieldKey LongVal StringVal3 1 23 2 13 3 3DROP TABLE t1;CREATE TABLE t1 (a INT, b INT);SET @id=0;UPDATE t1 SET a=0 ORDER BY (a=@id), b;DROP TABLE t1;CREATE TABLE t1 ( id smallint(6) unsigned NOT NULL default '0', menu tinyint(4) NOT NULL default '0', KEY id (id), KEY menu (menu)) ENGINE=MyISAM;INSERT INTO t1 VALUES (11384, 2),(11392, 2);SELECT id FROM t1 WHERE id <11984 AND menu =2 ORDER BY id DESC LIMIT 1 ;id11392drop table t1;create table t1(a int, b int, index(b));insert into t1 values (2, 1), (1, 1), (4, NULL), (3, NULL), (6, 2), (5, 2);explain select * from t1 where b=1 or b is null order by a;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ref_or_null b b 5 const 3 Using where; Using filesortselect * from t1 where b=1 or b is null order by a;a b1 12 13 NULL4 NULLexplain select * from t1 where b=2 or b is null order by a;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ref_or_null b b 5 const 4 Using where; Using filesortselect * from t1 where b=2 or b is null order by a;a b3 NULL4 NULL5 26 2drop table t1;create table t1 (a int not null auto_increment, b int not null, c int not null, d int not null,key(a,b,d), key(c,b,a));create table t2 like t1;insert into t1 values (NULL, 1, 2, 0), (NULL, 2, 1, 1), (NULL, 3, 4, 2), (NULL, 4, 3, 3);insert into t2 select null, b, c, d from t1;insert into t1 select null, b, c, d from t2;insert into t2 select null, b, c, d from t1;insert into t1 select null, b, c, d from t2;insert into t2 select null, b, c, d from t1;insert into t1 select null, b, c, d from t2;insert into t2 select null, b, c, d from t1;insert into t1 select null, b, c, d from t2;insert into t2 select null, b, c, d from t1;insert into t1 select null, b, c, d from t2;optimize table t1;Table Op Msg_type Msg_texttest.t1 optimize status OKset @row=10;insert into t1 select 1, b, c + (@row:=@row - 1) * 10, d - @row from t2 limit 10;select * from t1 where a=1 and b in (1) order by c, b, a;a b c d1 1 2 01 1 12 -11 1 52 -51 1 92 -9select * from t1 where a=1 and b in (1);a b c d1 1 92 -91 1 52 -51 1 12 -11 1 2 0drop table t1, t2;create table t1 (col1 int, col int);create table t2 (col2 int, col int);insert into t1 values (1,1),(2,2),(3,3);insert into t2 values (1,3),(2,2),(3,1);select t1.* , t2.col as t2_col from t1 left join t2 on (t1.col1=t2.col2)order by col;col1 col t2_col1 1 32 2 23 3 1select col1 as col, col from t1 order by col;ERROR 23000: Column 'col' in order clause is ambiguousselect t1.col as c1, t2.col as c2 from t1, t2 where t1.col1=t2.col2order by col;ERROR 23000: Column 'col' in order clause is ambiguousselect t1.col as c1, t2.col as c2 from t1, t2 where t1.col1=t2.col2order by col;ERROR 23000: Column 'col' in order clause is ambiguousselect col1 from t1, t2 where t1.col1=t2.col2 order by col;ERROR 23000: Column 'col' in order clause is ambiguousselect t1.col as t1_col, t2.col2 from t1, t2 where t1.col1=t2.col2order by col;ERROR 23000: Column 'col' in order clause is ambiguousselect t1.col as t1_col, t2.col from t1, t2 where t1.col1=t2.col2order by col;t1_col col3 12 21 3select col2 as c, col as c from t2 order by col;c c3 12 21 3select col2 as col, col as col2 from t2 order by col;col col21 32 23 1select t2.col2, t2.col, t2.col from t2 order by col;col2 col col3 1 12 2 21 3 3select t2.col2 as col from t2 order by t2.col;col321select t2.col2 as col, t2.col from t2 order by t2.col;col col3 12 21 3select t2.col2, t2.col, t2.col from t2 order by t2.col;col2 col col3 1 12 2 21 3 3drop table t1, t2;create table t1 (a char(25));insert into t1 set a = repeat('x', 20);insert into t1 set a = concat(repeat('x', 19), 'z');insert into t1 set a = concat(repeat('x', 19), 'ab');insert into t1 set a = concat(repeat('x', 19), 'aa');set max_sort_length=20;select a from t1 order by a;axxxxxxxxxxxxxxxxxxxabxxxxxxxxxxxxxxxxxxxaaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxzdrop table t1;create table t1 (`sid` decimal(8,0) default null,`wnid` varchar(11) not null default '',key `wnid14` (`wnid`(4)),key `wnid` (`wnid`)) engine=myisam default charset=latin1;insert into t1 (`sid`, `wnid`) values('10100','01019000000'),('37986','01019000000'),('37987','01019010000'),('39560','01019090000'),('37989','01019000000'),('37990','01019011000'),('37991','01019011000'),('37992','01019019000'),('37993','01019030000'),('37994','01019090000'),('475','02070000000'),('25253','02071100000'),('25255','02071100000'),('25256','02071110000'),('25258','02071130000'),('25259','02071190000'),('25260','02071200000'),('25261','02071210000'),('25262','02071290000'),('25263','02071300000'),('25264','02071310000'),('25265','02071310000'),('25266','02071320000'),('25267','02071320000'),('25269','02071330000'),('25270','02071340000'),('25271','02071350000'),('25272','02071360000'),('25273','02071370000'),('25281','02071391000'),('25282','02071391000'),('25283','02071399000'),('25284','02071400000'),('25285','02071410000'),('25286','02071410000'),('25287','02071420000'),('25288','02071420000'),('25291','02071430000'),('25290','02071440000'),('25292','02071450000'),('25293','02071460000'),('25294','02071470000'),('25295','02071491000'),('25296','02071491000'),('25297','02071499000');explain select * from t1 where wnid like '0101%' order by wnid;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 range wnid14,wnid wnid 13 NULL 10 Using whereselect * from t1 where wnid like '0101%' order by wnid;sid wnid10100 0101900000037986 0101900000037989 0101900000037987 0101901000037990 0101901100037991 0101901100037992 0101901900037993 0101903000039560 0101909000037994 01019090000drop table t1;CREATE TABLE t1 (a int);INSERT INTO t1 VALUES (2), (1), (1), (2), (1);SELECT a FROM t1 ORDER BY a;a11122(SELECT a FROM t1) ORDER BY a;a11122DROP TABLE t1;CREATE TABLE t1 (a int, b int);INSERT INTO t1 VALUES (1,30), (2,20), (1,10), (2,30), (1,20), (2,10);(SELECT b,a FROM t1 ORDER BY a,b) ORDER BY b,a;b a10 110 220 120 230 130 2(SELECT b FROM t1 ORDER BY b DESC) ORDER BY b ASC;b101020203030(SELECT b,a FROM t1 ORDER BY b,a) ORDER BY a,b;b a10 120 130 110 220 230 2(SELECT b,a FROM t1 ORDER by b,a LIMIT 3) ORDER by a,b;b a10 120 110 2DROP TABLE t1;CREATE TABLE t1 (a INT);INSERT INTO t1 VALUES (1),(2);SELECT a + 1 AS num FROM t1 ORDER BY 30 - num;num32SELECT CONCAT('test', a) AS str FROM t1 ORDER BY UPPER(str);strtest1test2SELECT a + 1 AS num FROM t1 GROUP BY 30 - num;num32SELECT a + 1 AS num FROM t1 HAVING 30 - num;num23SELECT a + 1 AS num, num + 1 FROM t1;ERROR 42S22: Unknown column 'num' in 'field list'SELECT a + 1 AS num, (select num + 2 FROM t1 LIMIT 1) FROM t1;num (select num + 2 FROM t1 LIMIT 1)2 43 5SELECT a.a + 1 AS num FROM t1 a JOIN t1 b ON num = b.a;ERROR 42S22: Unknown column 'num' in 'on clause'DROP TABLE t1;CREATE TABLE bug25126 (val int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY);UPDATE bug25126 SET MissingCol = MissingCol;ERROR 42S22: Unknown column 'MissingCol' in 'field list'UPDATE bug25126 SET val = val ORDER BY MissingCol;ERROR 42S22: Unknown column 'MissingCol' in 'order clause'UPDATE bug25126 SET val = val ORDER BY val;UPDATE bug25126 SET val = 1 ORDER BY val;UPDATE bug25126 SET val = 1 ORDER BY MissingCol;ERROR 42S22: Unknown column 'MissingCol' in 'order clause'UPDATE bug25126 SET val = 1 ORDER BY val, MissingCol;ERROR 42S22: Unknown column 'MissingCol' in 'order clause'UPDATE bug25126 SET val = MissingCol ORDER BY MissingCol;ERROR 42S22: Unknown column 'MissingCol' in 'order clause'UPDATE bug25126 SET MissingCol = 1 ORDER BY val, MissingCol;ERROR 42S22: Unknown column 'MissingCol' in 'order clause'UPDATE bug25126 SET MissingCol = 1 ORDER BY MissingCol;ERROR 42S22: Unknown column 'MissingCol' in 'order clause'UPDATE bug25126 SET MissingCol = val ORDER BY MissingCol;ERROR 42S22: Unknown column 'MissingCol' in 'order clause'UPDATE bug25126 SET MissingCol = MissingCol ORDER BY MissingCol;ERROR 42S22: Unknown column 'MissingCol' in 'order clause'DROP TABLE bug25126;CREATE TABLE t1 (a int);SELECT p.a AS val, q.a AS val1 FROM t1 p, t1 q ORDER BY val > 1;val val1SELECT p.a AS val, q.a AS val FROM t1 p, t1 q ORDER BY val;ERROR 23000: Column 'val' in order clause is ambiguousSELECT p.a AS val, q.a AS val FROM t1 p, t1 q ORDER BY val > 1;ERROR 23000: Column 'val' in order clause is ambiguousDROP TABLE t1;CREATE TABLE t1 (a int);INSERT INTO t1 VALUES (3), (2), (4), (1);SELECT a, IF(a IN (2,3), a, a+10) FROM t1ORDER BY IF(a IN (2,3), a, a+10);a IF(a IN (2,3), a, a+10)2 23 31 114 14SELECT a, IF(a NOT IN (2,3), a, a+10) FROM t1 ORDER BY IF(a NOT IN (2,3), a, a+10);a IF(a NOT IN (2,3), a, a+10)1 14 42 123 13SELECT a, IF(a IN (2,3), a, a+10) FROM t1 ORDER BY IF(a NOT IN (2,3), a, a+10);a IF(a IN (2,3), a, a+10)1 114 142 23 3SELECT a, IF(a BETWEEN 2 AND 3, a, a+10) FROM t1ORDER BY IF(a BETWEEN 2 AND 3, a, a+10);a IF(a BETWEEN 2 AND 3, a, a+10)2 23 31 114 14SELECT a, IF(a NOT BETWEEN 2 AND 3, a, a+10) FROM t1 ORDER BY IF(a NOT BETWEEN 2 AND 3, a, a+10);a IF(a NOT BETWEEN 2 AND 3, a, a+10)1 14 42 123 13SELECT a, IF(a BETWEEN 2 AND 3, a, a+10) FROM t1 ORDER BY IF(a NOT BETWEEN 2 AND 3, a, a+10);a IF(a BETWEEN 2 AND 3, a, a+10)1 114 142 23 3SELECT IF(a IN (1,2), a, '') as x1, IF(a NOT IN (1,2), a, '') as x2FROM t1 GROUP BY x1, x2;x1 x2 3 41 2 SELECT IF(a IN (1,2), a, '') as x1, IF(a NOT IN (1,2), a, '') as x2FROM t1 GROUP BY x1, IF(a NOT IN (1,2), a, '');x1 x2 3 41 2 SELECT a, a IN (1,2) FROM t1 ORDER BY a IN (1,2);a a IN (1,2)3 04 02 11 1SELECT a FROM t1 ORDER BY a IN (1,2);a3421SELECT a+10 FROM t1 ORDER BY a IN (1,2);a+1013141211SELECT a, IF(a IN (1,2), a, a+10) FROM t1ORDER BY IF(a IN (3,4), a, a+10);a IF(a IN (1,2), a, a+10)3 134 141 12 2DROP TABLE t1;create table t1 (a int not null, b int not null, c int not null);insert t1 values (1,1,1),(1,1,2),(1,2,1);select a, b from t1 group by a, b order by sum(c);a b1 21 1drop table t1;CREATE TABLE t1 (a int, b int, PRIMARY KEY (a));INSERT INTO t1 VALUES (1,1), (2,2), (3,3);explain SELECT t1.b as a, t2.b as c FROM t1 LEFT JOIN t1 t2 ON (t1.a = t2.a AND t2.a = 2) ORDER BY c;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using temporary; Using filesort1 SIMPLE t2 const PRIMARY PRIMARY 4 const 1 SELECT t2.b as c FROM t1 LEFT JOIN t1 t2 ON (t1.a = t2.a AND t2.a = 2) ORDER BY c;cNULLNULL2explain SELECT t1.b as a, t2.b as c FROM t1 JOIN t1 t2 ON (t1.a = t2.a AND t2.a = 2) ORDER BY c;id select_type table type possible_keys key key_len ref rows Extra1 SIMPLE t1 const PRIMARY PRIMARY 4 const 1 1 SIMPLE t2 const PRIMARY PRIMARY 4 const 1 CREATE TABLE t2 LIKE t1;INSERT INTO t2 SELECT * from t1;CREATE TABLE t3 LIKE t1;INSERT INTO t3 SELECT * from t1;CREATE TABLE t4 LIKE t1;INSERT INTO t4 SELECT * from t1;INSERT INTO t1 values (0,0),(4,4);SELECT t2.b FROM t1 LEFT JOIN (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)ON (t1.a=t2.a AND t1.b=t3.b) order by t2.b;bNULLNULL123DROP TABLE t1,t2,t3,t4;create table t1 (a int, b int, c int);insert into t1 values (1,2,3), (9,8,3), (19,4,3), (1,4,9);select a,(sum(b)/sum(c)) as ratio from t1 group by a order by sum(b)/sum(c) asc;a ratio1 0.500019 1.33339 2.6667drop table t1;CREATE TABLE t1 (a INT UNSIGNED NOT NULL, b TIME);INSERT INTO t1 (a) VALUES (100000), (0), (100), (1000000),(10000), (1000), (10);UPDATE t1 SET b = SEC_TO_TIME(a);SELECT a, b FROM t1 ORDER BY b DESC;a b1000000 277:46:40100000 27:46:4010000 02:46:401000 00:16:40100 00:01:4010 00:00:100 00:00:00SELECT a, b FROM t1 ORDER BY SEC_TO_TIME(a) DESC;a b1000000 277:46:40100000 27:46:4010000 02:46:401000 00:16:40100 00:01:4010 00:00:100 00:00:00DROP TABLE t1;
|