This is a follow-up to this previous question: Complicated COUNT query in MySQL. None of the answers worked under all conditions, and I have had trouble figuring out a solution as well. I will be awarding a 75 point bounty to the first person that provides a fully correct answer (I will award the bounty as soon as it is available, and as reference I've done this before: Improving Python/django view code).
I want to get the count of video credits a user has and not allow duplicates (i.e., for every video a user can be credited in it 0 or 1 times. I want to find three counts: the number of videos a user has uploaded (easy) -- Uploads; the number of videos credited in from videos not uploaded by the user -- Credited_by_others; and the total number of videos a user has been credited in -- Total_credits.
I have three tables:
CREATE TABLE `userprofile_userprofile` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`full_name` varchar(100) NOT NULL,
...
)
CREATE TABLE `videos_video` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` int(11) NOT NULL,
`uploaded_by_id` int(11) NOT NULL,
...
KEY `userprofile_video_e43a31e7` (`uploaded_by_id`),
CONSTRAINT `uploaded_by_id_refs_id_492ba9396be0968c` FOREIGN KEY (`uploaded_by_id`) REFERENCES `userprofile_userprofile` (`id`)
)
Note that the uploaded_by_id is the same as the userprofile.id
CREATE TABLE `videos_videocredit` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`video_id` int(11) NOT NULL,
`profile_id` int(11) DEFAULT NULL,
`position` int(11) NOT NULL
...
KEY `videos_videocredit_fa26288c` (`video_id`),
KEY `videos_videocredit_141c6eec` (`profile_id`),
CONSTRAINT `profile_id_refs_id_31fc4a6405dffd9f` FOREIGN KEY (`profile_id`) REFERENCES `userprofile_userprofile` (`id`),
CONSTRAINT `video_id_refs_id_4dcff2eeed362a80` FOREIGN KEY (`video_id`) REFERENCES `videos_video` (`id`)
)
Here is a step-by-step to illustrate:
1) create 2 users:
insert into userprofile_userprofile (id, full_name) values (1, 'John Smith');
insert into userprofile_userprofile (id, full_name) values (2, 'Jane Doe');
2) a user uploads a video. He does not yet credit anyone -- including himself -- in it.
insert into videos_video (id, title, uploaded_by_id) values (1, 'Hamlet', 1);
The result should be as follows:
**User** **Uploads** **Credited_by_others** **Total_credits**
John Smith 1 0 1
Jane Doe 0 0 0
3) the user who uploaded the video now credits himself in the video. Note this should not change anything, since the user has already received a credit for uploading the film and I am not allowing duplicate credits:
insert into videos_videocredit (id, video_id, profile_id, position) values (1, 1, 1, 'director')
The result should now be as follows:
**User** **Uploads** **Credited_by_others** **Total_credits**
John Smith 1 0 1
Jane Doe 0 0 0
4) The user now credits himself two more times in the same video (i.e., he has had multiple 'positions' in the video). In addition, he credits Jane Doe three times for that video:
insert into videos_videocredit (id, video_id, profile_id, position) values (2, 1, 1, 'writer')
insert into videos_videocredit (id, video_id, profile_id, position) values (3, 1, 1, 'producer')
insert into videos_videocredit (id, video_id, profile_id, position) values (4, 1, 2, 'director')
insert into videos_videocredit (id, video_id, profile_id, position) values (5, 1, 2, 'editor')
insert into videos_videocredit (id, video_id, profile_id, position) values (6, 1, 2, 'decorator')
The result should now be as follows:
**User** **Uploads** **Credited_by_others** **Total_credits**
John Smith 1 0 1
Jane Doe 0 1 1
5) Jane Doe now uploads a video. She does not credit herself, but credits John Smith twice in the video:
insert into videos_video (id, title, uploaded_by_id) values (2, 'Othello', 2)
insert into videos_videocredit (id, video_id, profile_id, position) values (7, 2, 1, 'writer')
insert into videos_videocredit (id, video_id, profile_id, position) values (8, 2, 1, 'producer')
The result should now be as follows:
**User** **Uploads** **Credited_by_others** **Total_credits**
John Smith 1 1 2
Jane Doe 1 1 2
So, I would like to find those three fields for each user -- Uploads, Credited_by_others, and Total_credits. Data should never be Null, but instead be 0 when the field has no count. Thank you.
(7, 2, 1)and(8, 2, 1)?