获取SQL查询中创建的别名总和

时间:2014-07-01 13:42:35

标签: mysql sql sum alias relevance

我有一个更大的SQL查询,我使用Boolean Full-Text Searches来获取relevance values

我得到两个相关值:

  • 一个用于标记(scoreTags),乘以3(更高的重要性)
  • 一个用于内容(scoreContent)

两者都分配给aliasses。现在我想在同一个SQL查询中的新别名(superScore)中为每个匹配添加它们,但似乎sql不能使用别名来计算它。

(scoreTags + scoreContent) AS superScore

我得到了以下错误:

mysqlt error: [1054: Unknown column 'scoreTags' in 'field list']

这是我完整的SQL语句:

SELECT csia.cID, csia.ak_tags, p.cParentID, cv.cvName, 
(MATCH (csia.ak_tags) AGAINST ('Bestäubung Bienen Politik' IN BOOLEAN MODE) * 3) AS scoreTags, 
MATCH (psi.content) AGAINST ('Bestäubung Bienen Politik' IN BOOLEAN MODE) AS scoreContent,        
(scoreTags + scoreContent) AS superScore 
FROM CollectionSearchIndexAttributes csia 
LEFT JOIN Pages p 
ON csia.cID = p.cID 
LEFT JOIN CollectionVersions cv 
ON csia.cID = cv.cID 
LEFT JOIN PageSearchIndex psi 
ON csia.cID = psi.cID 
WHERE cv.cvIsApproved = 1 
AND (MATCH (csia.ak_tags) AGAINST ('Bestäubung Bienen Politik' IN BOOLEAN MODE)) 
ORDER BY scoreContent DESC, scoreTags DESC, csia.cID DESC

我也尝试了SUM功能,但它也没有用。你知道如何让它工作吗?谢谢!

我想要的例子:

与cID 9932的查询匹配的相关性值(scoreTags)为5,相关性值为4(scoreContent)。该superScore应为19((5 * 3)+ 4)。

1 个答案:

答案 0 :(得分:1)

您可以使用实际函数替换别名,也可以将此语句放入派生表中,然后从中进行聚合。

所以第一个是:

SELECT csia.cID, 
       csia.ak_tags, 
       p.cParentID, cv.cvName, 
       (MATCH (csia.ak_tags) 
        AGAINST ('Bestäubung Bienen Politik' 
        IN BOOLEAN MODE) * 3) AS scoreTags, 
       MATCH (psi.content) 
       AGAINST ('Bestäubung Bienen Politik' 
       IN BOOLEAN MODE) AS scoreContent,        
       (MATCH (csia.ak_tags) 
       AGAINST ('Bestäubung Bienen Politik' IN BOOLEAN MODE) * 3) 
       + 
       MATCH (psi.content) 
       AGAINST ('Bestäubung Bienen Politik' IN BOOLEAN MODE) AS SUperScore        
FROM   CollectionSearchIndexAttributes csia 
LEFT JOIN Pages p 
ON csia.cID = p.cID 
LEFT JOIN CollectionVersions cv 
ON csia.cID = cv.cID 
LEFT JOIN PageSearchIndex psi 
ON csia.cID = psi.cID 
WHERE cv.cvIsApproved = 1 
AND (MATCH (csia.ak_tags) AGAINST ('Bestäubung Bienen Politik' IN BOOLEAN MODE)) 
ORDER BY scoreContent DESC, scoreTags DESC, csia.cID DESC

或者第二个:

select cID, 
       ak_tags, 
       cParentID, 
       cvName, 
       scoreTags, 
       scoreContent, 
       scoreTags+ScoreContent as SuperScore from (
            SELECT csia.cID, csia.ak_tags, p.cParentID, cv.cvName, 
            (MATCH (csia.ak_tags) 
            AGAINST ('Bestäubung Bienen Politik' 
            IN BOOLEAN MODE) * 3) AS scoreTags, 
            MATCH (psi.content) 
            AGAINST ('Bestäubung Bienen Politik' 
            IN BOOLEAN MODE) AS scoreContent
      FROM CollectionSearchIndexAttributes csia 
      LEFT JOIN Pages p 
      ON csia.cID = p.cID 
      LEFT JOIN CollectionVersions cv 
      ON csia.cID = cv.cID 
      LEFT JOIN PageSearchIndex psi 
      ON csia.cID = psi.cID 
      WHERE cv.cvIsApproved = 1 
      AND (MATCH (csia.ak_tags) AGAINST ('Bestäubung Bienen Politik' IN BOOLEAN MODE))
 ) x
 ORDER BY scoreContent DESC, scoreTags DESC, csia.cID DESC