MySQL SELECT 使用 LEFT JOIN 和 SUM GROUP BY
MySQL SELECT with LEFT JOIN & SUM GROUP BY
需要一些关于 mysql select 左连接的帮助,我尝试了很多选项,在堆栈中查找答案,但无法解决这个问题。我将不胜感激任何帮助。谢谢。
SELECT profile_main.id,
profile_main.add_date,
profile_main.expire_date,
profile_main.status_begin,
profile_main.status_expire,
profile_main.views,
profile_main.last,
profile_main.confirm,
profile_info.name,
profile_contacts.remains,
profile_rating.rating
FROM profile_main
LEFT JOIN profile_info
ON profile_main.id=profile_info.profile_id
LEFT JOIN profile_contacts
ON profile_main.id=profile_contacts.profile_id
LEFT JOIN (
SELECT profile_rating.profile_id, SUM(profile_rating.rating)
FROM profile_rating
GROUP BY profile_rating.profile_id
) profile_rating
ON profile_main.id=profile_rating.profile_id
WHERE profile_main.user_id=$user_id
AND profile_main.deleted=0
问题是 LEFT JOIN
和 profile_rating
。我需要 SUM(
profile_rating.rating)
这里是profile_maintable。 table 保存所有配置文件的内容
enter image description here
这里是profile_ratingtable。 [edit] 此 table 可能不包含所有个人资料评级,仅包含较早被评级的个人资料
id profile_id user_id rating ip_addr add_date deleted
--------------------------------------------------------------
1 16 5 15 111.111.111.111 123432433 0
2 16 5 17 111.111.111.111 123432433 0
3 16 5 25 111.111.111.111 123432433 0
4 16 5 34 111.111.111.111 123432433 0
5 16 5 12 111.111.111.111 123432433 0
如有其他需要,我再补充
你为什么SELECT
在JOIN
?
只需 select 主 select 中的字段:
SELECT profile_main.id,
profile_main.add_date,
profile_main.expire_date,
profile_main.status_begin,
profile_main.status_expire,
profile_main.views,
profile_main.last,
profile_main.confirm,
profile_info.name,
profile_contacts.remains,
profile_rating.rating,
profile_rating.profile_id,
SUM(profile_rating.rating) AS rating_sum
FROM profile_main
LEFT JOIN profile_info
ON profile_main.id=profile_info.profile_id
LEFT JOIN profile_contacts
ON profile_main.id=profile_contacts.profile_id
LEFT JOIN profile_rating
ON profile_main.id=profile_rating.profile_id
WHERE profile_main.user_id=$user_id
AND profile_main.deleted=0
GROUP BY profile_rating.profile_id
因此您得到的总评分为 rating_sum
你可以尝试下面的查询,如果需要其他东西让我知道,这样我就可以修改-
SELECT profile_main.id,
profile_main.add_date,
profile_main.expire_date,
profile_main.status_begin,
profile_main.status_expire,
profile_main.views,
profile_main.last,
profile_main.confirm,
profile_info.name,
profile_contacts.remains,
SUM(profile_rating.rating)
FROM profile_main
LEFT JOIN profile_info
ON profile_main.id=profile_info.profile_id
LEFT JOIN profile_contacts
ON profile_main.id=profile_contacts.profile_id
LEFT JOIN profile_rating
ON profile_main.id=profile_rating.profile_id
WHERE profile_main.user_id=$user_id
AND profile_main.deleted=0
GROUP BY profile_main.id
您的查询看起来不错,加入预先聚合的数据是个好主意,就像您在此处所做的那样。 (一个好习惯,以免错误地将一个 table 的聚合与另一个 table 的匹配项相乘。)
但是您访问的字段profile_rating.rating
是不可用的,因为您不再处理单个记录值,而是求和。为总和取一个别名:SUM(rating) AS sum_rating
并在 select 子句中使用它。
SELECT
m.id,
m.add_date,
m.expire_date,
m.status_begin,
m.status_expire,
m.views,
m.last,
m.confirm,
i.name,
c.remains,
r.sum_rating
FROM profile_main m
LEFT JOIN profile_info i ON m.id = i.profile_id
LEFT JOIN profile_contacts c ON m.id = c.profile_id
LEFT JOIN
(
SELECT
profile_id,
SUM(rating) AS sum_rating
FROM profile_rating
GROUP BY profile_id
) r ON m.id = r.profile_id
WHERE m.user_id = $user_id
AND m.deleted = 0;
我还使用 table 别名来增强可读性。
类似的问题已经在SO上得到了解答。您在不可用的错误列上执行 groupby,请使用 table 别名,如上答案所示。请参考这些链接:
SQL Server Query LEFT JOIN, SUM and GROUP BY and I'm stumped!
和
Left join, sum and count group by
需要一些关于 mysql select 左连接的帮助,我尝试了很多选项,在堆栈中查找答案,但无法解决这个问题。我将不胜感激任何帮助。谢谢。
SELECT profile_main.id,
profile_main.add_date,
profile_main.expire_date,
profile_main.status_begin,
profile_main.status_expire,
profile_main.views,
profile_main.last,
profile_main.confirm,
profile_info.name,
profile_contacts.remains,
profile_rating.rating
FROM profile_main
LEFT JOIN profile_info
ON profile_main.id=profile_info.profile_id
LEFT JOIN profile_contacts
ON profile_main.id=profile_contacts.profile_id
LEFT JOIN (
SELECT profile_rating.profile_id, SUM(profile_rating.rating)
FROM profile_rating
GROUP BY profile_rating.profile_id
) profile_rating
ON profile_main.id=profile_rating.profile_id
WHERE profile_main.user_id=$user_id
AND profile_main.deleted=0
问题是 LEFT JOIN
和 profile_rating
。我需要 SUM(
profile_rating.rating)
这里是profile_maintable。 table 保存所有配置文件的内容 enter image description here
这里是profile_ratingtable。 [edit] 此 table 可能不包含所有个人资料评级,仅包含较早被评级的个人资料
id profile_id user_id rating ip_addr add_date deleted
--------------------------------------------------------------
1 16 5 15 111.111.111.111 123432433 0
2 16 5 17 111.111.111.111 123432433 0
3 16 5 25 111.111.111.111 123432433 0
4 16 5 34 111.111.111.111 123432433 0
5 16 5 12 111.111.111.111 123432433 0
如有其他需要,我再补充
你为什么SELECT
在JOIN
?
只需 select 主 select 中的字段:
SELECT profile_main.id,
profile_main.add_date,
profile_main.expire_date,
profile_main.status_begin,
profile_main.status_expire,
profile_main.views,
profile_main.last,
profile_main.confirm,
profile_info.name,
profile_contacts.remains,
profile_rating.rating,
profile_rating.profile_id,
SUM(profile_rating.rating) AS rating_sum
FROM profile_main
LEFT JOIN profile_info
ON profile_main.id=profile_info.profile_id
LEFT JOIN profile_contacts
ON profile_main.id=profile_contacts.profile_id
LEFT JOIN profile_rating
ON profile_main.id=profile_rating.profile_id
WHERE profile_main.user_id=$user_id
AND profile_main.deleted=0
GROUP BY profile_rating.profile_id
因此您得到的总评分为 rating_sum
你可以尝试下面的查询,如果需要其他东西让我知道,这样我就可以修改-
SELECT profile_main.id,
profile_main.add_date,
profile_main.expire_date,
profile_main.status_begin,
profile_main.status_expire,
profile_main.views,
profile_main.last,
profile_main.confirm,
profile_info.name,
profile_contacts.remains,
SUM(profile_rating.rating)
FROM profile_main
LEFT JOIN profile_info
ON profile_main.id=profile_info.profile_id
LEFT JOIN profile_contacts
ON profile_main.id=profile_contacts.profile_id
LEFT JOIN profile_rating
ON profile_main.id=profile_rating.profile_id
WHERE profile_main.user_id=$user_id
AND profile_main.deleted=0
GROUP BY profile_main.id
您的查询看起来不错,加入预先聚合的数据是个好主意,就像您在此处所做的那样。 (一个好习惯,以免错误地将一个 table 的聚合与另一个 table 的匹配项相乘。)
但是您访问的字段profile_rating.rating
是不可用的,因为您不再处理单个记录值,而是求和。为总和取一个别名:SUM(rating) AS sum_rating
并在 select 子句中使用它。
SELECT
m.id,
m.add_date,
m.expire_date,
m.status_begin,
m.status_expire,
m.views,
m.last,
m.confirm,
i.name,
c.remains,
r.sum_rating
FROM profile_main m
LEFT JOIN profile_info i ON m.id = i.profile_id
LEFT JOIN profile_contacts c ON m.id = c.profile_id
LEFT JOIN
(
SELECT
profile_id,
SUM(rating) AS sum_rating
FROM profile_rating
GROUP BY profile_id
) r ON m.id = r.profile_id
WHERE m.user_id = $user_id
AND m.deleted = 0;
我还使用 table 别名来增强可读性。
类似的问题已经在SO上得到了解答。您在不可用的错误列上执行 groupby,请使用 table 别名,如上答案所示。请参考这些链接:
SQL Server Query LEFT JOIN, SUM and GROUP BY and I'm stumped!
和
Left join, sum and count group by