2015-09-20 22 views
0

我有一個工作的SELECT語句。不過,我想補充一點。我想使它匹配所需的匹配數量,直接對應於'input'列的char_length。因此,舉例來說:使所需的匹配數量直接對應列的char_length

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... 

如何添加^^^,爲下面的SELECT語句?

$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

運行下面的查詢分開第一:

SELECT @limit := 0; 

然後修改您的查詢,看起來像這樣:

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; 

這應該限制你匹配到需要的值

+0

它不起作用。它和以前一樣工作,沒有額外的變化。 http://stackoverflow.com/questions/32704222/selecting-matches-according-to-the-char-length-of-the-strings-in-rows?noredirect=1#comment53253000_32704222請看看這個問題。我已經用你的例子重新提出了這個問題,並增加了更多的細節。 – jessica

相關問題