Jump to content

Php/sql 2 & 3 Names, Single Name Attribute


Accolade

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.