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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:44:18+00:00 2026-06-05T15:44:18+00:00

So I am trying to write a single MySQL query to replace what is

  • 0

So I am trying to write a single MySQL query to replace what is currently being done in PHP with three separate queries. I have a table for Categories with only two values: an ID and a Name. There are two other tables which each have a foreign key referring to the ID from the first table. I need to get a list of all the values from the Categories table as well as a count of how many times each value is referenced in each of the two other tables. I can get the count accurately from each of the second tables using a separate query for each:

SELECT nga_calevir_event_categories.*, COUNT(nga_calevir_events.event_id) AS event_total
FROM nga_calevir_event_categories
LEFT JOIN (nga_calevir_events)
ON nga_calevir_event_categories.id = nga_calevir_events.event_category
GROUP BY nga_calevir_event_categories.id;

and

SELECT nga_calevir_event_categories.*, COUNT(nga_usermeta.user_id) AS member_total
FROM nga_calevir_event_categories
LEFT JOIN (nga_usermeta)
ON (nga_usermeta.meta_value = nga_calevir_event_categories.id
AND nga_usermeta.meta_key = 'category_id')
GROUP BY nga_calevir_event_categories.id;

However, when I try to combine these queries it starts giving incorrect results for both counts. Here is the combined query:

SELECT nga_calevir_event_categories.*,
COUNT(nga_calevir_events.event_id) AS event_total,
COUNT(nga_usermeta.user_id) AS member_total
FROM nga_calevir_event_categories
LEFT JOIN (nga_calevir_events)
ON nga_calevir_event_categories.id = nga_calevir_events.event_category
LEFT JOIN (nga_usermeta)
ON (nga_usermeta.meta_value = nga_calevir_event_categories.id
AND nga_usermeta.meta_key = 'category_id')
GROUP BY nga_calevir_event_categories.id;

It seems to be doubling the count for the first row. I’ve been experimenting with this for a couple hours now and I just can’t figure it out. Any ideas? Let me know if you need more information to help me with this.

  • 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-05T15:44:19+00:00Added an answer on June 5, 2026 at 3:44 pm

    This doesn’t work because joins are combinatorial:

    SELECT nga_calevir_event_categories.*,
    COUNT(nga_calevir_events.event_id) AS event_total,
    COUNT(nga_usermeta.user_id) AS member_total
    
    
    FROM nga_calevir_event_categories
    LEFT JOIN (nga_calevir_events)
    ON nga_calevir_event_categories.id = nga_calevir_events.event_category
    -- The above is almost the same as an inner join, so long as every 
    -- category has at least one event.
    
    LEFT JOIN (nga_usermeta)
    ON (nga_usermeta.meta_value = nga_calevir_event_categories.id
    AND nga_usermeta.meta_key = 'category_id')
    -- This is where you're going to get weird.  This is going to join 
    -- every row already figured out- basically, a row for every event
    -- with every usermeta that matches!
    
    
    GROUP BY nga_calevir_event_categories.id;
    

    This really is two queries- there’s no sane way to join usermeta to categories (unless I misunderstand the application). There is a way to get it into one SQL statement, though. Try this:

    SELECT categories.*,
           coalesce(event_hits.hits, 0) + coalesce(meta_hits.hits, 0) as total_hits
    FROM nga_calevir_event_categories as categories       
    
    LEFT JOIN
    (select event_category as catid, count(*) as hits
        FROM (nga_calevir_events)
        group by event_category
    ) as event_hits ON event_hits.catid = categories.id
    
    LEFT JOIN (
       select meta_value as catid, count(*) as hits
       FROM nga_usermeta
       WHERE meta_key = 'category_id'
       group by meta_value
    ) as meta_hits ON meta_hits.catid = categories.id
    

    This uses two inner selects to get a count of category ids used for each of the two tables, then joins those inner selects to the actual categories, and sums the counts they each returned. We use COALESCE to make sure that where there were no uses in one of the tables, 0 is assumed.

    Hope this helps!

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

Sidebar

Related Questions

I am trying to write a select query from one single table in SQLite
I'm trying to write a single query which will include one of the two
Im trying to write a single activity app, extending ListActivity. I have a class
I'm trying to write a single Linq Query that can concat inner collections of
I'm trying to write a query that joins a handful of tables from MySQL
I'm trying to write a regex function that will identify and replace a single
I've been trying to figure out how to reduce these php-mysql queries to a
Im trying to write a single byte at a certain location in a file.
Im trying to write something to get my images to show correctly. I have
I am trying to write a single character in a text file. I do:

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.