0
SELECT A.id as ActivityId, A.description, T.id, T.title, COUNT(R.*) as reactionCount
FROM activities 
LEFT JOIN activitiesReactions as R ON R.activityId = A.id
LEFT JOIN activitiesTags as T ON A.tagId = T.id

So basically I need a query that will get all the activities, and at the same time NOT get the reactions of that activity but get the COUNT of reactions, which are found in another table called activitiesReactions, how do I do this (see above query that I had in mind).

So the query should return:

array('activityId' => 3, 'description' => 'doing work', 'reactionCount' => 2)

An example row:

Activities table:
id | description
 3   doing work
 4   checking mail

ActivitiesReactions table:
id | activityId | message
 1            3   you never do anywork, so that must be bullshit.
 2            3   yes I do alot of work!

so now it should return, "2" on reactionCount when I execute the query and doing WHERE A.id = 3


SELECT A.id as ActivityId, A.description, COUNT(R.activityId) AS reactionCount
FROM activities 
LEFT JOIN activitiesReactions as R
ON R.activityId = A.id
GROUP BY A.id

That did work, but the reactionCount returns as * 2, so for example if there are 3 reactions the reactionCount = 6, 2 reactions the reactionCount = 4 etc.

2
  • Your example does not make any sense … How do you come up with 2 as a result? Also, your array does not match the rows you show us Commented Nov 28, 2011 at 13:16
  • lol you are right, it is fixed now Commented Nov 28, 2011 at 13:19

2 Answers 2

5

Your query only needs a group by clause to make it work, eg.

SELECT A.id as ActivityId, A.description, COUNT(R.*) 
FROM activities 
    LEFT JOIN activitiesReactions as R ON R.activityId = A.id
GROUP BY A.id, A.description;
Sign up to request clarification or add additional context in comments.

1 Comment

You don't need the A.description in the group by in MySQL. You only need: GROUP BY A.id
2

If you want to count the number of reactions per activity, you have to group by activity.id after joining both tables:

SELECT A.id as ActivityId, A.description, COUNT(*) AS reactionCount
FROM activities 
LEFT JOIN activitiesReactions as R
ON R.activityId = A.id
GROUP BY A.id

5 Comments

Oke the reactionCount should be only COUNT(R.*) which is giving me an error, is it impossible? The problem is that I'm LEFT joining other tables too. (fixed my example)
Oke that worked, but for some reason the count is 2*the actual count, any clue? So if there are 2 reactions, the reactionCount = 4, if there are 3 reactions the reactionCount = 6
@user1066101, @ypercube: It should not matter if you group by * or by the id column of R (id will never be null due to the nature of join, so it does not make any difference). @user1066101: If you additionally join tags, you will always get COUNT(*) times number_of_tags_per_activity. I would not join the tag table if you don't have very good reasons for doing so …
I just have to get the tag of the activity too, shall I put it in a different query than?
I think your DB schema is flawed (or your example is not correct again) … the way you are joining the tables now, there can only be a single tag per activity (and then it does not explain why you are getting multiple rows …). But yes, you should query the tags in a different query, because otherwise you will not get the result you desire … (don't attempt to do multiple things in a single just because you can – use the right queries for the right job)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.