简体   繁体   中英

MySQL Left Joins

EDIT: OK, think I need to be clearer - I'd like the result to show all the 'names' that appear in the table acme, against the counts (if any) from the results table. Hope that makes sense?

Having a huge issue and my brain isn't working as it should.

All I want to do is, in a single statement via a join, count the number of rows for a common field.

   SELECT name, COUNT(name) as Count FROM acme
   SELECT name, COUNT(name) as Total FROM results

I'm sure it should be something like this...

   SELECT acme.name, COUNT(acme.name) As Count, 
          COUNT(results.name) as Total 
   FROM acme 
   LEFT JOIN results ON acme.name = results.name 
   GROUP BY name 
   ORDERY BY name

But it doesn't bring back the correct counts.

Thoughts, where am I going wrong...this, I know, will be very very obvious.

H.

From your feedback, this will get what you want. You need to FIRST get unique names / counts from the "ACME" file first... THEN join that to the results table for count of records from that, otherwise, you would end up with a Cartesian result of counts. If ACME had Name "X" 5 times and Results had "X" 20 times, your total would be 100. The query below will actually result with a single row showing "X", 5, 20 which is what it appears you are looking for.. (for however many names exist in ACME).

I've changed to a LEFT join in case there are names in the ACME table that DO NOT exist in the RESULTS table, it won't drop them from your final answer

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

It looks like it's because of the join, it's screwing with your counts. Try running the join with SELECT * FROM... and look at the resulting table. The problem should be obvious from there. =D

Yes, your join (inner or outer, doesn't matter) is messing with your results.

In fact, it is likely returning the product of rows with the same name, rather than the sum.

What you want to do is sum the rows from the first table, sum the rows from the second table, and join that .

Like this:

Select name, a.count as Count, r.count as Total
From (select name, count(*) from acme group by name) a
Left join (select name, count(*) from results group by name) r using (name)

I do not see why you forbid using two statements this just complicates everything. The only reason I see for this is to get the two results into one answer. I do not know if the latter would work but I would try this:

SET @acount = (SELECT count(DISTINCT name) FROM acme);
SELECT count(DISTINCT name) as Total, @acount as Count FROM results

I would post this as one query and (hopefully) get back the correct results. Let me note, that it is not clear from you question if you want to know how often every name doubles or if you want to count unique names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM