custom ORDER BY

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
megaming
Forum Commoner
Posts: 27
Joined: Wed Dec 25, 2002 12:31 pm

custom ORDER BY

Post by megaming »

I want to ORDER BY a field named TYPE in this order: helm, amulet, shield, weapon, bodyarmour, boots.

Any idea how I would go about this?
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Two suggestions:

1) Create a table that relates those items to numbers.

So you might have a table called Items:
helm | 1
amulet | 2
shield | 3

Etc. Then, instead, order by the ItemID not the ItemName.

2) This is more of a hack, but you could in the database call the helm, 1helm and the amulet, 2amulet. Then, just either remember to cut off the first letter before outputting it, or something. Then you can order by this type alphabetically.
megaming
Forum Commoner
Posts: 27
Joined: Wed Dec 25, 2002 12:31 pm

Post by megaming »

Thanks for your time... I just found out how I wanted to do it :D

Code: Select all

ORDER by type='helm' DESC, type='amulet' DESC, type='cape' DESC, type='shield' DESC, type='weapon' DESC, type='bodyarmour' DESC
Post Reply