Jump to content

[SOLVED] Help with ORDER BY alternatives


David Joor

Recommended Posts

I sell ball bearings and the sizes are relatively close to each other.  The issue I am having is that I would like them sorted smallest to largest however just a standard ORDER BY places "10x15x4 Metal" before "2x6x3 Metal" because of the 1.  My previous solution was "ORDER BY 0 + products.title ASC".  Works decently but items that start with the same first digit just get randomly sorted.  Here are my examples of what is happening.

 

ex1: ORDER BY products.title ASC

 

10x15x4 Metal

5x10x4 Ceramic Metal

5x10x4 Flanged Rubber

5x10x4 Metal

5x10x4 Revolutions

5x11x4 Rubber

5x12x4 Revolution

5x13x4 Revolution

5x8x2.5 Ceramic Revolu...

5x8x2.5 Flanged Rubber

5x8x2.5 Rubber

5x9x3 Metal

5x9x3 Revolutions

 

 

ex2: ORDER BY 0 + products.title ASC

 

5x13x4 Revolution

5x9x3 Revolutions

5x8x2.5 Ceramic Revolu...

5x11x4 Rubber

5x9x3 Metal

5x10x4 Flanged Rubber

5x10x4 Revolutions

5x8x2.5 Flanged Rubber

5x10x4 Metal

5x8x2.5 Rubber

5x10x4 Ceramic Metal

5x12x4 Revolution

10x15x4 Metal

 

 

ex3: What I'm trying to accomplish

 

5x8x2.5 Ceramic Revolu...

5x8x2.5 Flanged Rubber

5x8x2.5 Rubber

5x9x3 Metal

5x9x3 Revolutions

5x10x4 Ceramic Metal

5x10x4 Flanged Rubber

5x10x4 Metal

5x10x4 Revolutions

5x11x4 Rubber

5x12x4 Revolution

5x13x4 Revolution

10x15x4 Metal

 

I wouldn't even mind if they were slightly out of sequence like ex1 as long as they are grouped together and 10x15x4 was below a 5x9x3.  Any ideas, this is so driving crazy!

 

Link to comment
Share on other sites

You can break the string into the three size fields, using msyql functions in the query, cast the values as numbers, then ORDER BY field1,field2,field3 (time permitting someone can probably post a query to do this) or you could add a column that you could put values into and use in the ORDER BY (probably not a good choice if you have a lot of existing items.)

Link to comment
Share on other sites

Partially tested only, should work -

 

SELECT *, SUBSTRING_INDEX(SUBSTRING_INDEX(products.title, ' ', 1), 'x', 1) + 0 as f1, 
SUBSTRING_INDEX(SUBSTRING_INDEX(products.title, 'x', 2), 'x', -1) + 0 as f2,
SUBSTRING_INDEX(SUBSTRING_INDEX(products.title, ' ', 1), 'x', -1) + 0 as f3
FROM your_table ORDER BY f1,f2,f3

Link to comment
Share on other sites

Each of three lines of the query using SUBSTRING_INDEX() (the first one can actually be simplified) gets a separate 'n' field out of the 'nxnxn text' string.

 

The 1st one gets everything up to the first x.

The 2nd one gets everything up to the second x, then gets just the number back to the previous x.

The 3rd one gets everything up to the first space, then gets just the number back to the previous x.

 

 

Link to comment
Share on other sites

SUBSTRING_INDEX() produces a string. When you sort stings that contain numeric digits, you get things like 10 is less than 9 because the first character in the string 10 is less than the first character in the string 9.

 

The + 0 forces the string to be cast as a number.

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.