Jump to content

Order MYSQL Results by Size Order in Php??


Wazuke

Recommended Posts

Hi Guys,

Looking to pull out available sizes for a product from the MySQL database, and then order them in Size order.

 

 

EG: Ordering the result of: XL,2XL,M,L,3XL,XS,S

to: 3XL,2XL,XL,L,M,S,XS

 

Any help would be appreciated.

 

Thanks,

 

Link to comment
Share on other sites

ORDER BY FIELD(size,'3XL','2XL','XL','L','M','S','XS')

 

Care to elaborate (field() is new to me)? field() returns the index position of size in the '3XL','2XL','XL','L','M','S','XS' list which is thus 0-7 then how does for example ORDER BY 3 help as it would just order by the 3rd column?

Link to comment
Share on other sites

ORDER BY FIELD(size,'3XL','2XL','XL','L','M','S','XS')

 

Care to elaborate (field() is new to me)? field() returns the index position of size in the '3XL','2XL','XL','L','M','S','XS' list which is thus 0-7 then how does for example ORDER BY 3 help as it would just order by the 3rd column?

 

Magick ;)

 

size, FIELD(size,'3XL','2XL','XL','L','M','S','XS')

--------------------------------------------

3XL, 1

2XL, 2

XL , 3

L  , 4

M  , 5

S  , 6

XS , 7

Link to comment
Share on other sites

ORDER BY FIELD(size,'3XL','2XL','XL','L','M','S','XS')

 

Care to elaborate (field() is new to me)? field() returns the index position of size in the '3XL','2XL','XL','L','M','S','XS' list which is thus 0-7 then how does for example ORDER BY 3 help as it would just order by the 3rd column?

 

Magick ;)

 

size, FIELD(size,'3XL','2XL','XL','L','M','S','XS')

--------------------------------------------

3XL, 1

2XL, 2

XL , 3

L  , 4

M  , 5

S  , 6

XS , 7

 

Thx Mchl Magick indeed ;) I now understand how it works and I'll remember to use that whenever I have a fixed list. Got any resource that contains more of such Magick? Book? Tut?

Link to comment
Share on other sites

internet

 

I can never remember how this is used, so when I need I do this:

http://lmgtfy.com/?q=mysql+custom+sort

 

That's not what I meant. I surely hoped there would be some specific document/book that discussed these advanced MySQL features/uses and I actually wanted to know the title of this document/book.

 

Although your lmgtfy turned up some good results :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.