0

我设法编写了下面的查询,它几乎可以工作,问题是任何空值都被IFNULL(likes.num, 0)结果表末尾的 get 清零。

SELECT t.*, IFNULL(likes.num, 0)
FROM `textagname` as t
LEFT JOIN likes
ON t.tex = likes.tex
ORDER BY num DESC

有没有另一种方法来编写这个查询,而不是这样:

SELECT *
FROM (
    SELECT t.*, IFNULL(likes.num, 0)
    FROM `textagname` as t
    LEFT JOIN likes
    ON t.tex = likes.tex
)
ORDER BY d.num DESC

最好是一种不会使其花费更长的时间的方法。

4

1 回答 1

1

诀窍是按生成的值而不是来自表的值进行排序。请注意下面添加了 numLikes。

SELECT t.*, IFNULL(likes.num, 0) as numLikes
FROM `textagname` as t
LEFT JOIN likes
ON t.tex = likes.tex
ORDER BY numLikes DESC
于 2012-04-29T22:15:20.817 回答