Tuesday, April 5, 2011

Tip: MySql ORDER BY FIELD()

Say you have a rowset of:

Categories table:
id     |     Category
1      |    Books
2      |    Electronics
3      |    Other
4      |    Autos
5      |    Furniture


And you want to order in a way that you would put "Other" category as the last element and also order the other elements in an ascending way.

Simply use ORDER BY FIELD():
SELECT *  FROM `categories` ORDER BY field(Category, 'Other') ASC, Category ASC

Notice the placement of FIELD() before "name ASC". You need to do this so that it will also reorder the remaining rows in ascending.

Result
id     |     Category
5      |    Autos
1      |    Books
2      |    Electronics
5      |    Furniture
3      |    Other

17 comments:

Coach Factory said...

moncler outlet, cheap oakley sunglasses, canada goose outlet, oakley outlet, ugg boots, ray ban outlet, oakley sunglasses cheap, ray ban, ray ban outlet, ray-ban, oakley sunglasses outlet, oakley outlet, cheap oakley sunglasses, oakley vault, ray ban sunglasses outlet, canada goose, oakley vault, louboutin, tn pas cher, cheap oakley sunglasses, ray ban, canada goose, ray ban, ugg, oakley sunglasses outlet, ray bans, raybans, canada goose, oakley vault, ray-ban, oakley outlet, cheap sunglasses, ralph lauren, rayban, cheap oakley, moncler, oakley sunglasses cheap, gucci, oakley sunglasses outlet, moncler, oakley, canada goose, cheap sunglasses, ray bans, cheap sunglasses, oakley sunglasses cheap, moncler, canada goose outlet, rayban, discount oakley sunglasses

Coach Factory said...

jordan shoes, louboutin, michael kors outlet, michael kors, michael kors, louis vuitton handbags, nike air max, louis vuitton, gucci outlet, true religion outlet, michael kors outlet, air max, coach outlet store, ray ban sunglasses, burberry outlet online, michael kors, longchamp outlet, nike free, true religion jeans, burberry outlet online, coach factory outlet, louboutin, toms shoes, air jordan, polo ralph lauren, chanel handbags, ray ban, prada outlet, tiffany and co, nike air max, polo ralph lauren outlet, louboutin, coach outlet, nike shoes, ralph lauren, kate spade outlet, tory burch outlet, air max, oakley sunglasses, oakley sunglasses, true religion, michael kors outlet, oakley sunglasses, longchamp, prada handbags, nike free, coach purses, coach factory,

Coach Factory said...

celine handbags, chi flat iron, nike air max, nfl jerseys, ugg boots, lululemon, mcm handbags, marc jacobs, supra shoes, soccer shoes, lancel, beats by dre, vans, p90x3, ghd, herve leger, nike free, ferragamo shoes, burberry, birkin bag, juicy couture outlet, montre pas cher, soccer jerseys, ralph lauren, hermes, north face outlet, ugg boots, valentino shoes, louis vuitton, jimmy choo, new balance shoes, mulberry, rolex watches, louis vuitton, abercrombie and fitch, oakley pas cher, reebok outlet, hollister, air max, mont blanc, bottega veneta, converse shoes, ray ban pas cher, karen millen, wedding dresses, hogan, yoga pants, insanity, asics gel, timberland

co wendy said...

Sergei , October 22, 1970 Born in Christian Louboutin Bois Dore Moscow State Ozherelye. Perhaps influenced by Cheap LV Handbags their parents worship warrior army, join the army in cheap jordans 1989, and into the elite airborne military service. 1991, by the influence of the Soviet Union, the uggs on sale Russian christian louboutin army general lack of funds, a large number ugg australia of Air Jordan 11 Gamma Blue soldiers seeking veterans, but under extremely difficult conditions still dedicated, praise superiors, and soon was sent to the famous christian louboutin shoes Ryazan Higher ugg Airborne Command School studies. After graduating in 1994, entered the famous Pskov 76th Guards Airborne discount christian louboutin Division, served as discount nike jordans the reconnaissance platoon, reconnaissance ugg soldes deputy company commander, company commander, battalion airborne regiment scouting director and other duties.From 2000 to Cheap Louis Vuitton Handbags 2004, where the forces have repeatedly ordered war with Chechen militants. Because of their opponents in order to form the squad activity and haunted impermanence, the Russians had to ugg boots mobilize elite troops set up large uggs outlet number of small units, "a Discount Louis Vuitton small wholesale jordan shoes play small" Implementation siege. commanding troops in Chechnya, Christian Louboutin Daffodile Ingushetia and other places over the mountains, to track the militants fled. Discount LV Handbags Although the militants were extremely vigilant, but as long as the task has never had outsmarted.August 8, 2008, Georgia suddenly invade South Ossetia (Russian peacekeepers stationed ugg pas cher there), the Russian military to respond quickly, including including the 76th Guards Airborne Division, more than christian louboutin remise 50% 3,000 people were immediately delivered to the pro-Russian Abkhazia, Georgia cheap nike jordan shoes Army contain two infantry brigades, effectively coordinate the cheap christian louboutin direction of the Russian Bags Louis Vuitton troops in South Ossetia.

