匹配4个连续字符的任何子字符串

时间:2015-06-21 20:37:44

标签: sql-server sql-server-2014

我正在寻找一个连接表达式,用于匹配来自两个不同表的字符串,这两个表都包含4个连续字符的相同子字符串。

例如,以下内容应匹配:

String1   String2
--------  -----------
xxjohnyy  abcjohnabc     [common substring: "john"]
xxjohnyy  johnny         [common substring: "john"]
birdsings ravenbird      [common substring: "bird"]
singbird  a singer       [common substring: "sing"]

3 个答案:

答案 0 :(得分:1)

此问题与查找Longest Common Substring问题非常相似。您找到最长的公共子串,然后选择具有4的公共字符串的那些。您肯定会发现this linkthis link对您有帮助。

答案 1 :(得分:1)

这是一项非常好的练习。这是我尝试使用Tally Table。

SQL Fiddle

;WITH E1(N) AS(
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
),
E2(N) AS(SELECT 1 FROM E1 a CROSS JOIN E1 b),
E4(N) AS(SELECT 1 FROM E2 a CROSS JOIN E2 b),
E8(N) AS(SELECT 1 FROM E4 a CROSS JOIN E4 b),
Tally(N) AS(
    SELECT TOP (
        SELECT
            CASE 
                WHEN MAX(LEN(String1)) > MAX(LEN(String2)) THEN MAX(LEN(String1))
                ELSE MAX(LEN(String2))
            END
        FROM TestTable
    )
        ROW_NUMBER() OVER(ORDER BY (SELECT NULL))
    FROM E8
),
CteTable AS( -- Added an ID to uniquely identify each row
    SELECT *, Id = ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) FROM TestTable
),
CteSubStr1 AS(
    SELECT
        ct.*,
        substr = SUBSTRING(ct.String1, t.N, 4)
    FROM CteTable ct
    CROSS APPLY(
        SELECT N FROM Tally
        WHERE N <= LEN(ct.String1) - 3
    )t
),
CteSubStr2 AS(
    SELECT
        ct.*,
        substr = SUBSTRING(ct.String2, t.N, 4)
    FROM CteTable ct
    CROSS APPLY(
        SELECT N FROM Tally
        WHERE N <= LEN(ct.String2) - 3
    )t
),
CteCommon AS(
    SELECT * FROM CteSubStr1 c1
    WHERE EXISTS(
        SELECT 1 FROM CteSubStr2
        WHERE
            Id = c1.Id
            AND substr = c1.substr
    )
)
SELECT
    String1, String2, substr
FROM (
    SELECT  *, RN = ROW_NUMBER() OVER(PARTITION BY Id ORDER BY LEN(substr) DESC)
    FROM CteCommon
)t
WHERE RN = 1

<强>结果

|   String1 |    String2 | substr |
|-----------|------------|--------|
|  xxjohnyy | abcjohnabc |   john |
|  xxjohnyy |     johnny |   john |
| birdsings |  ravenbird |   bird |
|  singbird |   a singer |   sing |

这部分寻找最长的公共子串。

SELECT
    String1, String2, substr
FROM (
    SELECT  *, RN = ROW_NUMBER() OVER(PARTITION BY Id ORDER BY LEN(substr) DESC)
    FROM CteCommon
)t
WHERE RN = 1

要获取所有常见的子字符串,请改用:

SELECT * FROM CteCommon

答案 2 :(得分:1)

100

Fiddlee http://sqlfiddle.com/#!3/bd4dd/16

只需将 //username validation function checkusername(){ var username_value = $("#reg-username").val(); var userpass; if(username_value === ""){ //username empty $("#user-result").html('Please choose a username.'); $('#user-result').css('color', 'red'); $('#reg-username').css('border-color', 'red'); }else if (username_value.length < 4){ //username atless 4 digi $("#user-result").html('Your username is too short.'); $('#user-result').css('color', 'red'); $('#reg-username').css('border-color', 'red'); }else if (username_value.length >= 4){ $.ajax({ type:"POST", url:'class/classvalidation.php', async:false, data:{'username':username_value}, success: function(data){ var usernameAvailable = JSON.parse(data); var color = usernameAvailable.exists ? '#dfe0e6' : 'red'; $("#user-result").html(usernameAvailable.message); $('#user-result').css('color', color); $("#reg-username").css('border-color', color); if(usernameAvailable.exists === true){ userpass = true; }else{ userpass = false; } } }); }; //return function value if(userpass === true){ return true; }else{ return false; }; } //jquery started $(document).ready(function () { $("#reg-username").keyup(checkusername); $(document).click(function() { $(document).on("submit", '#contact-form', function(){ checkusername(); if(!checkusername()){ event.preventDefault(); $("#othersubmit-result").css('color', 'red'); $("#othersubmit-result").html('sumbit not requirements.'); // window.scrollTo(0, 0); return false; }else{ $("#othersubmit-result").css('color', 'green'); $("#othersubmit-result").html('sumbit passed.'); return true; } }); }); }); </script> 更改为列的实际长度...