Jump to content

Sort products by second word


skyace888

Recommended Posts

I am finishing up work on an OSCommerce setup for someone and they had a request. Most of the products are named by a person's name such as "John Doe" or "Joseph Dean". By default, the products are sorted by the first name in each category. Is there a way I can sort the products by last name so "Joseph Dean" comes before "John Doe"? I don't want to have to change the names of the products to a "lastname, firstname" format. Also, all of the items are already in the database and we are talking about 5000+ products. Any suggestions are greatly appreciated.  I already posted this in the OSCommerce forum, but figured I'd ask here as well since it's more of a database question.

 

Thanks,

Al

Link to comment
Share on other sites

As long as you have a space or something separating the first part of the product name with the second, then you can use SUBSTRING_INDEX().

 

SELECT SUBSTRING_INDEX('John Doe', ' ', 1); # Returns John

SELECT SUBSTRING_INDEX('John Doe', ' ', -1); # Returns Doe

 

# So, here's an example query:

SELECT   SUBSTRING_INDEX(`column_name`, ' ', -1) AS last_name

           , SUBSTRING_INDEX(`column_name`, ' ', 1) AS first_name

  FROM

             `table_name`

ORDER BY

       last_name, first_name

;

 

 

See:

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

 

 

So, this hard coded example does return Joseph Dean first:

 

SELECT    SUBSTRING_INDEX('John Doe', ' ', -1) AS last_name

            , SUBSTRING_INDEX('John Doe', ' ', 1) AS first_name

UNION

SELECT    SUBSTRING_INDEX('Joseph Dean', ' ', -1) AS last_name

            , SUBSTRING_INDEX('Joseph Dean', ' ', 1) AS first_name

 

ORDER BY

      last_name, first_name

;

 

Link to comment
Share on other sites

Thanks for the reply, toplay.  This query doesn't appear to be working properly for me.  I forgot to mention that many of the rows have more than two strings in the name field such as "John Doe 1985" or "Chris Davis Special Edition".  I noticed that the "-1" appears to take the last string from the field.  When I run the query, all but a few of the last names are shown under last_name.  Is there a way to nest this function?  Again, I am trying to sort this by the second string in the field.  Thanks...

Link to comment
Share on other sites

I think I got it.  This seems to work for me.

 

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(products_name, ' ', 2), ' ', -1) AS 

last_name, SUBSTRING_INDEX(products_name, ' ', 1) AS first_name FROM 

products_description ORDER BY last_name, first_name;

 

Is there a better way to do this?  Also, if you happen to have worked with the OSCommerce package before, where on the index.php page (and any other pages) do I need to make my modifications?  Thanks again...

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.