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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:00:25+00:00 2026-05-28T20:00:25+00:00

Previous question linky: MySQL Left Joins Very kindly we got the following code working:

  • 0

Previous question linky: MySQL Left Joins

Very kindly we got the following code working:

select
      JustACME.Name,
      JustACME.NameCount,
      COALESCE( COUNT( * ), 0 ) as CountFromResultsTable
   from
      ( select a.Name,
               count(*) as NameCount
           from
              acme a
           group by
              a.Name ) JustACME

      LEFT JOIN results r
         on JustACME.Name = r.Name
   group by
      JustACME.Name

The above code works exactly as we need and brings back the following

Name     Count     Total
Tom      10        5
Bon      9         4

In addition to that information being in the acme and results table, we also need to bring back further information. The ‘results’ table is as follows:

Name     q1
Tom      1
Tom      2
Tom      1
Bob      3
Bob      2
Bob      1
Bob      2

I’d like to add to this query, in the talble called results there is another field called q1. This can have a number in it, either 1, 2, or 3 – what I’d like to do is bring back the number of times that 1,2 or 3 appears (count) for each of the Name that also appear in the table. Make sense?

Basically, the output of the query should be something like this:

Name     Count     Total     q1 = 1    q1 = 2     q1 = 3
Bob      9         4         1         2          1
Tom      10        5         4         1          0

I’m probably going out on a limb here but can it be done???

Thanks in advance,

H.

  • 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-28T20:00:27+00:00Added an answer on May 28, 2026 at 8:00 pm

    To get just the Name, q1=1, q1=2, q1=3 table from results only you can use:

    SELECT Name,
           COUNT(IF(q1=1,1,NULL)) as q1is1, 
           COUNT(IF(q1=2,1,NULL)) as q1is2,
           COUNT(IF(q1=3,1,NULL)) as q1is3
    FROM results
    GROUP BY Name
    

    This counts up the number of occurences of each result (per Name) — this works because you only want three extra columns and know inadvance which values of q1 you want to have a columns. (If there were say half a million q1 values there’s no way to do this in MySQL other than writing out the COUNT(IF(q1=i,1,0)) AS q1isi half a million times — not exactly ideal).

    To combine with your previous query, perhaps something like this (add it in as a JOIN):

    select
      JustACME.Name,
      JustACME.NameCount,
      COALESCE( COUNT( * ), 0 ) as CountFromResultsTable,
      r2.q1is1, r2.q1is2, r2.q1is3                     -- <-- grab those extra rows
    from
      ( select a.Name,
               count(*) as NameCount
           from
              acme a
           group by
              a.Name ) JustACME
    
      LEFT JOIN results r
         on JustACME.Name = r.Name
      LEFT JOIN (SELECT Name,                          -- <-- add the new table in
                     COUNT(IF(q1=1,1,NULL)) as q1is1, 
                     COUNT(IF(q1=2,1,NULL)) as q1is2,
                     COUNT(IF(q1=3,1,NULL)) as q1is3
                 FROM results
                 GROUP BY Name) r2
         ON JustACME.Name = r2.Name
    group by
      JustACME.Name
    

    In fact, I’m sure there’s a way to do this without needing to LEFT JOIN on a sub-query, but my mysql kung fu has run out 😛

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

Sidebar

Related Questions

Following on from my previous question [link text][1] , I have a new problem.
Following on from a previous question , I am creating a symbolic link on
This question is related to a previous question of mine That's my current code
Here's a link to my previous question on this same block of code with
Following my previous question regarding the rationale behind extremely long functions, I would like
This code is taken from a previous question, but my question directly relates to
Following on from a previous question, ( previous question here ), the problem I'm
In my previous question I got an excellent answer that helped me detect where
In a previous question , I managed to get some text replacement working for
Previous question which links onto this and has any addition code ref should I

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.