我有脚本:
SELECT *, (pbct_hits + (COUNT(likes.rvw_usr_like) * 5) - (COUNT(unlikes.rvw_usr_like)) * 5) AS score
FROM tb_publications
LEFT JOIN tb_reviews_users likes ON likes.rvw_usr_fk_publication = pbct_id AND likes.rvw_usr_like IS TRUE
LEFT JOIN tb_reviews_users unlikes ON unlikes.rvw_usr_fk_publication = pbct_id AND unlikes.rvw_usr_like IS FALSE
GROUP BY pbct_id
ORDER BY score DESC;
我不想对同一张表进行两次连接。
我相信可以优化上述脚本,但我没有得到。
编辑
问题解决了:
-- Final Script:
SELECT pbct.*
FROM tb_publications pbct
LEFT JOIN tb_reviews_users ON rvw_usr_fk_publication = pbct_id
GROUP BY pbct_id
ORDER BY
(
(pbct_hits * 1) +
((SUM(CASE WHEN rvw_usr_like IS TRUE THEN 1 ELSE 0 END)) * 5) -
((SUM(CASE WHEN rvw_usr_like IS FALSE THEN 1 ELSE 0 END)) * 5)
) DESC, pbct_record ASC;
基于@MikeSmithDev 的回答。