Problem Solution Discussion Using ORDER BY to Sort Query Results

inst ruct ions for creat ing t he t ables used in t he exam ples relat ing t o t he baseball1.com baseball dat abase.

6.2 Using ORDER BY to Sort Query Results

6.2.1 Problem

Out put from a query doesnt com e out in t he order you want .

6.2.2 Solution

Add an ORDER BY clause t o t he query.

6.2.3 Discussion

The cont ent s of t he driver_log and mail t ables shown in t he chapt er int roduct ion are disorganized and difficult t o m ake any sense of. The except ion is t hat t he values in t he id and t colum ns are in order, but t hat s j ust coincident al. Rows do t end t o be ret urned from a t able in t he order t hey were originally insert ed, but only unt il t he t able is subj ect ed t o delet e and updat e operat ions. Rows insert ed aft er t hat are likely t o be ret urned in t he m iddle of t he result set som ewhere. Many MySQL users not ice t his dist urbance in row ret rieval order, which leads t hem t o ask, How can I st ore rows in m y t able so t hey com e out in a part icular order when I ret rieve t hem ? The answer t o t his quest ion is t hat it s t he wrong quest ion. St oring rows is t he servers j ob and you should let t he server do it . Besides, even if you could specify st orage order, how would t hat help you if you want ed t o see result s sort ed in different orders at different t im es? When you select records, t heyre pulled out of t he dat abase and ret urned in what ever order t he server happens t o use. This m ay change, even for queries t hat dont sort rows, depending on which index t he server happens t o use when it execut es a query, because t he index can affect t he ret rieval order. Even if your rows appear t o com e out in t he proper order nat urally, a relat ional dat abase m akes no guarant ee about t he order in which it ret urns rows—unless you t ell it how. To arrange t he rows from a query result int o a specific order, sort t hem by adding an ORDER BY clause t o your SELECT st at em ent . Wit hout ORDER BY , you m ay find t hat t he ret rieval order changes when you m odify t he cont ent s of your t able. Wit h an ORDER BY clause, MySQL will always sort rows t he way you indicat e. ORDER BY has t he following general charact erist ics: • You can sort using a single colum n of values or m ult iple colum ns • You can sort any colum n in eit her ascending order t he default or descending order • You can refer t o sort colum ns by nam e, by t heir posit ion wit hin t he out put colum n list , or by using an alias This sect ion shows som e basic sort ing t echniques, and t he following sect ions illust rat e how t o perform m ore com plex sort s. Paradoxically, you can even use ORDER BY t o disorder a result set , which is useful for random izing t he rows, or in conj unct ion wit h LIMIT for picking a row at random from a result set . Those uses for ORDER BY are described in Chapt er 13 .

6.2.4 Naming the Sort Columns and Specifying Sorting Direction