내 대답은 다음 y
테이블의 의견과 총 평가 수를 가져옵니다 동안
select x.id, x.active, x.ip, x.registered, x.username, x.email, x.role,
x.posts, y.comments, x.rating + y.rating as rating
from
(select u.id, u.active, u.ip, u.registered, u.username, u.email, u.role,
count(p.user_id) as posts, ifnull(sum(pr.rating),0) as rating
from users u
join posts p on p.user_id=u.id
left join posts_ratings pr on pr.post_id=p.id) as x
join
(select u.id, u.active, u.ip, u.registered, u.username, u.email, u.role,
count(c.user_id) as comments, ifnull(sum(cr.rating),0) as rating
from users u
join comments c on c.user_id=u.id
left join comments_ratings cr on cr.comment_id=c.id) as y
on x.username=y.username;
x
표는 게시물의 수와 총 평가를 얻을 수 있습니다. 두 테이블 모두 사용자 이름 (또는 원하는 경우 user_id)으로 조인되며 각 테이블의 두 등급이 함께 추가됩니다.
select u.id, u.active, u.ip, u.registered, u.username, u.email, u.role, ... COLS here
후 최 선택 (첫 번째 줄)에 동일한 작업을 수행 :
select x.id, x.active, x.ip, x.registered, x.username, x.email, x.role, ... COLS here
명시 적으로 모두
x
및
y
테이블에있는 사용자 테이블에서 열을 나열해야합니다, 모든 사용자의 데이터를 얻으려면
특정 사용자를 얻으려면 표 x, 표 y 및 가장 바깥 쪽 선택 항목에 WHERE
절을 추가하십시오.
대단히 감사합니다. :) – kudlajz