Description:
Variable @stosunek returns NULL when it's declared at beginning of query or in query executed before this example query after line 6 always returns NULL
1 SELECT (...)
2 IF(LOCATE('matrix',LOWER(ff_posts_text.post_text))>1,
3 IFNULL(@licz:=@licz+1,@licz:=1),@licz),
4 @wyrazBz:=ABS(LENGTH(ff_posts_text.post_text) -
5 LENGTH(REPLACE(ff_posts_text.post_text,SUBSTRING('matrix',2),'matrix'))),
6 IF(@wyrazBz>0,@stosunek:=IFNULL(@stosunek+(@wyrazBz*0.1),@stosunek),@stosunek) ,
7 IF(LOCATE('matrix',LOWER(ff_posts_text.post_subject))>0,
8 @stosunek:=@stosunek+0.05,@stosunek),
9 IF(LOCATE('matrix',LOWER(ff_topics.topic_title))>0,
10 @stosunek:=@stosunek+2,@stosunek), @stosunek:=@stosunek*100 (...)
I tried without declaring variable and and with this expression instead of line 6 expresion:
IF(@wyrazBz>0,@stosunek:=@stosunek+(@wyrazBz*0.1),0)
But, when I declare variable @stosunek by this method:
@stosunek:=(IFNULL(@licz,0)/CHAR_LENGTH(ff_posts_text.post_text))
all expressions returns correct values.
How to repeat:
Full query (tables from any phpbb forum)
SELECT DISTINCT ff_topics.topic_id, ff_posts.post_time, IF(LOCATE('matrix',LOWER(ff_posts_text.post_text))>1, IFNULL(@licz:=@licz+1,@licz:=1),@licz), @wyrazBz:=ABS(LENGTH(ff_posts_text.post_text) - LENGTH(REPLACE(ff_posts_text.post_text,SUBSTRING('matrix',2),'matrix'))), IF(@wyrazBz>0,@stosunek:=IFNULL(@stosunek+(@wyrazBz*0.1),@stosunek),@stosunek) ,IF(LOCATE('matrix',LOWER(ff_posts_text.post_subject))>0, @stosunek:=@stosunek+0.05,@stosunek), IF(LOCATE('matrix',LOWER(ff_topics.topic_title))>0, @stosunek:=@stosunek+2,@stosunek), @stosunek:=@stosunek*100, @stosunek AS stosunek, ff_topics.topic_title, ff_users.username AS autor, ff_users.user_id AS autor_id, ff_posts_text.post_id AS id, IF(LOCATE('matrix',LOWER(ff_posts_text.post_subject))>0, @stosunek:=@stosunek+(@stosunek*2),@stosunek), IF(LOCATE('matrix',LOWER(ff_topics.topic_title))>0, @stosunek:=@stosunek+5,@stosunek) FROM ff_users, ff_posts_text LEFT JOIN ff_posts ON ( ff_posts.post_id = ff_posts_text.post_id) LEFT JOIN ff_topics ON ( ff_posts.topic_id = ff_topics.topic_id) WHERE (ff_posts_text.post_text LIKE '%matrix%' OR ff_posts_text.post_subject LIKE '%matrix%' OR ff_topics.topic_title LIKE '%matrix%') AND ff_users.user_id=ff_topics.topic_poster GROUP BY ff_topics.topic_id ORDER BY ff_posts.post_time DESC LIMIT 100