我有一个SELECT语句。我想知道是否可以在SELECT语句中按字符长度匹配?所以,例如。如果一个单词的字符长度为3或更短,并且如果它与该单词匹配,则它将计为1匹配。如果一个单词的字符长度超过3,如果它与该单词匹配,则将计为2个匹配。这可能吗?
SELECT colName,
( colName LIKE '% 22 %' ) +
( colName LIKE '% 333 %' ) +
( colName LIKE '% 4444 %' ) as matches
FROM tableName
HAVING matches > 0
AND matches = (select max(
( colName LIKE '% 22 %' ) +
( colName LIKE '% 333 %' ) +
( colName LIKE '% 4444 %' )
) from tableName) limit 30
实施例
22 = 1 match
333 = 1 match
4444 = 2 matches
答案 0 :(得分:1)
使用IF
或CASE
SELECT colName,
(colName LIKE '% 22 %') +
(colName LIKE '% 333 %') +
IF(colName LIKE '% 4444 %', 2, 0) AS matches
你也可以使用乘法:
SELECT colName,
(colName LIKE '% 22 %') +
(colName LIKE '% 333 %') +
2 * (colName LIKE '% 4444 %') AS matches