Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6200907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:27:17+00:00 2026-05-24T04:27:17+00:00

This is a follow-up to this previous question: Complicated COUNT query in MySQL .

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:27:18+00:00Added an answer on May 24, 2026 at 4:27 am

    I rewrote the query using joins, so it would become easier for the server to optimize.

    First two views to simplify the query

    CREATE VIEW IF NOT EXISTS vperson_videos AS
        SELECT
            v.uploaded_by_id AS id,
            COUNT(*) AS uploads
        FROM vvideo v
        GROUP BY v.uploaded_by_id;
    

    The above view just counts the number of videos which where uploaded by the user.

    CREATE VIEW vperson_credits AS
        SELECT
            c.profile_id AS id,
            COUNT(DISTINCT c.video_id) AS credits
        FROM vcredit c
        INNER JOIN vvideo cv ON cv.id = c.video_id
        WHERE cv.uploaded_by_id <> c.profile_id
        GROUP BY c.profile_id;
    

    The above view counts the number of (distinct) videos which where credited to the user, but ignoring those that the user has uploaded himself.

    Then the query itself:

    SELECT
        p.id,
        p.full_name,
        IFNULL(pv.uploads,0) AS uploads,
        IFNULL(pc.credits,0) AS credits,
        IFNULL(pv.uploads,0) + IFNULL(pc.credits,0) AS total_credits
    FROM vperson p
    LEFT OUTER JOIN vperson_videos pv ON pv.id = p.id
    LEFT OUTER JOIN vperson_credits pc ON pc.id = p.id;
    

    I used LEFT OUTER JOIN to include those users who has not uploaded any video or not been credited in any. IFNULL() was necessary because I would get NULL instead of 0.

    The final result is:

    +----+------------+---------+---------+---------------+
    | id | full_name  | uploads | credits | total_credits |
    +----+------------+---------+---------+---------------+
    |  1 | John Smith |       1 |       1 |             2 | 
    |  2 | Jane Doe   |       1 |       1 |             2 | 
    +----+------------+---------+---------+---------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, this is a follow up from a previous question here: C# formatting external
This a follow-up to a previous question . How can I optimize this query
This is a follow-on from a previous question, in the implementation, I have two
this is a follow up question of my previous question So I have this
First of all, this is a follow up to a previous question of mine
This is a follow up to a previous question which seems to have confused
As a follow up to this previous question , I have a Core Data-based
This is a follow-up to my previous question: Optimisation of an oracle query I
This is a follow up of the Previous Question It got really complicated so
This is a follow up to a previous question . if I have multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.