Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql:查找起始字符最匹配的字符串_Mysql_String - Fatal编程技术网

Mysql:查找起始字符最匹配的字符串

Mysql:查找起始字符最匹配的字符串,mysql,string,Mysql,String,我有一个问题,我想知道我是否可以在mysql查询中解决 我的数据库中有一个“姓名”列表: id=1,name=“name1” id=2,name=“name11” id=3,name=“name111” 我想在db中获取名称的id,并在开头使用最常见的字符 =>name111someignorechar将返回3 =>name1SomeIgnoreChar将返回2 =>name1someignorechar将返回1 有什么想法吗 如果我使用LIKE关键字, 从名称中选择id,其中CONCAT(名称

我有一个问题,我想知道我是否可以在mysql查询中解决

我的数据库中有一个“姓名”列表:
id=1,name=“name1”
id=2,name=“name11”
id=3,name=“name111”

我想在db中获取名称的id,并在开头使用最常见的字符

=>
name111someignorechar将返回3

=>
name1SomeIgnoreChar将返回2

=>
name1someignorechar将返回1

有什么想法吗

如果我使用LIKE关键字, 从名称中选择id,其中CONCAT(名称,“%”)类似于“name111someignorechar” 它将返回我3个结果

试试这个:

select name
from names
where 'name111someignorechar' like concat(name,'%')
order by length(name) desc
limit 1

当前的答案确实适用于OP给出的示例,但它并没有真正回答问题。OP请求一个按最常见字符排序的查询,但该查询仅在DB条目包含搜索查询中字符的子集时有效,仅此而已

例如,将name111someignorechar与name1otherignorechar进行比较是行不通的

还有另一种解决方案,如下所述:

我们可以使用MySQL函数计算两个字符串的相同起始字符数:

DROP FUNCTION IF EXISTS countMatchingBeginChars;
delimiter // 
CREATE FUNCTION `countMatchingBeginChars`( s1 text, s2 text ) RETURNS int(11)
    DETERMINISTIC
BEGIN 
    DECLARE s1_len, s2_len, min_len, j INT; 
    SET s1_len = LENGTH(s1), s2_len = LENGTH(s2);

    IF s1_len > s2_len THEN  
      SET min_len = s2_len;  
    ELSE  
      SET min_len = s1_len;  
    END IF; 
    SET j = 0;
     WHILE j <= min_len DO 
        SET j = j + 1;
        IF SUBSTRING(s1,j,1) != SUBSTRING(s2,j,1) THEN
            RETURN j-1;
        END IF;
     END WHILE;
     RETURN min_len;
END//
delimiter ;
将返回6(如上所述),但也将返回6

SELECT countMatchingBeginChars("name111someignorechar", "name11otherignore")
现在,我们可以根据匹配字符的数量对数据库中的条目进行排序:

SELECT name FROM names ORDER BY countMatchingBeginChars("name111someignorechar",name) DESC, name
如果只需要匹配字符最多的条目,我们只能返回一个结果

SELECT name FROM names ORDER BY countMatchingBeginChars("name111someignorechar",name) DESC, name LIMIT 1

您是希望返回3、2和1,还是试图返回name=”“的内容,即或其他值?
SELECT name FROM names ORDER BY countMatchingBeginChars("name111someignorechar",name) DESC, name LIMIT 1