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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:40:02+00:00 2026-05-12T07:40:02+00:00

I have the these tables: – Users – id – Photos – id –

  • 0

I have the these tables:

- Users
    - id
- Photos
    - id
    - user_id
- Classifications
    - id
    - user_id
    - photo_id

I would like to order Users by the total number of Photos + Classifications which they own.

I wrote this query:

SELECT users.id, 
COUNT(photos.id) AS n_photo, 
COUNT(classifications.id) AS n_classifications, 
(COUNT(photos.id) + COUNT(classifications.id)) AS n_sum 
FROM users 
LEFT JOIN photos ON (photos.user_id = users.id) 
LEFT JOIN classifications ON (classifications.user_id = users.id) 
GROUP BY users.id 
ORDER BY (COUNT(photos.id) + COUNT(classifications.id)) DESC

The problem is that this query does not work as I expect and returns high numbers while I have only a few photos and classifications in the db. It returns something like this:

id n_photo n_classifications   n_sum
29  19241   19241                   38482
16  16905   16905                   33810
1    431     0                       431
...
  • 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-12T07:40:02+00:00Added an answer on May 12, 2026 at 7:40 am

    You are missing distinct.

      SELECT U.ID, COUNT(DISTINCT P.Id)+COUNT(DISTINCT C.Id) Count
      FROM User U
      LEFT JOIN Photos P ON P.User_Id=U.Id
      LEFT JOIN Classifications C ON C.User_Id=U.Id
      GROUP BY U.Id
      ORDER BY COUNT(DISTINCT P.Id)+COUNT(DISTINCT C.ID)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these two tables tickets - id, user_id, manager_id etc users - id,
I have these 3 tables: users table(PK user_id) Fields: user_id, user_first_name, user_last_name, username, user_email...etc
I have these tables: customer -------- customer_id int name varchar(255) order ----- order_id int
i have these tables /entities VacationRequestDate table which has a VacationRequestId field that links
I have these tables: Projects(projectID, CreatedByID) Employees(empID,depID) Departments(depID,OfficeID) Offices(officeID) CreatedByID is a foreign key
I have these 3 tables + data: items : itemId, itemName data: 1, my
Does anyone have any suggestions for creating meta tables in a database? These tables
I have tree tables, Customer, Invoice and InvoiceRow with the standard relations. These I
Say that I have two tables like those: Employers (id, name, .... , deptId).
Suppose I have a dataset with those two immortal tables: Employee & Order Emp

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.