Return shortest string if matches in first two or more words - PHP MYSQL
I have for example, the following data in my database table...
30 Miles DVD
300
310 To Yuma BluRay
310 To Yuma DVD 2007
310 To Yuma DVD Wide Screen
310 To YumaYoung Guns DVD
4 Collection Fights King Of The Cage DVD
48 Hours DVD Wide Screen
49th Parallel DVD
55 Days at Peking DVD
5th Commandment DVD
6th Day BluRay
6th Day DVD 2000
6th Day The Last Action Hero Box Set DVD
7 Men From Now
7 Seconds Sell Through DVD
7 Seconds UMD Mini for PSP 2005
7 Star Grand Mantis DVD
8 Strikes Of The Wildcat DVD
800 Bullets DVD
83 Hours Til Dawn DVD
9 And A Half Ninjas DVD
What I want to display is the UNIQUE titles, so, most of the above are OK but where we have:
310 To Yuma BluRay
310 To Yuma DVD 2007
310 To Yuma DVD Wide Screen
I only want to show, if possible:
310 To Yuma BluRay
again from the above list where there is
6th Day BluRay
6th Day DVD 2000
6th Day The Last Action Hero Box Set DVD
I only want to show:
6th Day BluRay
Or better still "310 To Yuma" && "6th Day" respectively.
I'm thinking I need to maybe do a check on a 开发者_Go百科number of letters in first word before a space then if that's over say 5 letters match with the next rows, if it does then keep adding the rows to a list somehow then when it stops matching spit out the shortest one? If the first word is less than 5 letters then move onto second word (just trying to avoid, the :: them :: this :: their etc etc)
Another way of saying it: If the first 5 characters (obv. to include spaces) match, once they stop matching get rid of what's left and print the result. Now, I also have to be careful once again with this as "The Hangover" && "The Hangover 2" are refectly plausible!
One answer suggested on an earlier version of this question was:
SELECT `title` FROM `PRprod_FILMS` m
WHERE `genre` = 'Action Adventure'
AND NOT EXISTS (
SELECT 1 FROM `PRprod_FILMS` m2
WHERE m.`title` LIKE CONCAT(m2.`title`, '%')
AND LENGTH(m2.`title`) < LENGTH(m.`title`)
AND m2.`genre` = m.`genre`
)
;
Which kind of works but strips out some unique titles anyway so not the right answer I wonder if this is even possible or am I asking too much or need to rethink the strategy/logic?
Darren
There is no 100% good solution for this that will never show duplicates or will not remove good records. However my solution would be to search for some common words (DVD, Bluray, PSP, etc) strip everything after that and make that unique. If you do this a lot, store is in another field:
SELECT DISTINCT( TRIM(
LEFT(title,
IF( LOCATE('DVD', title),
LOCATE('DVD', title) - 1,
IF( LOCATE('Bluray', title),
LOCATE('Bluray', title) - 1,
999
)
)
)
)) FROM `PRprod_FILMS`
精彩评论