I wasn’t even sure how to phrase this question. I’ll give example content and wanted output, I’m looking for a query to do this.
Let’s say I have table called “flagged” with this content:
content_id | user_id
1 | 1
1 | 2
1 | 3
2 | 1
2 | 3
2 | 4
3 | 2
3 | 3
4 | 1
4 | 2
5 | 1
6 | 1
6 | 4
And I have a a-symmetrical relationship between content_ids:
master_content_id | slave_content_id
1 | 2
3 | 4
5 | 6
For each “master” content_id (1, 3 and 5), I want to count how many distinct users have flagged either the master or the slave content, but count someone who flagged both as a single flag – which means that in the above example, content_id=1 was counted by user_id=1 (as content_id=1 and content_id=2), by user_id=2 (as content_id=1), by user_id=3 (as content_id=1 and content_id=2), and by user_id=4 (as content_id=2!)
An example of the output of the query I want to make is:
content_id | user_count
1 | 4 # users 1, 2, 3, 4
3 | 3 # users 1, 2, 3
5 | 2 # users 1, 4
I can’t assume that the related content_ids are always a consecutive odd/even (i.e. 66 can be the master of the slave 58)
I am using MySQL and don’t mind using its extensions to SQL (but rather the query be ANSI, or at least portable to the most databases)
From the samples given, does this do the job (I don’t have MySQL available to test)?
It would be better not to have the DISTINCT, but I can’t see a way around it.