0

I have some difficulties to make a query.

Basically, I have a 3 tables: member, build and up:

member
--+---
id|...

build
--+-----------+---
id|member (fk)|...

up
--+-----------+---------
id|member (fk)|build (fk)

How can I get the total of "up" for each member (build owner)? I only know how to get it for each build:

SELECT up.build, COUNT(*) as totalUp FROM up GROUP BY build

To be clear, I'm not asking about this query:

SELECT up.member, COUNT(*) as totalUp FROM up GROUP BY member

More details

In my application, a member can "up" (it's a kind of "like") a build. When a member up a build, there is a new entry in the table up with the id of the build liked and the id of the member who liked the build. Each member can have a build.

Globally I want to get the total of likes for each build owner.

Example:

member
--+---
id|
0 |
1 |
2 |

build
--+------+---
id|member|...
--+------+---
0 | 1
1 | 1
2 | 2

up
--+------+-----
id|member|build
--+------+-----
0 |0     |0
1 |0     |1
2 |0     |2

The result of the query should be:

memberID|totalUp
--------+-------
1       |2
0       |0
2       |1
5
  • 1
    No idea what you're looking for then. You might want to explain in more detail.
    – tombom
    Commented Nov 5, 2014 at 0:10
  • I updated the post. Should be more clear now. :)
    – gaetanm
    Commented Nov 5, 2014 at 8:59
  • Why does the up.member column have only 0s? Commented Nov 5, 2014 at 10:02
  • Because only the member with the id 0 liked the builds.
    – gaetanm
    Commented Nov 5, 2014 at 10:03
  • You need to improve the naming of your columns. Commented Nov 5, 2014 at 10:04

1 Answer 1

1
SELECT
    A.id build_owner,
    COUNT(C.build) build_member_count
FROM member A
INNER JOIN build B ON A.id = B.member       -- or LEFT joins
INNER JOIN up    C ON B.id = C.build        -- 
GROUP BY A.id ;
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.