SELECT qa_posts.postid, qa_posts.categoryid, qa_posts.type, LEFT(qa_posts.type, 1) AS basetype, INSTR(qa_posts.type, '_HIDDEN')>0 AS hidden, qa_posts.acount, qa_posts.selchildid, qa_posts.closedbyid, qa_posts.upvotes, qa_posts.downvotes, qa_posts.netvotes, qa_posts.views, qa_posts.hotness, qa_posts.flagcount, qa_posts.title, qa_posts.tags, UNIX_TIMESTAMP(qa_posts.created) AS created, qa_categories.title AS categoryname, qa_categories.backpath AS categorybackpath, qa_posts.userid, qa_posts.cookieid, INET_NTOA(qa_posts.createip) AS createip, qa_userpoints.points, qa_users.flags, qa_users.level, qa_users.email AS email, qa_users.handle AS handle, qa_users.avatarblobid, qa_users.avatarwidth, qa_users.avatarheight, score, matchparts FROM qa_posts LEFT JOIN qa_categories ON qa_categories.categoryid=qa_posts.categoryid LEFT JOIN qa_users ON qa_posts.userid=qa_users.userid LEFT JOIN qa_userpoints ON qa_posts.userid=qa_userpoints.userid JOIN (SELECT questionid, SUM(score)+2*(LOG(10000)*(qa_posts.hotness-(SELECT MIN(hotness) FROM qa_posts WHERE type='Q'))/((SELECT MAX(hotness) FROM qa_posts WHERE type='Q')-(SELECT MIN(hotness) FROM qa_posts WHERE type='Q')))+LOG(questionid)/1000000 AS score, GROUP_CONCAT(CONCAT_WS(':', matchposttype, matchpostid, ROUND(score,3))) AS matchparts FROM ((SELECT postid AS questionid, LOG(10000/titlecount) AS score, 'Q' AS matchposttype, postid AS matchpostid FROM qa_titlewords JOIN qa_words ON qa_titlewords.wordid=qa_words.wordid WHERE word IN ('all','questions') AND titlecount<10000) UNION ALL (SELECT questionid, (1-1/(1+count))*LOG(10000/contentcount)*(CASE qa_contentwords.type WHEN 'Q' THEN 1.0 WHEN 'A' THEN 0.5 ELSE 0.25 END) AS score, qa_contentwords.type AS matchposttype, qa_contentwords.postid AS matchpostid FROM qa_contentwords JOIN qa_words ON qa_contentwords.wordid=qa_words.wordid WHERE word IN ('all','questions') AND contentcount<10000) UNION ALL (SELECT postid AS questionid, 2*LOG(10000/tagwordcount) AS score, 'Q' AS matchposttype, postid AS matchpostid FROM qa_tagwords JOIN qa_words ON qa_tagwords.wordid=qa_words.wordid WHERE word IN ('all','questions') AND tagwordcount<10000) UNION ALL (SELECT postid AS questionid, LOG(10000/qposts) AS score, 'Q' AS matchposttype, postid AS matchpostid FROM qa_posts JOIN qa_users ON qa_posts.userid=qa_users.userid JOIN qa_userpoints ON qa_userpoints.userid=qa_users.userid WHERE handle IN ('all','questions') AND type='Q') UNION ALL (SELECT postid AS questionid, LOG(10000/qposts) AS score, 'Q' AS matchposttype, postid AS matchpostid FROM qa_posts JOIN qa_users ON qa_posts.userid=qa_users.userid JOIN qa_userpoints ON qa_userpoints.userid=qa_users.userid WHERE handle='all questions' AND type='Q')) x LEFT JOIN qa_posts ON qa_posts.postid=questionid GROUP BY questionid ORDER BY score DESC LIMIT 0,21) y ON qa_posts.postid=y.questionid