Make number of matches required directly corresponds with char_length of column

北慕城南 提交于 2019-12-25 06:37:53

问题


I have a working SELECT statement. However, I would like to add something to it. I would like to make it so that the number of matches required, directly corresponds with the char_length of the column 'input'. So, for example:

if (char_length(input) <= 5) { matches required is 1 }
if (char_length(input) > 5 && char_length(input) <= 10) { matches required is 2 }
if (char_length(input) > 10 && char_length(input) <= 15) { matches required is 3 }

and ect...

How do I add ^^^that to the SELECT statement below?

$text = "one";
$textLen = strlen($text);

SELECT response, ( input LIKE  '% $text %' ) as matches
FROM allData
WHERE (char_length(input) >= '$textLen'-($textLen*.1) 
AND char_length(input) <= '$textLen'+($textLen*.1)) 
HAVING matches > 0 
AND matches = (select max(( input LIKE  '% $text %' )) from allData) limit 30;

回答1:


Run the following query separately first:

SELECT @limit := 0;

Then modify your query to look like this:

SELECT response, ( input LIKE  '% $text %' ) as matches, @limit := @limit + 1
FROM allData
WHERE (char_length(input) >= '$textLen'-($textLen*.1) 
AND char_length(input) <= '$textLen'+($textLen*.1)) 
AND @limit < CEIL(CHAR_LENGTH(input) / 5)
HAVING matches > 0 
AND matches = (select max(( input LIKE  '% $text %' )) from allData) limit 30;

That should limit your matches to the needed values



来源:https://stackoverflow.com/questions/32682273/make-number-of-matches-required-directly-corresponds-with-char-length-of-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!