参考' r1'不支持(参考组功能)

时间:2018-03-14 17:38:04

标签: php sql

我可以帮助别人吗?我想通过总评分DESC订购用户,2个评分在不同的表格中,我的sql:

SELECT dle_users.user_id, dle_users.user_group, dle_users.name, 
       dle_users.foto, 
       SUM(dle_comments.rating) as r1, 
       SUM(dle_post_extras.rating) as r2
FROM dle_users
JOIN dle_comments
ON dle_comments.user_id = dle_users.user_id
JOIN dle_post_extras ON dle_post_extras.user_id = dle_users.user_id
WHERE dle_comments.rating 
AND dle_post_extras.rating > 0
GROUP BY dle_users.user_id
ORDER BY SUM(r1+r2) DESC 
LIMIT 0,10

1 个答案:

答案 0 :(得分:0)

加入前汇总或您的金额将被取消:

SELECT u.user_id, u.user_group, u.name, u.foto, 
       COALESCE(c.r1, 0) as r1, COALESCE(e.r2, 0) as r2 
FROM dle_users u LEFT JOIN
     (SELECT user_id, SUM(rating) as r1
      FROM dle_comments
      GROUP BY user_id
     ) c
     ON c.user_id = u.user_id LEFT JOIN
     (SELECT user_id, SUM(rating) as r2
      FROM dle_post_extras
      WHERE rating > 0
      GROUP BY user_id
     ) e
     ON e.user_id = u.user_id
GROUP BY dle_users.user_id
ORDER BY COALESCE(r1, 0) + COALESCE(r2, 0) DESC 
LIMIT 0, 10