1
drop table if exists t1,t2,t3;
3
id int(6) DEFAULT '0' NOT NULL,
5
clee char(20) NOT NULL,
10
INSERT INTO t1 VALUES (2,4,'6067169d','Y');
11
INSERT INTO t1 VALUES (2,5,'606716d1','Y');
12
INSERT INTO t1 VALUES (2,1,'606717c1','Y');
13
INSERT INTO t1 VALUES (3,1,'6067178d','Y');
14
INSERT INTO t1 VALUES (2,6,'60671515','Y');
15
INSERT INTO t1 VALUES (2,7,'60671569','Y');
16
INSERT INTO t1 VALUES (2,3,'dd','Y');
18
id int(6) NOT NULL auto_increment,
19
description varchar(40) NOT NULL,
21
ordre int(6) unsigned DEFAULT '0' NOT NULL,
26
INSERT INTO t2 VALUES (1,'Emettre un appel d''offres','en_construction.html',10,'emettre.gif');
27
INSERT INTO t2 VALUES (2,'Emettre des soumissions','en_construction.html',20,'emettre.gif');
28
INSERT INTO t2 VALUES (7,'Liste des t2','t2_liste_form.phtml',51060,'link.gif');
29
INSERT INTO t2 VALUES (8,'Consulter les soumissions','consulter_soumissions.phtml',200,'link.gif');
30
INSERT INTO t2 VALUES (9,'Ajouter un type de materiel','typeMateriel_ajoute_form.phtml',51000,'link.gif');
31
INSERT INTO t2 VALUES (10,'Lister/modifier un type de materiel','typeMateriel_liste_form.phtml',51010,'link.gif');
32
INSERT INTO t2 VALUES (3,'Créer une fiche de client','clients_ajoute_form.phtml',40000,'link.gif');
33
INSERT INTO t2 VALUES (4,'Modifier des clients','en_construction.html',40010,'link.gif');
34
INSERT INTO t2 VALUES (5,'Effacer des clients','en_construction.html',40020,'link.gif');
35
INSERT INTO t2 VALUES (6,'Ajouter un service','t2_ajoute_form.phtml',51050,'link.gif');
36
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;
37
id idservice ordre description
38
2 1 10 Emettre un appel d'offres
39
2 3 40000 Créer une fiche de client
40
2 4 40010 Modifier des clients
41
2 5 40020 Effacer des clients
42
2 6 51050 Ajouter un service
43
2 7 51060 Liste des t2
45
create table t1 (first char(10),last char(10));
46
insert into t1 values ("Michael","Widenius");
47
insert into t1 values ("Allan","Larsson");
48
insert into t1 values ("David","Axmark");
49
select concat(first," ",last) as name from t1 order by name;
54
select concat(last," ",first) as name from t1 order by name;
60
create table t1 (i int);
61
insert into t1 values(1),(2),(1),(2),(1),(2),(3);
62
select distinct i from t1;
67
select distinct i from t1 order by rand(5);
72
select distinct i from t1 order by i desc;
77
select distinct i from t1 order by 1-i;
82
select distinct i from t1 order by mod(i,2),i;
88
create table t1 ( pk int primary key, name varchar(255) not null, number varchar(255) not null);
89
insert into t1 values (1, 'Gamma', '123'), (2, 'Gamma Ext', '123a'), (3, 'Alpha', '001'), (4, 'Beta', '200c');
90
select distinct t1.name as 'Building Name',t1.number as 'Building Number' from t1 order by t1.name asc;
91
Building Name Building Number
97
create table t1 (id int not null,col1 int not null,col2 int not null,index(col1));
98
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);
99
select * from t1 order by col1,col2;
109
select col1 from t1 order by id;
119
select col1 as id from t1 order by id;
129
select concat(col1) as id from t1 order by id;
140
CREATE TABLE t1 (id int auto_increment primary key,aika varchar(40),aikakentta timestamp);
141
insert into t1 (aika) values ('Keskiviikko');
142
insert into t1 (aika) values ('Tiistai');
143
insert into t1 (aika) values ('Maanantai');
144
insert into t1 (aika) values ('Sunnuntai');
145
SELECT FIELD(SUBSTRING(t1.aika,1,2),'Ma','Ti','Ke','To','Pe','La','Su') AS test FROM t1 ORDER by test;
154
a int unsigned NOT NULL,
155
b int unsigned NOT NULL,
156
c int unsigned NOT NULL,
163
c int unsigned NOT NULL,
164
i int unsigned NOT NULL,
169
c int unsigned NOT NULL,
173
INSERT INTO t1 VALUES (1,1,1);
174
INSERT INTO t1 VALUES (2,1,2);
175
INSERT INTO t1 VALUES (3,2,1);
176
INSERT INTO t1 VALUES (4,2,2);
177
INSERT INTO t2 VALUES (1,50);
178
INSERT INTO t2 VALUES (2,25);
179
INSERT INTO t3 VALUES (1,'123 Park Place');
180
INSERT INTO t3 VALUES (2,'453 Boardwalk');
181
SELECT a,b,if(b = 1,i,if(b = 2,v,''))
183
LEFT JOIN t2 USING(c)
184
LEFT JOIN t3 ON t3.c = t1.c;
185
a b if(b = 1,i,if(b = 2,v,''))
190
SELECT a,b,if(b = 1,i,if(b = 2,v,''))
192
LEFT JOIN t2 ON t1.c = t2.c
193
LEFT JOIN t3 ON t3.c = t1.c;
194
a b if(b = 1,i,if(b = 2,v,''))
199
SELECT a,b,if(b = 1,i,if(b = 2,v,''))
201
LEFT JOIN t2 USING(c)
202
LEFT JOIN t3 ON t3.c = t1.c
204
a b if(b = 1,i,if(b = 2,v,''))
209
SELECT a,b,if(b = 1,i,if(b = 2,v,''))
211
LEFT JOIN t2 ON t1.c = t2.c
212
LEFT JOIN t3 ON t3.c = t1.c
214
a b if(b = 1,i,if(b = 2,v,''))
220
create table t1 (ID int not null primary key, TransactionID int not null);
221
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);
222
create table t2 (ID int not null primary key, GroupID int not null);
223
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);
224
create table t3 (ID int not null primary key, DateOfAction date not null);
225
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');
226
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;
227
DateOfAction TransactionID
239
select 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;
240
DateOfAction TransactionID
254
member_id int(11) NOT NULL auto_increment,
255
inschrijf_datum varchar(20) NOT NULL default '',
256
lastchange_datum varchar(20) NOT NULL default '',
257
nickname varchar(20) NOT NULL default '',
258
password varchar(8) NOT NULL default '',
259
voornaam varchar(30) NOT NULL default '',
260
tussenvoegsels varchar(10) NOT NULL default '',
261
achternaam varchar(50) NOT NULL default '',
262
straat varchar(100) NOT NULL default '',
263
postcode varchar(10) NOT NULL default '',
264
wijk varchar(40) NOT NULL default '',
265
plaats varchar(50) NOT NULL default '',
266
telefoon varchar(10) NOT NULL default '',
267
geboortedatum date NOT NULL default '0000-00-00',
268
geslacht varchar(5) NOT NULL default '',
269
email varchar(80) NOT NULL default '',
270
uin varchar(15) NOT NULL default '',
271
homepage varchar(100) NOT NULL default '',
272
internet varchar(15) NOT NULL default '',
273
scherk varchar(30) NOT NULL default '',
274
favo_boek varchar(50) NOT NULL default '',
275
favo_tijdschrift varchar(50) NOT NULL default '',
276
favo_tv varchar(50) NOT NULL default '',
277
favo_eten varchar(50) NOT NULL default '',
278
favo_muziek varchar(30) NOT NULL default '',
279
info text NOT NULL default '',
280
ipnr varchar(30) NOT NULL default '',
281
PRIMARY KEY (member_id)
282
) ENGINE=MyISAM PACK_KEYS=1;
284
Warning 1101 BLOB/TEXT column 'info' can't have a default value
285
insert into t1 (member_id) values (1),(2),(3);
286
select member_id, nickname, voornaam FROM t1
287
ORDER by lastchange_datum DESC LIMIT 2;
288
member_id nickname voornaam
292
create table t1 (a int not null, b int, c varchar(10), key (a, b, c));
293
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');
294
explain select * from t1 where (a = 1 and b is null and c = 'b') or (a > 2) order by a desc;
295
id select_type table type possible_keys key key_len ref rows Extra
296
1 SIMPLE t1 index a a 22 NULL 11 Using where; Using index
297
select * from t1 where (a = 1 and b is null and c = 'b') or (a > 2) order by a desc;
300
explain select * from t1 where a >= 1 and a < 3 order by a desc;
301
id select_type table type possible_keys key key_len ref rows Extra
302
1 SIMPLE t1 range a a 4 NULL 10 Using where; Using index
303
select * from t1 where a >= 1 and a < 3 order by a desc;
316
explain select * from t1 where a = 1 order by a desc, b desc;
317
id select_type table type possible_keys key key_len ref rows Extra
318
1 SIMPLE t1 ref a a 4 const 5 Using where; Using index
319
select * from t1 where a = 1 order by a desc, b desc;
327
explain select * from t1 where a = 1 and b is null order by a desc, b desc;
328
id select_type table type possible_keys key key_len ref rows Extra
329
1 SIMPLE t1 ref a a 9 const,const 2 Using where; Using index; Using filesort
330
select * from t1 where a = 1 and b is null order by a desc, b desc;
334
explain select * from t1 where a >= 1 and a < 3 and b >0 order by a desc,b desc;
335
id select_type table type possible_keys key key_len ref rows Extra
336
1 SIMPLE t1 range a a 9 NULL 8 Using where; Using index
337
explain select * from t1 where a = 2 and b >0 order by a desc,b desc;
338
id select_type table type possible_keys key key_len ref rows Extra
339
1 SIMPLE t1 range a a 9 NULL 5 Using where; Using index
340
explain select * from t1 where a = 2 and b is null order by a desc,b desc;
341
id select_type table type possible_keys key key_len ref rows Extra
342
1 SIMPLE t1 ref a a 9 const,const 1 Using where; Using index; Using filesort
343
explain select * from t1 where a = 2 and (b is null or b > 0) order by a
345
id select_type table type possible_keys key key_len ref rows Extra
346
1 SIMPLE t1 range a a 9 NULL 6 Using where; Using index
347
explain select * from t1 where a = 2 and b > 0 order by a desc,b desc;
348
id select_type table type possible_keys key key_len ref rows Extra
349
1 SIMPLE t1 range a a 9 NULL 5 Using where; Using index
350
explain select * from t1 where a = 2 and b < 2 order by a desc,b desc;
351
id select_type table type possible_keys key key_len ref rows Extra
352
1 SIMPLE t1 range a a 9 NULL 2 Using where; Using index
353
explain select * from t1 where a = 1 order by b desc;
354
id select_type table type possible_keys key key_len ref rows Extra
355
1 SIMPLE t1 ref a a 4 const 5 Using where; Using index
356
select * from t1 where a = 1 order by b desc;
364
alter table t1 modify b int not null, modify c varchar(10) not null;
366
Warning 1265 Data truncated for column 'b' at row 1
367
Warning 1265 Data truncated for column 'c' at row 1
368
Warning 1265 Data truncated for column 'b' at row 2
369
Warning 1265 Data truncated for column 'c' at row 3
370
explain select * from t1 order by a, b, c;
371
id select_type table type possible_keys key key_len ref rows Extra
372
1 SIMPLE t1 index NULL a 20 NULL 11 Using index
373
select * from t1 order by a, b, c;
386
explain select * from t1 order by a desc, b desc, c desc;
387
id select_type table type possible_keys key key_len ref rows Extra
388
1 SIMPLE t1 index NULL a 20 NULL 11 Using index
389
select * from t1 order by a desc, b desc, c desc;
402
explain select * from t1 where (a = 1 and b = 1 and c = 'b') or (a > 2) order by a desc;
403
id select_type table type possible_keys key key_len ref rows Extra
404
1 SIMPLE t1 range a a 20 NULL 3 Using where; Using index
405
select * from t1 where (a = 1 and b = 1 and c = 'b') or (a > 2) order by a desc;
409
explain select * from t1 where a < 2 and b <= 1 order by a desc, b desc;
410
id select_type table type possible_keys key key_len ref rows Extra
411
1 SIMPLE t1 range a a 4 NULL 6 Using where; Using index
412
select * from t1 where a < 2 and b <= 1 order by a desc, b desc;
419
select count(*) from t1 where a < 5 and b > 0;
422
select * from t1 where a < 5 and b > 0 order by a desc,b desc;
433
explain select * from t1 where a between 1 and 3 and b <= 1 order by a desc, b desc;
434
id select_type table type possible_keys key key_len ref rows Extra
435
1 SIMPLE t1 range a a 8 NULL 10 Using where; Using index
436
select * from t1 where a between 1 and 3 and b <= 1 order by a desc, b desc;
445
explain select * from t1 where a between 0 and 1 order by a desc, b desc;
446
id select_type table type possible_keys key key_len ref rows Extra
447
1 SIMPLE t1 range a a 4 NULL 5 Using where; Using index
448
select * from t1 where a between 0 and 1 order by a desc, b desc;
458
gid int(10) unsigned NOT NULL auto_increment,
459
cid smallint(5) unsigned NOT NULL default '0',
461
KEY component_id (cid)
463
INSERT INTO t1 VALUES (103853,108),(103867,108),(103962,108),(104505,108),(104619,108),(104620,108);
464
ALTER TABLE t1 add skr int(10) not null;
466
gid int(10) unsigned NOT NULL default '0',
467
uid smallint(5) unsigned NOT NULL default '1',
468
sid tinyint(3) unsigned NOT NULL default '1',
473
INSERT INTO t2 VALUES (103853,250,5),(103867,27,5),(103962,27,5),(104505,117,5),(104619,75,5),(104620,15,5);
475
uid smallint(6) NOT NULL auto_increment,
478
INSERT INTO t3 VALUES (1),(15),(27),(75),(117),(250);
479
ALTER TABLE t3 add skr int(10) not null;
480
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;
488
select 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;
496
EXPLAIN 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;
497
id select_type table type possible_keys key key_len ref rows Extra
498
1 SIMPLE t2 ALL PRIMARY,uid NULL NULL NULL 6 Using temporary; Using filesort
499
1 SIMPLE t3 eq_ref PRIMARY PRIMARY 2 test.t2.uid 1 Using where; Using index
500
1 SIMPLE t1 eq_ref PRIMARY PRIMARY 4 test.t2.gid 1 Using index
501
EXPLAIN SELECT t1.gid, t3.uid from t1, t3 where t1.gid = t3.uid order by t1.gid,t3.skr;
502
id select_type table type possible_keys key key_len ref rows Extra
503
1 SIMPLE t3 ALL PRIMARY NULL NULL NULL 6 Using temporary; Using filesort
504
1 SIMPLE t1 eq_ref PRIMARY PRIMARY 4 test.t3.uid 1 Using where; Using index
505
EXPLAIN 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;
506
id select_type table type possible_keys key key_len ref rows Extra
507
1 SIMPLE t2 ALL PRIMARY,uid NULL NULL NULL 6 Using temporary; Using filesort
508
1 SIMPLE t1 eq_ref PRIMARY PRIMARY 4 test.t2.gid 1 Using index
509
1 SIMPLE t3 eq_ref PRIMARY PRIMARY 2 test.t2.uid 1 Using where; Using index
510
EXPLAIN SELECT t1.gid, t3.uid from t1, t3 where t1.gid = t3.uid order by t3.skr,t1.gid;
511
id select_type table type possible_keys key key_len ref rows Extra
512
1 SIMPLE t3 ALL PRIMARY NULL NULL NULL 6 Using temporary; Using filesort
513
1 SIMPLE t1 eq_ref PRIMARY PRIMARY 4 test.t3.uid 1 Using where; Using index
514
EXPLAIN SELECT t1.gid, t3.uid from t1, t3 where t1.skr = t3.uid order by t1.gid,t3.skr;
515
id select_type table type possible_keys key key_len ref rows Extra
516
1 SIMPLE t1 ALL NULL NULL NULL NULL 6 Using temporary; Using filesort
517
1 SIMPLE t3 eq_ref PRIMARY PRIMARY 2 test.t1.skr 1 Using index condition
520
`titre` char(80) NOT NULL default '',
521
`numeropost` mediumint(8) unsigned NOT NULL auto_increment,
522
`date` datetime NOT NULL default '0000-00-00 00:00:00',
523
`auteur` char(35) NOT NULL default '',
524
`icone` tinyint(2) unsigned NOT NULL default '0',
525
`lastauteur` char(35) NOT NULL default '',
526
`nbrep` smallint(6) unsigned NOT NULL default '0',
527
`dest` char(35) NOT NULL default '',
528
`lu` tinyint(1) unsigned NOT NULL default '0',
529
`vue` mediumint(8) unsigned NOT NULL default '0',
530
`ludest` tinyint(1) unsigned NOT NULL default '0',
531
`ouvert` tinyint(1) unsigned NOT NULL default '1',
532
PRIMARY KEY (`numeropost`),
534
KEY `dest` (`dest`,`ludest`),
535
KEY `auteur` (`auteur`,`lu`),
536
KEY `auteur_2` (`auteur`,`date`),
537
KEY `dest_2` (`dest`,`date`)
540
`numeropost` mediumint(8) unsigned NOT NULL default '0',
541
`pseudo` char(35) NOT NULL default '',
542
PRIMARY KEY (`numeropost`,`pseudo`),
543
KEY `pseudo` (`pseudo`)
545
INSERT INTO t1 (titre,auteur,dest) VALUES ('test','joce','bug');
546
INSERT INTO t2 (numeropost,pseudo) VALUES (1,'joce'),(1,'bug');
547
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;
548
titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur dest
549
test 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bug
550
SELECT 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;
551
titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur dest
552
test 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bug
553
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;
554
titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur dest
555
test 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bug
556
SELECT 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;
557
titre numeropost auteur icone nbrep 0 date vue ouvert lastauteur dest
558
test 1 joce 0 0 0 0000-00-00 00:00:00 0 1 bug
560
CREATE TABLE t1 (a int, b int);
561
INSERT INTO t1 VALUES (1, 2);
562
INSERT INTO t1 VALUES (3, 4);
563
INSERT INTO t1 VALUES (5, NULL);
564
SELECT * FROM t1 ORDER BY b;
569
SELECT * FROM t1 ORDER BY b DESC;
574
SELECT * FROM t1 ORDER BY (a + b);
579
SELECT * FROM t1 ORDER BY (a + b) DESC;
585
create table t1(id int not null auto_increment primary key, t char(12));
586
explain select id,t from t1 order by id;
587
id select_type table type possible_keys key key_len ref rows Extra
588
1 SIMPLE t1 ALL NULL NULL NULL NULL 1000 Using filesort
589
explain select id,t from t1 force index (primary) order by id;
590
id select_type table type possible_keys key key_len ref rows Extra
591
1 SIMPLE t1 index NULL PRIMARY 4 NULL 1000
594
FieldKey varchar(36) NOT NULL default '',
595
LongVal bigint(20) default NULL,
596
StringVal mediumtext,
597
KEY FieldKey (FieldKey),
598
KEY LongField (FieldKey,LongVal),
599
KEY StringField (FieldKey,StringVal(32))
601
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');
602
EXPLAIN SELECT * FROM t1 WHERE FieldKey = '1' ORDER BY LongVal;
603
id select_type table type possible_keys key key_len ref rows Extra
604
1 SIMPLE t1 ref FieldKey,LongField,StringField LongField 38 const 3 Using where
605
SELECT * FROM t1 WHERE FieldKey = '1' ORDER BY LongVal;
606
FieldKey LongVal StringVal
610
EXPLAIN SELECT * FROM t1 ignore index (FieldKey, LongField) WHERE FieldKey > '2' ORDER BY LongVal;
611
id select_type table type possible_keys key key_len ref rows Extra
612
1 SIMPLE t1 range StringField StringField 38 NULL 4 Using where; Using filesort
613
SELECT * FROM t1 WHERE FieldKey > '2' ORDER BY LongVal;
614
FieldKey LongVal StringVal
618
EXPLAIN SELECT * FROM t1 WHERE FieldKey > '2' ORDER BY FieldKey, LongVal;
619
id select_type table type possible_keys key key_len ref rows Extra
620
1 SIMPLE t1 range FieldKey,LongField,StringField LongField 38 NULL 4 Using where
621
SELECT * FROM t1 WHERE FieldKey > '2' ORDER BY FieldKey, LongVal;
622
FieldKey LongVal StringVal
627
CREATE TABLE t1 (a INT, b INT);
629
UPDATE t1 SET a=0 ORDER BY (a=@id), b;
631
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;
632
INSERT INTO t1 VALUES (11384, 2),(11392, 2);
633
SELECT id FROM t1 WHERE id <11984 AND menu =2 ORDER BY id DESC LIMIT 1 ;
637
create table t1(a int, b int, index(b));
638
insert into t1 values (2, 1), (1, 1), (4, NULL), (3, NULL), (6, 2), (5, 2);
639
explain select * from t1 where b=1 or b is null order by a;
640
id select_type table type possible_keys key key_len ref rows Extra
641
1 SIMPLE t1 ref_or_null b b 5 const 3 Using filesort
642
select * from t1 where b=1 or b is null order by a;
648
explain select * from t1 where b=2 or b is null order by a;
649
id select_type table type possible_keys key key_len ref rows Extra
650
1 SIMPLE t1 ref_or_null b b 5 const 4 Using filesort
651
select * from t1 where b=2 or b is null order by a;
658
create table t1 (a int not null auto_increment, b int not null, c int not null, d int not null,
659
key(a,b,d), key(c,b,a));
660
create table t2 like t1;
661
insert into t1 values (NULL, 1, 2, 0), (NULL, 2, 1, 1), (NULL, 3, 4, 2), (NULL, 4, 3, 3);
662
insert into t2 select null, b, c, d from t1;
663
insert into t1 select null, b, c, d from t2;
664
insert into t2 select null, b, c, d from t1;
665
insert into t1 select null, b, c, d from t2;
666
insert into t2 select null, b, c, d from t1;
667
insert into t1 select null, b, c, d from t2;
668
insert into t2 select null, b, c, d from t1;
669
insert into t1 select null, b, c, d from t2;
670
insert into t2 select null, b, c, d from t1;
671
insert into t1 select null, b, c, d from t2;
673
Table Op Msg_type Msg_text
674
test.t1 optimize status OK
676
insert into t1 select 1, b, c + (@row:=@row - 1) * 10, d - @row from t2 limit 10;
677
select * from t1 where a=1 and b in (1) order by c, b, a;
683
select * from t1 where a=1 and b in (1);
690
create table t1 (col1 int, col int);
691
create table t2 (col2 int, col int);
692
insert into t1 values (1,1),(2,2),(3,3);
693
insert into t2 values (1,3),(2,2),(3,1);
694
select t1.* , t2.col as t2_col from t1 left join t2 on (t1.col1=t2.col2)
700
select col1 as col, col from t1 order by col;
701
ERROR 23000: Column 'col' in order clause is ambiguous
702
select t1.col as c1, t2.col as c2 from t1, t2 where t1.col1=t2.col2
704
ERROR 23000: Column 'col' in order clause is ambiguous
705
select t1.col as c1, t2.col as c2 from t1, t2 where t1.col1=t2.col2
707
ERROR 23000: Column 'col' in order clause is ambiguous
708
select col1 from t1, t2 where t1.col1=t2.col2 order by col;
709
ERROR 23000: Column 'col' in order clause is ambiguous
710
select t1.col as t1_col, t2.col2 from t1, t2 where t1.col1=t2.col2
712
ERROR 23000: Column 'col' in order clause is ambiguous
713
select t1.col as t1_col, t2.col from t1, t2 where t1.col1=t2.col2
719
select col2 as c, col as c from t2 order by col;
724
select col2 as col, col as col2 from t2 order by col;
729
select t2.col2, t2.col, t2.col from t2 order by col;
734
select t2.col2 as col from t2 order by t2.col;
739
select t2.col2 as col, t2.col from t2 order by t2.col;
744
select t2.col2, t2.col, t2.col from t2 order by t2.col;
750
create table t1 (a char(25));
751
insert into t1 set a = repeat('x', 20);
752
insert into t1 set a = concat(repeat('x', 19), 'z');
753
insert into t1 set a = concat(repeat('x', 19), 'ab');
754
insert into t1 set a = concat(repeat('x', 19), 'aa');
755
set max_sort_length=20;
756
select a from t1 order by a;
758
xxxxxxxxxxxxxxxxxxxab
759
xxxxxxxxxxxxxxxxxxxaa
764
`sid` decimal(8,0) default null,
765
`wnid` varchar(11) not null default '',
766
key `wnid14` (`wnid`(4)),
768
) engine=myisam default charset=latin1;
769
insert into t1 (`sid`, `wnid`) values
770
('10100','01019000000'),('37986','01019000000'),('37987','01019010000'),
771
('39560','01019090000'),('37989','01019000000'),('37990','01019011000'),
772
('37991','01019011000'),('37992','01019019000'),('37993','01019030000'),
773
('37994','01019090000'),('475','02070000000'),('25253','02071100000'),
774
('25255','02071100000'),('25256','02071110000'),('25258','02071130000'),
775
('25259','02071190000'),('25260','02071200000'),('25261','02071210000'),
776
('25262','02071290000'),('25263','02071300000'),('25264','02071310000'),
777
('25265','02071310000'),('25266','02071320000'),('25267','02071320000'),
778
('25269','02071330000'),('25270','02071340000'),('25271','02071350000'),
779
('25272','02071360000'),('25273','02071370000'),('25281','02071391000'),
780
('25282','02071391000'),('25283','02071399000'),('25284','02071400000'),
781
('25285','02071410000'),('25286','02071410000'),('25287','02071420000'),
782
('25288','02071420000'),('25291','02071430000'),('25290','02071440000'),
783
('25292','02071450000'),('25293','02071460000'),('25294','02071470000'),
784
('25295','02071491000'),('25296','02071491000'),('25297','02071499000');
785
explain select * from t1 where wnid like '0101%' order by wnid;
786
id select_type table type possible_keys key key_len ref rows Extra
787
1 SIMPLE t1 range wnid14,wnid wnid 13 NULL 10 Using where
788
select * from t1 where wnid like '0101%' order by wnid;
801
CREATE TABLE t1 (a int);
802
INSERT INTO t1 VALUES (2), (1), (1), (2), (1);
803
SELECT a FROM t1 ORDER BY a;
810
(SELECT a FROM t1) ORDER BY a;
818
CREATE TABLE t1 (a int, b int);
819
INSERT INTO t1 VALUES (1,30), (2,20), (1,10), (2,30), (1,20), (2,10);
820
(SELECT b,a FROM t1 ORDER BY a,b) ORDER BY b,a;
828
(SELECT b FROM t1 ORDER BY b DESC) ORDER BY b ASC;
836
(SELECT b,a FROM t1 ORDER BY b,a) ORDER BY a,b;
844
(SELECT b,a FROM t1 ORDER by b,a LIMIT 3) ORDER by a,b;
850
CREATE TABLE t1 (a INT);
851
INSERT INTO t1 VALUES (1),(2);
852
SELECT a + 1 AS num FROM t1 ORDER BY 30 - num;
856
SELECT CONCAT('test', a) AS str FROM t1 ORDER BY UPPER(str);
860
SELECT a + 1 AS num FROM t1 GROUP BY 30 - num;
864
SELECT a + 1 AS num FROM t1 HAVING 30 - num;
868
SELECT a + 1 AS num, num + 1 FROM t1;
869
ERROR 42S22: Unknown column 'num' in 'field list'
870
SELECT a + 1 AS num, (select num + 2 FROM t1 LIMIT 1) FROM t1;
871
num (select num + 2 FROM t1 LIMIT 1)
874
SELECT a.a + 1 AS num FROM t1 a JOIN t1 b ON num = b.a;
875
ERROR 42S22: Unknown column 'num' in 'on clause'
877
CREATE TABLE bug25126 (
878
val int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY
880
UPDATE bug25126 SET MissingCol = MissingCol;
881
ERROR 42S22: Unknown column 'MissingCol' in 'field list'
882
UPDATE bug25126 SET val = val ORDER BY MissingCol;
883
ERROR 42S22: Unknown column 'MissingCol' in 'order clause'
884
UPDATE bug25126 SET val = val ORDER BY val;
885
UPDATE bug25126 SET val = 1 ORDER BY val;
886
UPDATE bug25126 SET val = 1 ORDER BY MissingCol;
887
ERROR 42S22: Unknown column 'MissingCol' in 'order clause'
888
UPDATE bug25126 SET val = 1 ORDER BY val, MissingCol;
889
ERROR 42S22: Unknown column 'MissingCol' in 'order clause'
890
UPDATE bug25126 SET val = MissingCol ORDER BY MissingCol;
891
ERROR 42S22: Unknown column 'MissingCol' in 'order clause'
892
UPDATE bug25126 SET MissingCol = 1 ORDER BY val, MissingCol;
893
ERROR 42S22: Unknown column 'MissingCol' in 'order clause'
894
UPDATE bug25126 SET MissingCol = 1 ORDER BY MissingCol;
895
ERROR 42S22: Unknown column 'MissingCol' in 'order clause'
896
UPDATE bug25126 SET MissingCol = val ORDER BY MissingCol;
897
ERROR 42S22: Unknown column 'MissingCol' in 'order clause'
898
UPDATE bug25126 SET MissingCol = MissingCol ORDER BY MissingCol;
899
ERROR 42S22: Unknown column 'MissingCol' in 'order clause'
901
CREATE TABLE t1 (a int);
902
SELECT p.a AS val, q.a AS val1 FROM t1 p, t1 q ORDER BY val > 1;
904
SELECT p.a AS val, q.a AS val FROM t1 p, t1 q ORDER BY val;
905
ERROR 23000: Column 'val' in order clause is ambiguous
906
SELECT p.a AS val, q.a AS val FROM t1 p, t1 q ORDER BY val > 1;
907
ERROR 23000: Column 'val' in order clause is ambiguous
909
CREATE TABLE t1 (a int);
910
INSERT INTO t1 VALUES (3), (2), (4), (1);
911
SELECT a, IF(a IN (2,3), a, a+10) FROM t1
912
ORDER BY IF(a IN (2,3), a, a+10);
913
a IF(a IN (2,3), a, a+10)
918
SELECT a, IF(a NOT IN (2,3), a, a+10) FROM t1
919
ORDER BY IF(a NOT IN (2,3), a, a+10);
920
a IF(a NOT IN (2,3), a, a+10)
925
SELECT a, IF(a IN (2,3), a, a+10) FROM t1
926
ORDER BY IF(a NOT IN (2,3), a, a+10);
927
a IF(a IN (2,3), a, a+10)
932
SELECT a, IF(a BETWEEN 2 AND 3, a, a+10) FROM t1
933
ORDER BY IF(a BETWEEN 2 AND 3, a, a+10);
934
a IF(a BETWEEN 2 AND 3, a, a+10)
939
SELECT a, IF(a NOT BETWEEN 2 AND 3, a, a+10) FROM t1
940
ORDER BY IF(a NOT BETWEEN 2 AND 3, a, a+10);
941
a IF(a NOT BETWEEN 2 AND 3, a, a+10)
946
SELECT a, IF(a BETWEEN 2 AND 3, a, a+10) FROM t1
947
ORDER BY IF(a NOT BETWEEN 2 AND 3, a, a+10);
948
a IF(a BETWEEN 2 AND 3, a, a+10)
953
SELECT IF(a IN (1,2), a, '') as x1, IF(a NOT IN (1,2), a, '') as x2
954
FROM t1 GROUP BY x1, x2;
960
SELECT IF(a IN (1,2), a, '') as x1, IF(a NOT IN (1,2), a, '') as x2
961
FROM t1 GROUP BY x1, IF(a NOT IN (1,2), a, '');
967
SELECT a, a IN (1,2) FROM t1 ORDER BY a IN (1,2);
973
SELECT a FROM t1 ORDER BY a IN (1,2);
979
SELECT a+10 FROM t1 ORDER BY a IN (1,2);
985
SELECT a, IF(a IN (1,2), a, a+10) FROM t1
986
ORDER BY IF(a IN (3,4), a, a+10);
987
a IF(a IN (1,2), a, a+10)
993
create table t1 (a int not null, b int not null, c int not null);
994
insert t1 values (1,1,1),(1,1,2),(1,2,1);
995
select a, b from t1 group by a, b order by sum(c);
1000
CREATE TABLE t1 (a int, b int, PRIMARY KEY (a));
1001
INSERT INTO t1 VALUES (1,1), (2,2), (3,3);
1002
explain SELECT t1.b as a, t2.b as c FROM
1003
t1 LEFT JOIN t1 t2 ON (t1.a = t2.a AND t2.a = 2)
1005
id select_type table type possible_keys key key_len ref rows Extra
1006
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using temporary; Using filesort
1007
1 SIMPLE t2 const PRIMARY PRIMARY 4 const 1
1008
SELECT t2.b as c FROM
1009
t1 LEFT JOIN t1 t2 ON (t1.a = t2.a AND t2.a = 2)
1015
explain SELECT t1.b as a, t2.b as c FROM
1016
t1 JOIN t1 t2 ON (t1.a = t2.a AND t2.a = 2)
1018
id select_type table type possible_keys key key_len ref rows Extra
1019
1 SIMPLE t1 const PRIMARY PRIMARY 4 const 1
1020
1 SIMPLE t2 const PRIMARY PRIMARY 4 const 1
1021
CREATE TABLE t2 LIKE t1;
1022
INSERT INTO t2 SELECT * from t1;
1023
CREATE TABLE t3 LIKE t1;
1024
INSERT INTO t3 SELECT * from t1;
1025
CREATE TABLE t4 LIKE t1;
1026
INSERT INTO t4 SELECT * from t1;
1027
INSERT INTO t1 values (0,0),(4,4);
1028
SELECT t2.b FROM t1 LEFT JOIN (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
1029
ON (t1.a=t2.a AND t1.b=t3.b) order by t2.b;
1036
DROP TABLE t1,t2,t3,t4;
1037
create table t1 (a int, b int, c int);
1038
insert into t1 values (1,2,3), (9,8,3), (19,4,3), (1,4,9);
1039
select a,(sum(b)/sum(c)) as ratio from t1 group by a order by sum(b)/sum(c) asc;
1045
CREATE TABLE t1 (a INT UNSIGNED NOT NULL, b TIME);
1046
INSERT INTO t1 (a) VALUES (100000), (0), (100), (1000000),(10000), (1000), (10);
1047
UPDATE t1 SET b = SEC_TO_TIME(a);
1048
SELECT a, b FROM t1 ORDER BY b DESC;
1057
SELECT a, b FROM t1 ORDER BY SEC_TO_TIME(a) DESC;
1067
CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), UNIQUE KEY b (b));
1068
INSERT INTO t1 VALUES (1,1),(2,2);
1069
CREATE TABLE t2 (a INT, b INT, KEY a (a,b));
1070
INSERT INTO t2 VALUES (1,1),(1,2),(2,1),(2,2);
1071
EXPLAIN SELECT 1 FROM t1,t2 WHERE t1.b=2 AND t1.a=t2.a ORDER BY t2.b;
1072
id select_type table type possible_keys key key_len ref rows Extra
1073
1 SIMPLE t1 const PRIMARY,b b 5 const 1
1074
1 SIMPLE t2 ref a a 5 const 2 Using where; Using index
1077
id int auto_increment PRIMARY KEY, c2 int, c3 int, INDEX k2(c2), INDEX k3(c3));
1078
INSERT INTO t1 (c2,c3) VALUES
1079
(31,34),(35,38),(34,31),(32,35),(31,39),
1080
(11,14),(15,18),(14,11),(12,15),(11,19);
1081
INSERT INTO t1 (c2,c3) SELECT c2,c3 FROM t1;
1082
INSERT INTO t1 (c2,c3) SELECT c2,c3 FROM t1;
1083
INSERT INTO t1 (c2,c3) SELECT c2,c3 FROM t1;
1084
INSERT INTO t1 (c2,c3) SELECT c2,c3 FROM t1;
1085
INSERT INTO t1 (c2,c3) SELECT c2,c3 FROM t1;
1086
INSERT INTO t1 (c2,c3) SELECT c2,c3 FROM t1;
1087
INSERT INTO t1 (c2,c3) SELECT c2,c3 FROM t1;
1088
INSERT INTO t1 (c2,c3) SELECT c2,c3 FROM t1;
1089
INSERT INTO t1 (c2,c3) SELECT c2,c3 FROM t1;
1090
INSERT INTO t1 (c2,c3) SELECT c2,c3 FROM t1;
1091
INSERT INTO t1 (c2,c3) SELECT c2,c3 FROM t1;
1092
INSERT INTO t1 (c2,c3) SELECT c2,c3 FROM t1;
1093
UPDATE t1 SET c2=20 WHERE id%100 = 0;
1094
SELECT COUNT(*) FROM t1;
1097
CREATE TABLE t2 LIKE t1;
1098
INSERT INTO t2 SELECT * FROM t1 ORDER BY id;
1099
EXPLAIN SELECT id,c3 FROM t2 WHERE c2=11 ORDER BY c3 LIMIT 20;
1100
id select_type table type possible_keys key key_len ref rows Extra
1101
1 SIMPLE t2 index k2 k3 5 NULL 111 Using where
1102
EXPLAIN SELECT id,c3 FROM t2 WHERE c2=11 ORDER BY c3 LIMIT 4000;
1103
id select_type table type possible_keys key key_len ref rows Extra
1104
1 SIMPLE t2 ref k2 k2 5 const 7341 Using where; Using filesort
1105
EXPLAIN SELECT id,c3 FROM t2 WHERE c2 BETWEEN 10 AND 12 ORDER BY c3 LIMIT 20;
1106
id select_type table type possible_keys key key_len ref rows Extra
1107
1 SIMPLE t2 index k2 k3 5 NULL 73 Using where
1108
EXPLAIN SELECT id,c3 FROM t2 WHERE c2 BETWEEN 20 AND 30 ORDER BY c3 LIMIT 4000;
1109
id select_type table type possible_keys key key_len ref rows Extra
1110
1 SIMPLE t2 range k2 k2 5 NULL 386 Using index condition; Using where; Using MRR; Using filesort
1111
SELECT id,c3 FROM t2 WHERE c2=11 ORDER BY c3 LIMIT 20;
1140
INSERT INTO t1 VALUES (1,1),(2,2),(3,3),(4,4);
1141
INSERT INTO t1 SELECT a + 4, b + 4 FROM t1;
1142
INSERT INTO t1 SELECT a + 8, b + 8 FROM t1;
1143
INSERT INTO t1 SELECT a +16, b +16 FROM t1;
1144
INSERT INTO t1 SELECT a +32, b +32 FROM t1;
1145
INSERT INTO t1 SELECT a +64, b +64 FROM t1;
1146
EXPLAIN SELECT a FROM t1 IGNORE INDEX FOR GROUP BY (a, ab) GROUP BY a;
1147
id select_type table type possible_keys key key_len ref rows Extra
1148
1 SIMPLE t1 range NULL ab 4 NULL 10 Using index for group-by
1149
SELECT a FROM t1 IGNORE INDEX FOR GROUP BY (a, ab) GROUP BY a;
1279
SELECT @tmp_tables_after = @tmp_tables_before ;
1280
@tmp_tables_after = @tmp_tables_before
1282
EXPLAIN SELECT a FROM t1 IGNORE INDEX FOR ORDER BY (a, ab) ORDER BY a;
1283
id select_type table type possible_keys key key_len ref rows Extra
1284
1 SIMPLE t1 index NULL PRIMARY 4 NULL 128 Using index
1285
SELECT a FROM t1 IGNORE INDEX FOR ORDER BY (a, ab) ORDER BY a;
1415
SELECT @tmp_tables_after = @tmp_tables_before;
1416
@tmp_tables_after = @tmp_tables_before
1420
# Bug#31590: Wrong error message on sort buffer being too small.
1422
create table t1(a int, b tinytext);
1423
insert into t1 values (1,2),(3,2);
1424
set session sort_buffer_size= 30000;
1426
Warning 1292 Truncated incorrect sort_buffer_size value: '30000'
1427
set session max_sort_length= 2180;
1428
select * from t1 order by b;
1429
ERROR HY001: Out of sort memory; increase server sort buffer size
1431
CREATE TABLE t2 (a varchar(32), b int(11), c float, d double,
1432
UNIQUE KEY a (a,b,c), KEY b (b), KEY c (c));
1433
CREATE TABLE t1 (a varchar(32), b char(3), UNIQUE KEY a (a,b), KEY b (b));
1434
CREATE TABLE t3 (a varchar(32), b char(3), UNIQUE KEY a (a,b));
1435
INSERT INTO t3 SELECT * FROM t1;
1437
SELECT d FROM t1, t2
1438
WHERE t2.b=14 AND t2.a=t1.a AND 5.1<t2.c AND t1.b='DE'
1439
ORDER BY t2.c LIMIT 1;
1440
id select_type table type possible_keys key key_len ref rows Extra
1441
1 SIMPLE t1 ref a,b b 4 const 4 Using index condition; Using temporary; Using filesort
1442
1 SIMPLE t2 ref a,b,c a 40 test.t1.a,const 11 Using index condition
1443
SELECT d FROM t1, t2
1444
WHERE t2.b=14 AND t2.a=t1.a AND 5.1<t2.c AND t1.b='DE'
1445
ORDER BY t2.c LIMIT 1;
1449
SELECT d FROM t3 AS t1, t2 AS t2
1450
WHERE t2.b=14 AND t2.a=t1.a AND 5.1<t2.c AND t1.b='DE'
1451
ORDER BY t2.c LIMIT 1;
1452
id select_type table type possible_keys key key_len ref rows Extra
1453
1 SIMPLE t2 range a,b,c c 5 NULL 420 Using where; Using MRR
1454
1 SIMPLE t1 ref a a 39 test.t2.a,const 10 Using where; Using index
1455
SELECT d FROM t3 AS t1, t2 AS t2
1456
WHERE t2.b=14 AND t2.a=t1.a AND 5.1<t2.c AND t1.b='DE'
1457
ORDER BY t2.c LIMIT 1;
1460
DROP TABLE t1,t2,t3;