我有三张桌子:
活动表中的like_count没有问题,我用这个查询计算了所有这些:
SELECT SUM(activities.like_count) AS likes, users.user_id, users.email, users.username
FROM activities
INNER JOIN users
ON activities.subject = users.user_id
GROUP BY user_id
但是,每当其他类型的帖子都被喜欢时,活动表中还有另一个计数被插入(未更新),我通过此查询计算它们:
SELECT activities.subject, activities.object, COUNT(activities.type) AS likes
FROM activities
INNER JOIN activities2
ON activities.object = activities2.object AND activities2.target_type = 'parent'
WHERE activities2.type LIKE 'like_%'
GROUP BY activities2.subject
但是当我想要将它们连接在一起时,问题就从这里开始了!我想计算like_count
+插入该表(activities2)中的喜欢的数量,其中包含'像_'除此之外,我想加入与其他表(用户)中的user_id相关的主题(在活动表中)。
我的最后一个问题是:
SELECT users.user_id, users.email, users.username, activities.object, COUNT(activities.type)+SUM(activities.like_count) AS likes
FROM activities
INNER JOIN activities2
ON activities.object = activities2.object AND activities2.target_type = 'parent'
INNER JOIN users
ON activities.subject = users.user_id
WHERE activities2.type LIKE 'like_%'
GROUP BY users.user_id
问题是加入时,它获取的用户信息不是我想要的信息。 总之,我想要这样的事情:
user_id------email-----------------username----------object-------likes
2521---------a@b.com---------------abc---------------9578---------3
5484---------c@d.com---------------def---------------8547---------16
8431---------e@f.com---------------ghi---------------4584---------1
并且当然结果是这样,但只有喜欢是正确的但是用户表中的user_id,email,username的列不是!
我想知道你是否愿意帮忙修复它。我真的厌倦了尝试并面对没有结果:(
用户表数据:
user_id-------------email---------------username
1-------------------a@b.com-------------abc
2-------------------c@d.com-------------def
3-------------------e@f.com-------------ghi
活动表数据:
object----------type----------------like_count------subject
20--------------like_video----------0---------------1
20--------------like_photo----------0---------------2
33--------------like_music----------0---------------3
33--------------some_other_type-----5---------------6
33--------------some_other_type-----8---------------10
活动2表数据:
object------target_type-----subject-----type
20----------parent----------30----------like_video
21----------owner-----------40----------like_audio
22----------parent----------50----------something_not_start_with_like_
我想:
user_id------email-----------------username----------object-------likes
1------------a@b.com---------------abc---------------9578---------(sum of like_count + count of type which has like_ in first characters)
2------------c@d.com---------------def---------------8547---------(sum of like_count + count of type which has like_ in first characters)
3------------e@f.com---------------ghi---------------4584---------(sum of like_count + count of type which has like_ in first characters)
答案 0 :(得分:0)
考虑使用派生表加入聚合查询,然后在外部查询中运行添加计算。此外, object 列下方已从第二个聚合查询的GROUP BY
子句中删除,但仍在JOIN
中使用,因为您需要在主题级别进行求和。
SELECT u.user_id, u.email, u.username, a.`object`, u.likes + a.likes as `total_likes`
FROM
(SELECT SUM(activities.like_count) AS likes, users.user_id, users.email, users.username
FROM activities
INNER JOIN users
ON activities.subject = users.user_id
GROUP BY user_id, users.email, users.username) u
INNER JOIN
(SELECT activities.subject, COUNT(activities.type) AS likes
FROM activities
INNER JOIN activities2
ON activities.object = activities2.object AND activities2.target_type = 'parent'
WHERE activities2.type LIKE 'like_%'
GROUP BY activities2.subject) As a
ON u.user_id = a.subject