简体   繁体   English

如果前两个或更多单词匹配,则返回最短的字符串-PHP MYSQL

[英]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: 我要显示的是UNIQUE标题,因此,上面的大多数内容都可以,但是有:

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. 或者更好的分别是“ 310 To Yuma”和&“第六日”。

I'm thinking I need to maybe do a check on a 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? 我想我可能需要检查空格前第一个单词中的字母数量,如果超过5个字母则与下一行匹配,如果确实如此,则继续以某种方式将这些行添加到列表中,然后停止匹配吐出最短的一个? If the first word is less than 5 letters then move onto second word (just trying to avoid, the :: them :: this :: their etc etc) 如果第一个单词少于5个字母,则移至第二个单词(只是要避免使用::他们::这个::他们的等等)

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. 另一种说法是:如果前5个字符(包括空格)匹配,则一旦停止匹配,就删除剩下的并打印结果。 Now, I also have to be careful once again with this as "The Hangover" && "The Hangover 2" are refectly plausible! 现在,我还必须再次注意这一点,因为“宿醉” &&“宿醉2”似乎很合理!

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. 没有100%好的解决方案,它永远不会显示重复或不会删除好的记录。 However my solution would be to search for some common words (DVD, Bluray, PSP, etc) strip everything after that and make that unique. 但是,我的解决方案是搜索一些常用词(DVD,Bluray,PSP等),然后剥离所有内容并使其唯一。 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`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM