Accolade Posted November 3, 2012 Share Posted November 3, 2012 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 More sharing options...
Pikachu2000 Posted November 4, 2012 Share Posted November 4, 2012 Normalize the data. Have a field for first, last, middle names. Link to comment https://forums.phpfreaks.com/topic/270251-phpsql-2-3-names-single-name-attribute/#findComment-1390052 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.