You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1066 lines
32 KiB

23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
23 years ago
22 years ago
22 years ago
22 years ago
19 years ago
  1. drop table if exists t1,t2,t3;
  2. CREATE TABLE t1 (
  3. id int(6) DEFAULT '0' NOT NULL,
  4. idservice int(5),
  5. clee char(20) NOT NULL,
  6. flag char(1),
  7. KEY id (id),
  8. PRIMARY KEY (clee)
  9. );
  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');
  17. CREATE TABLE t2 (
  18. id int(6) NOT NULL auto_increment,
  19. description varchar(40) NOT NULL,
  20. idform varchar(40),
  21. ordre int(6) unsigned DEFAULT '0' NOT NULL,
  22. image varchar(60),
  23. PRIMARY KEY (id),
  24. KEY id (id,ordre)
  25. );
  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
  44. drop table t1,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;
  50. name
  51. Allan Larsson
  52. David Axmark
  53. Michael Widenius
  54. select concat(last," ",first) as name from t1 order by name;
  55. name
  56. Axmark David
  57. Larsson Allan
  58. Widenius Michael
  59. drop table t1;
  60. create table t1 (i int);
  61. insert into t1 values(1),(2),(1),(2),(1),(2),(3);
  62. select distinct i from t1;
  63. i
  64. 1
  65. 2
  66. 3
  67. select distinct i from t1 order by rand(5);
  68. i
  69. 1
  70. 3
  71. 2
  72. select distinct i from t1 order by i desc;
  73. i
  74. 3
  75. 2
  76. 1
  77. select distinct i from t1 order by 1-i;
  78. i
  79. 3
  80. 2
  81. 1
  82. select distinct i from t1 order by mod(i,2),i;
  83. i
  84. 2
  85. 1
  86. 3
  87. drop table t1;
  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
  92. Alpha 001
  93. Beta 200c
  94. Gamma 123
  95. Gamma Ext 123a
  96. drop table t1;
  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;
  100. id col1 col2
  101. 4 1 1
  102. 3 1 2
  103. 5 1 4
  104. 2 2 1
  105. 1 2 2
  106. 6 2 3
  107. 8 2 4
  108. 7 3 1
  109. select col1 from t1 order by id;
  110. col1
  111. 2
  112. 2
  113. 1
  114. 1
  115. 1
  116. 2
  117. 3
  118. 2
  119. select col1 as id from t1 order by id;
  120. id
  121. 1
  122. 1
  123. 1
  124. 2
  125. 2
  126. 2
  127. 2
  128. 3
  129. select concat(col1) as id from t1 order by id;
  130. id
  131. 1
  132. 1
  133. 1
  134. 2
  135. 2
  136. 2
  137. 2
  138. 3
  139. drop table t1;
  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;
  146. test
  147. 1
  148. 2
  149. 3
  150. 7
  151. drop table t1;
  152. CREATE TABLE t1
  153. (
  154. a int unsigned NOT NULL,
  155. b int unsigned NOT NULL,
  156. c int unsigned NOT NULL,
  157. UNIQUE(a),
  158. INDEX(b),
  159. INDEX(c)
  160. );
  161. CREATE TABLE t2
  162. (
  163. c int unsigned NOT NULL,
  164. i int unsigned NOT NULL,
  165. INDEX(c)
  166. );
  167. CREATE TABLE t3
  168. (
  169. c int unsigned NOT NULL,
  170. v varchar(64),
  171. INDEX(c)
  172. );
  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,''))
  182. FROM t1
  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,''))
  186. 1 1 50
  187. 2 1 25
  188. 3 2 123 Park Place
  189. 4 2 453 Boardwalk
  190. SELECT a,b,if(b = 1,i,if(b = 2,v,''))
  191. FROM t1
  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,''))
  195. 1 1 50
  196. 2 1 25
  197. 3 2 123 Park Place
  198. 4 2 453 Boardwalk
  199. SELECT a,b,if(b = 1,i,if(b = 2,v,''))
  200. FROM t1
  201. LEFT JOIN t2 USING(c)
  202. LEFT JOIN t3 ON t3.c = t1.c
  203. ORDER BY a;
  204. a b if(b = 1,i,if(b = 2,v,''))
  205. 1 1 50
  206. 2 1 25
  207. 3 2 123 Park Place
  208. 4 2 453 Boardwalk
  209. SELECT a,b,if(b = 1,i,if(b = 2,v,''))
  210. FROM t1
  211. LEFT JOIN t2 ON t1.c = t2.c
  212. LEFT JOIN t3 ON t3.c = t1.c
  213. ORDER BY a;
  214. a b if(b = 1,i,if(b = 2,v,''))
  215. 1 1 50
  216. 2 1 25
  217. 3 2 123 Park Place
  218. 4 2 453 Boardwalk
  219. drop table t1,t2,t3;
  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
  228. 1999-07-18 486
  229. 1999-07-19 87
  230. 1999-07-19 89
  231. 1999-07-19 92
  232. 1999-07-19 94
  233. 1999-07-27 828
  234. 1999-07-27 832
  235. 1999-07-27 834
  236. 1999-07-27 840
  237. 2000-03-27 490
  238. 2000-03-28 753
  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
  241. 1999-07-19 87
  242. 1999-07-19 89
  243. 1999-07-19 92
  244. 1999-07-19 94
  245. 1999-07-18 486
  246. 2000-03-27 490
  247. 2000-03-28 753
  248. 1999-07-27 828
  249. 1999-07-27 832
  250. 1999-07-27 834
  251. 1999-07-27 840
  252. drop table t1,t2,t3;
  253. CREATE TABLE t1 (
  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;
  283. Warnings:
  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
  289. 1
  290. 2
  291. drop table t1;
  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;
  298. a b c
  299. 1 NULL b
  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;
  304. a b c
  305. 2 3 c
  306. 2 2 b
  307. 2 2 a
  308. 2 1 b
  309. 2 1 a
  310. 1 3 b
  311. 1 1 b
  312. 1 1 b
  313. 1 1 NULL
  314. 1 NULL b
  315. 1 NULL NULL
  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;
  320. a b c
  321. 1 3 b
  322. 1 1 b
  323. 1 1 b
  324. 1 1 NULL
  325. 1 NULL b
  326. 1 NULL NULL
  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;
  331. a b c
  332. 1 NULL NULL
  333. 1 NULL b
  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
  344. desc,b desc;
  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;
  357. a b c
  358. 1 3 b
  359. 1 1 b
  360. 1 1 b
  361. 1 1 NULL
  362. 1 NULL b
  363. 1 NULL NULL
  364. alter table t1 modify b int not null, modify c varchar(10) not null;
  365. Warnings:
  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;
  374. a b c
  375. 1 0
  376. 1 0 b
  377. 1 1
  378. 1 1 b
  379. 1 1 b
  380. 1 3 b
  381. 2 1 a
  382. 2 1 b
  383. 2 2 a
  384. 2 2 b
  385. 2 3 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;
  390. a b c
  391. 2 3 c
  392. 2 2 b
  393. 2 2 a
  394. 2 1 b
  395. 2 1 a
  396. 1 3 b
  397. 1 1 b
  398. 1 1 b
  399. 1 1
  400. 1 0 b
  401. 1 0
  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;
  406. a b c
  407. 1 1 b
  408. 1 1 b
  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;
  413. a b c
  414. 1 1 b
  415. 1 1 b
  416. 1 1
  417. 1 0 b
  418. 1 0
  419. select count(*) from t1 where a < 5 and b > 0;
  420. count(*)
  421. 9
  422. select * from t1 where a < 5 and b > 0 order by a desc,b desc;
  423. a b c
  424. 2 3 c
  425. 2 2 b
  426. 2 2 a
  427. 2 1 b
  428. 2 1 a
  429. 1 3 b
  430. 1 1 b
  431. 1 1 b
  432. 1 1
  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;
  437. a b c
  438. 2 1 b
  439. 2 1 a
  440. 1 1 b
  441. 1 1 b
  442. 1 1
  443. 1 0 b
  444. 1 0
  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;
  449. a b c
  450. 1 3 b
  451. 1 1 b
  452. 1 1 b
  453. 1 1
  454. 1 0 b
  455. 1 0
  456. drop table t1;
  457. CREATE TABLE t1 (
  458. gid int(10) unsigned NOT NULL auto_increment,
  459. cid smallint(5) unsigned NOT NULL default '0',
  460. PRIMARY KEY (gid),
  461. KEY component_id (cid)
  462. ) ENGINE=MyISAM;
  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;
  465. CREATE TABLE t2 (
  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',
  469. PRIMARY KEY (gid),
  470. KEY uid (uid),
  471. KEY status_id (sid)
  472. ) ENGINE=MyISAM;
  473. INSERT INTO t2 VALUES (103853,250,5),(103867,27,5),(103962,27,5),(104505,117,5),(104619,75,5),(104620,15,5);
  474. CREATE TABLE t3 (
  475. uid smallint(6) NOT NULL auto_increment,
  476. PRIMARY KEY (uid)
  477. ) ENGINE=MyISAM;
  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;
  481. gid sid uid
  482. 104620 5 15
  483. 103867 5 27
  484. 103962 5 27
  485. 104619 5 75
  486. 104505 5 117
  487. 103853 5 250
  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;
  489. gid sid uid
  490. 104620 5 15
  491. 103867 5 27
  492. 103962 5 27
  493. 104619 5 75
  494. 104505 5 117
  495. 103853 5 250
  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 t1 index PRIMARY PRIMARY 4 NULL 6 Using index
  499. 1 SIMPLE t2 eq_ref PRIMARY,uid PRIMARY 4 test.t1.gid 1
  500. 1 SIMPLE t3 eq_ref PRIMARY PRIMARY 2 test.t2.uid 1 Using where; 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 t1 index PRIMARY PRIMARY 4 NULL 6 Using index; Using temporary; Using filesort
  508. 1 SIMPLE t2 eq_ref PRIMARY,uid PRIMARY 4 test.t1.gid 1
  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 where
  518. drop table t1,t2,t3;
  519. CREATE TABLE t1 (
  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`),
  533. KEY `date` (`date`),
  534. KEY `dest` (`dest`,`ludest`),
  535. KEY `auteur` (`auteur`,`lu`),
  536. KEY `auteur_2` (`auteur`,`date`),
  537. KEY `dest_2` (`dest`,`date`)
  538. ) CHECKSUM=1;
  539. CREATE TABLE t2 (
  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`)
  544. );
  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
  559. drop table t1,t2;
  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;
  565. a b
  566. 5 NULL
  567. 1 2
  568. 3 4
  569. SELECT * FROM t1 ORDER BY b DESC;
  570. a b
  571. 3 4
  572. 1 2
  573. 5 NULL
  574. SELECT * FROM t1 ORDER BY (a + b);
  575. a b
  576. 5 NULL
  577. 1 2
  578. 3 4
  579. SELECT * FROM t1 ORDER BY (a + b) DESC;
  580. a b
  581. 3 4
  582. 1 2
  583. 5 NULL
  584. DROP TABLE t1;
  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
  592. drop table t1;
  593. CREATE TABLE t1 (
  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))
  600. );
  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
  607. 1 0 2
  608. 1 1 3
  609. 1 2 1
  610. EXPLAIN SELECT * FROM t1 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 FieldKey,LongField,StringField FieldKey 38 NULL 4 Using where; Using filesort
  613. SELECT * FROM t1 WHERE FieldKey > '2' ORDER BY LongVal;
  614. FieldKey LongVal StringVal
  615. 3 1 2
  616. 3 2 1
  617. 3 3 3
  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
  623. 3 1 2
  624. 3 2 1
  625. 3 3 3
  626. DROP TABLE t1;
  627. CREATE TABLE t1 (a INT, b INT);
  628. SET @id=0;
  629. UPDATE t1 SET a=0 ORDER BY (a=@id), b;
  630. DROP TABLE t1;
  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 ;
  634. id
  635. 11392
  636. drop table t1;
  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 where; Using filesort
  642. select * from t1 where b=1 or b is null order by a;
  643. a b
  644. 1 1
  645. 2 1
  646. 3 NULL
  647. 4 NULL
  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 where; Using filesort
  651. select * from t1 where b=2 or b is null order by a;
  652. a b
  653. 3 NULL
  654. 4 NULL
  655. 5 2
  656. 6 2
  657. drop table t1;
  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;
  672. optimize table t1;
  673. Table Op Msg_type Msg_text
  674. test.t1 optimize status OK
  675. set @row=10;
  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;
  678. a b c d
  679. 1 1 2 0
  680. 1 1 12 -1
  681. 1 1 52 -5
  682. 1 1 92 -9
  683. select * from t1 where a=1 and b in (1);
  684. a b c d
  685. 1 1 92 -9
  686. 1 1 52 -5
  687. 1 1 12 -1
  688. 1 1 2 0
  689. drop table t1, t2;
  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)
  695. order by col;
  696. col1 col t2_col
  697. 1 1 3
  698. 2 2 2
  699. 3 3 1
  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
  703. order by col;
  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
  706. order by col;
  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
  711. order by col;
  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
  714. order by col;
  715. t1_col col
  716. 3 1
  717. 2 2
  718. 1 3
  719. select col2 as c, col as c from t2 order by col;
  720. c c
  721. 3 1
  722. 2 2
  723. 1 3
  724. select col2 as col, col as col2 from t2 order by col;
  725. col col2
  726. 1 3
  727. 2 2
  728. 3 1
  729. select t2.col2, t2.col, t2.col from t2 order by col;
  730. col2 col col
  731. 3 1 1
  732. 2 2 2
  733. 1 3 3
  734. select t2.col2 as col from t2 order by t2.col;
  735. col
  736. 3
  737. 2
  738. 1
  739. select t2.col2 as col, t2.col from t2 order by t2.col;
  740. col col
  741. 3 1
  742. 2 2
  743. 1 3
  744. select t2.col2, t2.col, t2.col from t2 order by t2.col;
  745. col2 col col
  746. 3 1 1
  747. 2 2 2
  748. 1 3 3
  749. drop table t1, t2;
  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;
  757. a
  758. xxxxxxxxxxxxxxxxxxxab
  759. xxxxxxxxxxxxxxxxxxxaa
  760. xxxxxxxxxxxxxxxxxxxx
  761. xxxxxxxxxxxxxxxxxxxz
  762. drop table t1;
  763. create table t1 (
  764. `sid` decimal(8,0) default null,
  765. `wnid` varchar(11) not null default '',
  766. key `wnid14` (`wnid`(4)),
  767. key `wnid` (`wnid`)
  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;
  789. sid wnid
  790. 10100 01019000000
  791. 37986 01019000000
  792. 37989 01019000000
  793. 37987 01019010000
  794. 37990 01019011000
  795. 37991 01019011000
  796. 37992 01019019000
  797. 37993 01019030000
  798. 39560 01019090000
  799. 37994 01019090000
  800. drop table t1;
  801. CREATE TABLE t1 (a int);
  802. INSERT INTO t1 VALUES (2), (1), (1), (2), (1);
  803. SELECT a FROM t1 ORDER BY a;
  804. a
  805. 1
  806. 1
  807. 1
  808. 2
  809. 2
  810. (SELECT a FROM t1) ORDER BY a;
  811. a
  812. 1
  813. 1
  814. 1
  815. 2
  816. 2
  817. DROP TABLE t1;
  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;
  821. b a
  822. 10 1
  823. 10 2
  824. 20 1
  825. 20 2
  826. 30 1
  827. 30 2
  828. (SELECT b FROM t1 ORDER BY b DESC) ORDER BY b ASC;
  829. b
  830. 10
  831. 10
  832. 20
  833. 20
  834. 30
  835. 30
  836. (SELECT b,a FROM t1 ORDER BY b,a) ORDER BY a,b;
  837. b a
  838. 10 1
  839. 20 1
  840. 30 1
  841. 10 2
  842. 20 2
  843. 30 2
  844. (SELECT b,a FROM t1 ORDER by b,a LIMIT 3) ORDER by a,b;
  845. b a
  846. 10 1
  847. 20 1
  848. 10 2
  849. DROP TABLE t1;
  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;
  853. num
  854. 3
  855. 2
  856. SELECT CONCAT('test', a) AS str FROM t1 ORDER BY UPPER(str);
  857. str
  858. test1
  859. test2
  860. SELECT a + 1 AS num FROM t1 GROUP BY 30 - num;
  861. num
  862. 3
  863. 2
  864. SELECT a + 1 AS num FROM t1 HAVING 30 - num;
  865. num
  866. 2
  867. 3
  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)
  872. 2 4
  873. 3 5
  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'
  876. DROP TABLE t1;
  877. CREATE TABLE bug25126 (
  878. val int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY
  879. );
  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'
  900. DROP TABLE bug25126;
  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;
  903. val val1
  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
  908. DROP TABLE t1;
  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)
  914. 2 2
  915. 3 3
  916. 1 11
  917. 4 14
  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)
  921. 1 1
  922. 4 4
  923. 2 12
  924. 3 13
  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)
  928. 1 11
  929. 4 14
  930. 2 2
  931. 3 3
  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)
  935. 2 2
  936. 3 3
  937. 1 11
  938. 4 14
  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)
  942. 1 1
  943. 4 4
  944. 2 12
  945. 3 13
  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)
  949. 1 11
  950. 4 14
  951. 2 2
  952. 3 3
  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;
  955. x1 x2
  956. 3
  957. 4
  958. 1
  959. 2
  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, '');
  962. x1 x2
  963. 3
  964. 4
  965. 1
  966. 2
  967. SELECT a, a IN (1,2) FROM t1 ORDER BY a IN (1,2);
  968. a a IN (1,2)
  969. 3 0
  970. 4 0
  971. 2 1
  972. 1 1
  973. SELECT a FROM t1 ORDER BY a IN (1,2);
  974. a
  975. 3
  976. 4
  977. 2
  978. 1
  979. SELECT a+10 FROM t1 ORDER BY a IN (1,2);
  980. a+10
  981. 13
  982. 14
  983. 12
  984. 11
  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)
  988. 3 13
  989. 4 14
  990. 1 1
  991. 2 2
  992. DROP TABLE t1;
  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);
  996. a b
  997. 1 2
  998. 1 1
  999. drop table t1;
  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)
  1004. ORDER BY c;
  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)
  1010. ORDER BY c;
  1011. c
  1012. NULL
  1013. NULL
  1014. 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)
  1017. ORDER BY c;
  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;
  1030. b
  1031. NULL
  1032. NULL
  1033. 1
  1034. 2
  1035. 3
  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;
  1040. a ratio
  1041. 1 0.5000
  1042. 19 1.3333
  1043. 9 2.6667
  1044. drop table t1;
  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;
  1049. a b
  1050. 1000000 277:46:40
  1051. 100000 27:46:40
  1052. 10000 02:46:40
  1053. 1000 00:16:40
  1054. 100 00:01:40
  1055. 10 00:00:10
  1056. 0 00:00:00
  1057. SELECT a, b FROM t1 ORDER BY SEC_TO_TIME(a) DESC;
  1058. a b
  1059. 1000000 277:46:40
  1060. 100000 27:46:40
  1061. 10000 02:46:40
  1062. 1000 00:16:40
  1063. 100 00:01:40
  1064. 10 00:00:10
  1065. 0 00:00:00
  1066. DROP TABLE t1;