Jump to content

Recommended Posts

Have a single attribute for names, e.g. John Doe Smith, Johnathan Smithson. Need to be able to order by the surname if the name contains only 2 words, e.g. Johnathan Smith (Order by Smith) & if the name field has 3 words, order by the first name e.g. John Doe Smith (Order by John).

 

I have it working for 2 words (below), bit confused upon how I would go about ordering the by first name when the field contains 3 words though. I have a feeling it will be something like adding a substring for the middle name and then writing IF statements for if MiddleName not null, then order by FirstName?

 

Here is my code:

 

 

$sql = "SELECT

SUBSTR( name, 1, LOCATE( ' ', name ) -1 ) AS FirstName,

SUBSTR( name, LOCATE( ' ', name ) +1 ) AS LastName,

author_id, description

FROM author

GROUP by description

ORDER by LastName $order_asc_desc";

Link to comment
https://forums.phpfreaks.com/topic/270251-phpsql-2-3-names-single-name-attribute/
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.