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 tables: USERS: user_ID username password email data_registered POSTS: post_ID user_ID post_title
I have these tables: users , comments , ratings , and items I would
I have these tables: comments uid id pid pages pid user users id rank
I have these tables: Idea and poll (and others) which are types of what
I have these tables: table_a user_id article_id created_at articles user_id created_at ... I need
I have these tables. tables #__qa_question qid, user_id, date_add, date_modify, question, publish #__qa_answer aid,
Assume I have these tables, from which i need to display search results in
I have these tables: users ----- id INT name VARCHAR(20) email VARCHAR(40) user_fans ----------
I have these tables CREATE TABLE parent ( id NUMBER(10, 0) NOT NULL, name
I have these two tables with the same structure but with different number of

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.