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

8 comments:

Unknown 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.

Unknown 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/

Unknown 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...

ed hardy uk
versace shoes
bills jerseys
christian louboutin shoes
cheap oakley sunglasses
birkenstocks
oakland raiders jerseys
louis vuitton pas cher
mlb jerseys
colts jerseys

Unknown said...

michael kors outlet clearance
adidas trainers uk
ray ban eyeglasses
ray ban uk
tory burch sale
ralph lauren polo outlet
hermes outlet
michael kors bags
ferragamo outlet
manolo blahnik outlet
2017.6.27chenlixiang

fausse rolex said...

Replique Montre, Replique Rolex Pas Cher - Replique Montre De Luxe.
Replique Montre, Rolex Pas Cher - Replique Panerai, Omega, Breitling, Hublot.