Make number of matches required directly correspon

2019-09-08 12:51发布

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条回答
再贱就再见
2楼-- · 2019-09-08 13:18

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

查看更多
登录 后发表回答