Home > Software design >  How to count an inner join in MySQL
How to count an inner join in MySQL

Time:09-03

How could I count an inner join output, thanks a lot

-- Quantity A  = 981
SELECT COUNT(DISTINCT ID) FROM A;

-- Quantity B = 673
SELECT COUNT(DISTINCT ID) FROM B;

How can i count an inner join

SELECT * FROM A
    INNER JOIN B
    ON A.ID = B.ID;

CodePudding user response:

Combine your two attempts into one since you're performing an INNER JOIN, it does not matter if you use A.ID or B.ID in the DISTINCT COUNT:

SELECT COUNT(DISTINCT A.ID) AS AB_Count FROM A INNER JOIN B ON A.ID = B.ID;

Fiddle for reference.

  • Related