I have a problem I would like to know if I can tackle in my mysql query.
我有一个问题,我想知道我是否可以解决我的mysql查询。
I have a list of "names" in my DB: id=1, name="name1"
id=2, name="name11"
id=3, name="name111"
我的数据库中有一个“名称”列表:id = 1,name =“name1”id = 2,name =“name11”id = 3,name =“name111”
I would like to get the id of the name in db with the most common characters at the beginning.
我想在db中获取名称的id,并在开头使用最常见的字符。
=> name111someignorechar would return 3
=> name111someignorechar将返回3
=> name11someignorechar would return 2
=> name11someignorechar将返回2
=> name1someignorechar would return 1
=> name1someignorechar将返回1
Any idea?
If I use the LIKE keyword, SELECT id FROM names WHERE CONCAT(name,"%") LIKE "name111someignorechar" It will return me 3 results !
如果我使用LIKE关键字,SELECT id FROM名称WHERE CONCAT(名称,“%”)LIKE“name111someignorechar”它将返回3个结果!
2 个解决方案
#1
1
Try this:
select name
from names
where 'name111someignorechar' like concat(name,'%')
order by length(name) desc
limit 1
#2
1
The current answer does work for the example that the OP has given, but it doesn't really answer the question. The OP asks for a query that orders by the most common characters, but the query only works if the DB entry contains a subset of the characters in the search query, and nothing more.
目前的答案确实适用于OP提供的示例,但它并没有真正回答这个问题。 OP请求按最常见字符排序的查询,但只有当DB条目包含搜索查询中的字符子集时,查询才有效,仅此而已。
For example, comparing name111someignorechar with name1otherignorechar wouldn't work.
例如,将name111someignorechar与name1otherignorechar进行比较将不起作用。
There's another solution, as explained here: http://tech-q-a.tumblr.com/post/58713827644/how-to-sort-results-based-on-the-number-of-identical
还有另一种解决方案,如下所述:http://tech-q-a.tumblr.com/post/58713827644/how-to-sort-results-based-on-the-number-of-identical
We can use a MySQL function that counts the number identical starting-characters for two strings:
我们可以使用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 ;
So,
SELECT countMatchingBeginChars("name111someignorechar", "name11")
would return 6 (as above), but so would
将返回6(如上所述),但也会如此
SELECT countMatchingBeginChars("name111someignorechar", "name11otherignore")
Now, we can sort the entries in the database according to the number of matching characters:
现在,我们可以根据匹配字符的数量对数据库中的条目进行排序:
SELECT name FROM names ORDER BY countMatchingBeginChars("name111someignorechar",name) DESC, name
If only the entry with the most matching characters is required, we can just return only one result.
如果只需要具有最匹配字符的条目,我们只能返回一个结果。
SELECT name FROM names ORDER BY countMatchingBeginChars("name111someignorechar",name) DESC, name LIMIT 1
#1
1
Try this:
select name
from names
where 'name111someignorechar' like concat(name,'%')
order by length(name) desc
limit 1
#2
1
The current answer does work for the example that the OP has given, but it doesn't really answer the question. The OP asks for a query that orders by the most common characters, but the query only works if the DB entry contains a subset of the characters in the search query, and nothing more.
目前的答案确实适用于OP提供的示例,但它并没有真正回答这个问题。 OP请求按最常见字符排序的查询,但只有当DB条目包含搜索查询中的字符子集时,查询才有效,仅此而已。
For example, comparing name111someignorechar with name1otherignorechar wouldn't work.
例如,将name111someignorechar与name1otherignorechar进行比较将不起作用。
There's another solution, as explained here: http://tech-q-a.tumblr.com/post/58713827644/how-to-sort-results-based-on-the-number-of-identical
还有另一种解决方案,如下所述:http://tech-q-a.tumblr.com/post/58713827644/how-to-sort-results-based-on-the-number-of-identical
We can use a MySQL function that counts the number identical starting-characters for two strings:
我们可以使用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 ;
So,
SELECT countMatchingBeginChars("name111someignorechar", "name11")
would return 6 (as above), but so would
将返回6(如上所述),但也会如此
SELECT countMatchingBeginChars("name111someignorechar", "name11otherignore")
Now, we can sort the entries in the database according to the number of matching characters:
现在,我们可以根据匹配字符的数量对数据库中的条目进行排序:
SELECT name FROM names ORDER BY countMatchingBeginChars("name111someignorechar",name) DESC, name
If only the entry with the most matching characters is required, we can just return only one result.
如果只需要具有最匹配字符的条目,我们只能返回一个结果。
SELECT name FROM names ORDER BY countMatchingBeginChars("name111someignorechar",name) DESC, name LIMIT 1