0

I'm using the following query to filter and score results which are assigned to certain categories which is working perfectly right now. However, if I try to do another join, to be able to factor in tag matches to the scoring, I run in to an issue where lots of results are returned which aren't assigned to the 3 categories.

Here's the working query...

        SELECT DISTINCT results.*,
                    ( 
                        3*(MATCH(body) AGAINST('*' IN BOOLEAN MODE)) + 
                        5*(MATCH(title) AGAINST('*' IN BOOLEAN MODE)) + 
                        1*usefulness + 
                        30*(MATCH(body) AGAINST('""' IN BOOLEAN MODE)) + 
                        20*(MATCH(title) AGAINST('""' IN BOOLEAN MODE)) + 
                        5*shares 
                    ) AS score 
        FROM results
        INNER JOIN categories c on results.ID = c.RESULT_ID
        WHERE c.name in ('refinance', 'condo', 'usda')
        AND ( results.scope = 'all' OR results.scope = 'hi' )
        AND published = 1

        GROUP BY results.ID
        HAVING COUNT(c.c_ID) = 3
        ORDER BY score DESC 
        LIMIT 8 OFFSET 0

Adding the following line below the categories results, is what's giving me problems

INNER JOIN tags ON results.id = tags.result_id

It's as if the following line stops working when I add the 2nd join

HAVING COUNT(c.c_ID) = 3

I'm at a loss here and any help would be greatly appreciated!

4

1 回答 1

1

失败是因为新count()联接中有多行与原始数据匹配。 Count()计算非空值。因此,如果每个 id 有两个匹配项,那么您将获得 6 行 - 计数为 6。

我认为以下将解决您的问题:

having count(distinct c.c_id) = 3
于 2013-04-25T13:32:27.130 回答