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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:37:15+00:00 2026-05-30T19:37:15+00:00

I’m trying to use the SUM function to count rows from 3 tables, which

  • 0

I’m trying to use the SUM function to count rows from 3 tables, which is however, not working effectively since when the total_files and total_notes are returned, they both are the same when there is at least one file and then total_files will take the same value as total_notes which I don’t understand why it’s doing that.

It should count the number of rows which is relevant to each record that will get return as a record list with a count of total files, total notes and total contacts assigned to the record per record row (the data of files, notes and contacts do not get displayed only counted).

My query is shown below:

SELECT rec.street_number,
       rec.street_name,
       rec.city,
       rec.state,
       rec.country,
       rec.latitude,
       rec.longitude,
       LEFT(rec.description, 250) AS description,
       usr.username,
       usr.full_name,
       ppl.person_id,
       ppl.first_name,
       ppl.last_name,
       SUM(IF(rlk.record_id = rec.record_id, 1, 0)) AS total_contacts,
       SUM(IF(files.record_id = rec.record_id, 1, 0)) AS total_files,
       SUM(IF(notes.record_id = rec.record_id, 1, 0)) AS total_notes,
       (
           SELECT COUNT(DISTINCT rec.record_id)
           FROM records rec
           WHERE rec.marked_delete = 0 AND rec.is_archive = 0
       ) AS total_records
FROM
(
    records rec

    INNER JOIN members usr ON rec.user_id = usr.user_id

    LEFT OUTER JOIN record_links rlk ON rec.record_id = rlk.record_id

    LEFT OUTER JOIN people ppl ON ppl.person_id = rlk.person_id AND rlk.record_id = rec.record_id

    LEFT OUTER JOIN files files ON files.record_id = rec.record_id

    LEFT OUTER JOIN notes notes ON notes.record_id = rec.record_id
)
WHERE rec.marked_delete = 0 AND rec.is_archive = 0
GROUP BY rec.record_id
ORDER BY rec.submit_date DESC
LIMIT 0, 25

Basically as you can see there is three SUM which will count relevant rows that comes from those tables, but I seriously don’t understand how total_files would be taking the same value as total_notes is there something wrong I’m doing here?

  • 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-30T19:37:16+00:00Added an answer on May 30, 2026 at 7:37 pm

    It’s because rec is joined to both notes and files.

    Suppose record 1 has 2 notes and 1 file, record 2 has two note and two files, and record 3 has a note but no files.

    Then the table rec LEFT OUTER JOIN files ... LEFT OUTER JOIN notes will look like this:

    +-----------+---------+---------+
    | record_id | file_id | note_id |
    +-----------+---------+---------+
    |         1 |       1 |       1 |
    |         1 |       2 |       1 |
    |         2 |       3 |       2 |
    |         2 |       3 |       3 |
    |         2 |       4 |       2 |
    |         2 |       4 |       3 |
    |         3 |    NULL |       4 |
    +-----------+---------+---------+
    

    Note how every file_id gets joined to every note_id (within the same record_id). Also, since you have SUM(IF(files.record_id = rec.record_id,1,0)) and the join condition is files.record_id = rec.record_id, you are actually counting COUNT(files)*COUNT(notes) per record_id.

    I’d recommend you instead COUNT(DISTINCT files.id) and COUNT(DISTINCT records.id). The column in the COUNT would be your primary key on files/notes, not files.record_id:

    SELECT rec.record_id,
           COUNT(DISTINCT files.id) AS total_files,
           COUNT(DISTINCT notes.id) AS total_notes
    FROM rec
    -- note: LEFT OUTER JOIN is the same as LEFT JOIN in MySQL
    LEFT JOIN files ON files.record_id=rec.record_id 
    LEFT JOIN notes ON notes.record_id=rec.record_id
    GROUP BY record_id
    
    
    +-----------+-------------+-------------+
    | record_id | total_files | total_notes |
    +-----------+-------------+-------------+
    |         1 |           2 |           1 |
    |         2 |           2 |           2 |
    |         3 |           0 |           1 |
    +-----------+-------------+-------------+
    

    Of course, adjust to your query as necessary (add in those extra columns/joins).

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.