1

是否可以从此 SQL 中删除子查询?我需要按“匹配”分数排序,但显然不能按别名排序。

SELECT *
FROM
  (SELECT b.shortDesc,
          b.img,
          sm.uri,
          match(`bodyCopy`, `shortDesc`) against ('Storage' IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION) AS score
   FROM `blog` b
   JOIN `sitemap` sm ON sm.id = b.pageId
   WHERE 'Active' IN (b.status, sm.status)
  ) t1
WHERE score > 0
ORDER BY score DESC
4

2 回答 2

4

您可以摆脱子查询:

SELECT b.shortDesc,
       b.img,
       sm.uri,
       match(`bodyCopy`, `shortDesc`) against ('Storage' IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION) AS score
FROM `blog` b
JOIN `sitemap` sm ON sm.id = b.pageId
WHERE 'Active' IN (b.status, sm.status)
HAVING score > 0
ORDER BY score DESC

在计算结果之前,的值score是未知的。这意味着它不能在WHERE子句中使用。相反,它可以用于HAVING. ORDER最后应用,可以在score这里使用。

文档:SELECT(也用于HAVINGand ORDER

于 2013-05-28T18:13:02.233 回答
0

这里:

SELECT b.shortDesc,
      b.img,
      sm.uri,
      match(`bodyCopy`, `shortDesc`) against ('Storage' IN NATURAL LANGUAGE MODE WITH   
      QUERY EXPANSION) AS score
      FROM `blog` b
     JOIN `sitemap` sm ON sm.id = b.pageId
     WHERE 'Active' IN (b.status, sm.status) and score > 0  
    ORDER BY score DESC
于 2013-05-28T18:12:34.187 回答