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 8679645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:01:32+00:00 2026-06-12T21:01:32+00:00

Here is what I’m trying to do. I have a table with user assessments

  • 0

Here is what I’m trying to do. I have a table with user assessments which may contain duplicate rows. I’m looking to only get DISTINCT values for each user.

In the example of the table below. If only user_id 1 and 50 belongs to the specific location, then only the unique video_id’s for each user should be returned as the COUNT. User 1 passed video 1, 2, and 1. So that should only be 2 records, and user 50 passed video 2. So the total for this location would be 3. I think I need to have two DISTINCT's in the query, but am not sure how to do this.

+-----+----------+----------+
| id  | video_id | user_id  |
+-----+----------+----------+
| 1   |       1  |        1 |
| 2   |       2  |       50 |
| 3   |       1  |      115 |
| 4   |       2  |       25 |
| 5   |       2  |        1 |
| 6   |       6  |       98 |
| 7   |       1  |        1 |
+-----+----------+----------+

This is what my current query looks like.

$stmt2 = $dbConn->prepare("SELECT COUNT(DISTINCT user_assessment.id)
FROM user_assessment
LEFT JOIN user ON user_assessment.user_id = user.id
WHERE user.location = '$location'");
$stmt2->execute();
$stmt2->bind_result($video_count);
$stmt2->fetch();
$stmt2->close();

So my query returns all of the count for that specific location, but it doesn’t omit the non-unique results from each specific user.

Hope this makes sense, thanks for the help.

  • 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-06-12T21:01:33+00:00Added an answer on June 12, 2026 at 9:01 pm

    The query below joins a sub-query that fetches the distinct videos per user. Then, the main query does a sum on those numbers to get the total of videos for the location.

    SELECT 
      SUM(video_count)
    FROM 
      user u
      INNER JOIN 
      ( SELECT
          ua.user_id,
          COUNT(DISTINCT video_id) as video_count
        FROM
          user_assessment ua
        GROUP BY
          ua.user_id) uav on uav.user_id = u.user_id
    WHERE 
      u.location = '$location'
    

    Note, that since you already use bindings, you can also pass $location in a bind parameter. I leave this to you, since it’s not part of the question. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here are the tables I have: Table A which has entries with item and
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is the problem that I am trying to solve. I have two folders
Here's the code I have. It works. The only problem is that the first
Here is what I am trying to achieve in PHP: I have this string:
Here's what I'm trying to do: When there's a new INSERT into the table
Here is the problem. I have <div id=main></div> I want to check user resolution
Here is the code in a function I'm trying to revise. This example works
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id

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.