问题
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