bschultz Posted October 22, 2012 Share Posted October 22, 2012 (edited) I need to write a select statement where Jo. Doe Doe, Jo Doe John will ALL match John Doe... and where Ja. Doe Doe, Ja Doe Jane will ALL match Jane Doe. So, Initials and full last name are given...is this possible? I've only worked with LIKE...and that didnt' work. Is there another function I'm not aware of? Edited October 22, 2012 by bschultz Quote Link to comment https://forums.phpfreaks.com/topic/269784-mysql-like/ Share on other sites More sharing options...
ManiacDan Posted October 22, 2012 Share Posted October 22, 2012 Jo and Ja are not initials, they're some portion of the first name. You have to do heavy processing of this on the PHP end to turn it into something like: WHERE (firstname LIKE 'Jo%' OR lastname LIKE 'Jo%') AND (firstname LIKE 'Doe%' OR lastname LIKE 'Doe%'); Even then, you may get false positives since there's no way to tell which order the names are in. Quote Link to comment https://forums.phpfreaks.com/topic/269784-mysql-like/#findComment-1387009 Share on other sites More sharing options...
Christian F. Posted October 22, 2012 Share Posted October 22, 2012 You may be able to do something like this with metaphone () or soundex (), but it will require a redesign of the database and the above script. The PHP manual contains more details on how to use these functions, and what they do. Quote Link to comment https://forums.phpfreaks.com/topic/269784-mysql-like/#findComment-1387060 Share on other sites More sharing options...
bschultz Posted October 23, 2012 Author Share Posted October 23, 2012 Metaphone didn't work for the examples I listed...but Levenshtein-Distance did. There I go again...learning something new! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/269784-mysql-like/#findComment-1387099 Share on other sites More sharing options...
ManiacDan Posted October 23, 2012 Share Posted October 23, 2012 How does levenshtein work for you at all? Maybe "jo" to "john" would have a distance of 4, but it would have a distance of 1 to "Bo". Quote Link to comment https://forums.phpfreaks.com/topic/269784-mysql-like/#findComment-1387119 Share on other sites More sharing options...
bschultz Posted October 23, 2012 Author Share Posted October 23, 2012 This is for a football stats system. The user enters the teams roster (which goes into the database. Then they enter the player stats. All of this information is listed on the schools website. Problem is, so schools put stats online in first last order...some put last, first...some put last, first initial (2 initials if there's a John Smith and a Jim Smith (Jo. and Ji.). I just need to match the full name that's in the database. If there's a mulitple possible match, I suppose I could add a check for the user to choose which player it is. It's VERY rare that a team has more than one player with a same last name. I won't have time to work on this project til the weekend...I'll probably be back then with further questions. Quote Link to comment https://forums.phpfreaks.com/topic/269784-mysql-like/#findComment-1387123 Share on other sites More sharing options...
bschultz Posted October 25, 2012 Author Share Posted October 25, 2012 Alright, so levenshtein didn't work at all... Kroeplin, D. (as taken from the stats page of the team---which is Dustin Kroeplin) came back as Dustin Dowd using levenshtein. Back to square one I guess! Quote Link to comment https://forums.phpfreaks.com/topic/269784-mysql-like/#findComment-1387714 Share on other sites More sharing options...
ManiacDan Posted October 25, 2012 Share Posted October 25, 2012 Reply #2 contains the correct solution. Quote Link to comment https://forums.phpfreaks.com/topic/269784-mysql-like/#findComment-1387739 Share on other sites More sharing options...
bschultz Posted October 26, 2012 Author Share Posted October 26, 2012 The database is structured as name...not first name and last name. Since that's the way the rosters are formatted, that's the way the db was formatted. I did a little research on full text search and it looks promissing. I've only tested for the player I refferenced above, D. Kroeplin...and it worked. I'm guessing it's slow though. Is it? And is there any way to keep the db as is...and still use LIKE like you suggested? Quote Link to comment https://forums.phpfreaks.com/topic/269784-mysql-like/#findComment-1387818 Share on other sites More sharing options...
Christian F. Posted October 26, 2012 Share Posted October 26, 2012 You can use a RegExp to extract the first and last names into their own portions, but this will require conditionals in your rule. Another alternative, as ManicDan stated, would be to rely upon some heavy PHP string processing. In either case: If the three patterns you posted above are the only patterns you expect to receive, then you can drop the LIKE matching against the last name. However, if you get a name written the proper way (firstname lastname), then you will need to match everything against everything. Lastly: You do need to redesign your database anyway, since you've stored the full name as one field. You cannot reliably match against surnames/given names in such a setting, as the database engine does not understand the syntax of a name. Split it up into two, and I would recommend adding fields for the sound keys at the same time. Keeping the database as designed, especially when you get new requirements, is not a goal you should have. Code changes as the requirements does, and so does the database. Especially when you have new data introduced to the mix. You'll make your life a whole lot easier by accepting that altering the database is not only necessary in some cases, but also beneficial. Quote Link to comment https://forums.phpfreaks.com/topic/269784-mysql-like/#findComment-1387870 Share on other sites More sharing options...
ManiacDan Posted October 26, 2012 Share Posted October 26, 2012 FULLTEXT is fine, but you'll still have to pre-process the string. Or my solution with only `name` WHERE `name` LIKE 'Jo%' AND `name` LIKE 'Doe%'; Quote Link to comment https://forums.phpfreaks.com/topic/269784-mysql-like/#findComment-1387909 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.