Zheng junxai5 said...

2015-09-16 zhengjx
coach factory outlet
Louis Vuitton Outlet Handbags Pursers In USA
Michael Kors Outlet Online Store Clearance
Coach Factory Outlet Online Sale Discount
Original Michael Kors Handbags Outlet Online
Coach Outlet Store Online Clearance
Cheap Michael Kors Handbags On Sale
Official Cheap Michael Kors Handbags Outlet
ugg boots
louis vuitton
cheap nfl jerseys
Louis Vuitton Outlet Store Locations
abercrombie & fitch
Coach Factory Outlet Official Website
louis vuitton outlet
michael kors
Michael Kors Outlet Stores Online
canada goose outlet
Kobe Shoes,Kobe 9,Kobe 8 Discount
Coach Factory Outlet Official Coach Handbags Sale
ray-ban sunglasses
Louis Vuitton Handbags Outlet Online
michael kors bag
Coach Outlet Online Coach Factory Outlet
Louis Vuitton Handbags UK For Sale
Louis Vuitton Handbags Official USA Website
timberland boots
ugg boots
Oakley Sunglasses Official Website Cheap Off
coach factory outlet online

Wenhao Guo said...

cheng0928
louis vuitton handbags, http://www.louisvuittonhandbag.us/
hermes birkin, http://www.hermesbirkin.org/
mbt shoes, http://www.mbtshoesoutlet.us.com/
swarovski outlet, http://www.swarovski-outlet.co.uk/
hogan, http://www.scarpehogan-outlet.it/
ray ban, http://www.occhiali-rayban.it/
nfl jerseys wholesale, http://www.nfljerseys-wholesale.us.com/
nba jerseys, http://www.nbajerseys.us.com/
longchamp handbags, http://www.longchamphandbag.us.com/
cheap nhl jerseys, http://www.nhljerseys.us.com/
cheap mlb jerseys, http://www.cheapmlbjerseys.net/
north face jackets, http://www.thenorthfaces.in.net/
cheap snapbacks, http://www.cheapsnapbacks.us.com/
adidas wings, http://www.adidaswings.net/
basketball shoes, http://www.basketballshoes.us.com/
lebron james shoes, http://www.lebronjamesshoes.in.net/
lacoste polo shirts, http://www.lacostepoloshirts.cc/
vans shoes, http://www.vans-shoes.cc/
ugg boots, http://www.uggboot.com.co/
hermes belt, http://www.hermesbelts.us/

mao qiuyun said...

ray ban sunglasses, http://www.raybansunglassesonline.us.com/
nike trainers, http://www.niketrainers.me.uk/
gucci handbags, http://www.guccihandbags-outlet.co.uk/
herve leger, http://www.herveleger.us.com/
timberland boots, http://www.timberlandboots.name/
coach outlet online, http://www.coachoutletonline-store.us.com/
michael kors outlet, http://www.michaelkorsoutletcanada.in.net/
juicy couture outlet, http://www.juicycoutureoutlet.net/
calvin klein underwear, http://www.calvinklein.in.net/
ray ban sunglasses, http://www.raybansunglass.us.com/
prada outlet, http://www.pradaoutlet.us/
lululemon outlet, http://www.lululemonoutletstore.in.net/
tory burch shoes, http://www.toryburchshoesoutlet.com/
michael kors outlet, http://www.michaelkorsoutlet.org.uk/
replica watches, http://www.replicawatchesforsale.us.com/
pandora jewelry, http://www.pandorajewelryoutlet.us.com/
nfl jerseys wholesale, http://www.nfljerseys-wholesale.us.com/
michael kors outlet, http://www.michaelkorsoutletonlinstore.us.com/
oakley outlet, http://www.oakleyoutlet.in.net/
oakley sunglasses, http://www.oakleysunglasses-outlet.us.com/
michael kors bags, http://www.michaelkorsbags.uk/
ray ban sunglasses, http://www.ray-bansunglasses.eu.com/
oakley sunglasses, http://www.oakley.in.net/
air max 2015, http://www.airmax2015.in.net/
the north face uk, http://www.thenorthfaces.org.uk/
swarovski outlet, http://www.swarovski-outlet.co.uk/
pandora jewelry, http://www.pandora.eu.com/
chanel handbags, http://www.chanelhandbags-outlet.co.uk/
1002maoqiuyun

chenlina said...

chenlina20151127
chanel handbags
air force 1
christian louboutin shoes
pandora outlet
the north face outlet
cheap air max
oakley sunglasses
jordan retro
nike free runs
uggs outlet
oakley outlet
jordans for sale
ugg boots outlet
christian louboutin outlet
the north face outlet
louis vuitton purses
louis vuitton outlet
jordan concords
louis vuitton outlet
louis vuitton outlet
ugg boots
uggs on sale
ugg boots
louis vuitton
louboutin pas cher
christian louboutin
coach factory outlet
gucci outlet
ugg slippers
running shoes
swarovski outlet
coach outlet store online
official coach factory outlet
uggs outlet
air jordans
canada goose outlet
cheap air jordans
mont blanc pens
mulberry handbags
louis vuitton bags
as

chenlina said...

chenlina20160127jordan concords
michael kors outlet stores
ugg outlet
coach outlet
polo ralph lauren
north face outlet
coach factory outlet
michael kors outlet online
christian louboutin outlet
jordans for sale
uggs outlet
hollister outlet
michael kors uk
tory burch outlet
louis vuitton outlet
uggs on sale
hollister outlet
kate spade handbags
michael kors outlet online
michael kors handbags
coach outlet store online
michael kors outlet
toms wedges
prada handbags
nike huarache shoes
hollister clothing
christian louboutin outlet
coach outlet
true religion outlet
tods outlet online
polo ralph lauren
fitflops
micheael kors outlet
oakley outlet
the north face
ugg boots
ray ban wayfarer
michael kors handbags
polo ralph lauren
coach factory outlet
as

Unknown said...

air max 90
louis vuitton handbags
air jordan 11
nike store uk
michael kors wallet
north face jacket
converse shoes
ralph lauren
reebok trainers
louis vuitton
toms shoes
canada goose jackets
mcm outlet
lululemon outlet
michael kors outlet
ralph lauren polo
swarovski crystal
burberry outlet
michael kors handbags
polo ralph lauren
louis vuitton
hollister canada
ugg boots
polo ralph lauren
tiffany and co
ralph lauren uk
rolex watches
celine outlet
rolex watches for sale
coach outlet clearance
polo ralph lauren
coach outlet store
true religion sale
longchamp handbags
michael kors outlet
chanyuan0913

roshini said...


Sage Support Phone Number
Sage 50 Tech Support Phone Number
Sage 50 Support Number
Sage 50 Technical Support Phone Number
Sage 50 Tech Support Number
Sage 50 Technical Support Phone Number
Sage 50 Technical Support Number
Sage 50 Customer Support Phone Number
Sage 50 Customer Service Number

Soni yadav said...

Aol Technical Support Phone Number
Aol Technical Support Number
Aol Tech Support Number
Aol Tech Support phone Number
Aol Support Phone Number
Aol Tech Support Number
Outlook Technical Support Phone Number
Outlook Technical Support Number
Outlook Tech Support Number
Outlook Tech Support Phone Number
Outlook Support Phone Number
Outlook Tech Support Number

Anjilo Jilo said...


Quicken Support Phone Number
Quicken Support Phone Number
Quicken Support Number
Quicken Tech Support Number
Quicken Tech Support Phone Number
Quicken Technical Support Number
Quicken Technical Support Phone Number
Quicken Technical Support Number
Quicken Tech Support Phone Number
Quicken Tech Support Number

mohan kumar said...

Quickbooks Support Phone Number
Quickbooks Support Phone Number
Quickbooks Tech Support Number
Quickbooks Support Number
Quickbooks Technical Support Number
Quickbooks Tech Support Phone Number
Quickbooks Technical Support Phone Number
Quickbooks Help Desk Phone Number
Quickbooks Help Desk Number
Quickbooks Customer Service Number
Quickbooks Customer Service Phone Number
Quickbooks Customer Support Number
Quickbooks Customer Support Phone Number

Kanika Jaiswal said...

QuickBooks Technician
Quickbooks Support Phone Number
Quickbooks Tech Support Number
Quickbooks Support Number
Quickbooks Technical Support Number
Quickbooks Tech Support Phone Number
Quickbooks Technical Support Phone Number
Quickbooks Customer Service Number
Quickbooks Customer Service Phone Number
Quickbooks Customer Support Number
Quickbooks Customer Support Phone Number
Quickbooks Help Desk Phone Number
Quickbooks Help Desk Number

Mohini Roy said...

Quicken Support Number
Quicken Support Phone Number
Quicken Tech Support Number
Quicken Support Number
Quicken Technical Support Number
Quicken Tech Support Phone Number
Quicken Customer Service Number
Quicken Customer Service Phone Number
Quicken Customer Support Number
Quicken Customer Support Phone Number
Quicken Help Desk Phone Number
Quicken Help Desk Number

Mohini Roy said...

Quicken Tech Support Number
Quicken Support Phone Number
Quicken Tech Support Number
Quicken Support Number
Quicken Technical Support Number
Quicken Tech Support Phone Number
Quicken Customer Service Number
Quicken Customer Service Phone Number
Quicken Customer Support Number
Quicken Customer Support Phone Number
Quicken Help Desk Phone Number
Quicken Help Desk Number