John
John

Reputation: 952

Count with LEFT JOIN shows only one row

I have the following data in my database:

scu_banks:

---------------------------------
|   id    |   type   |   name   |
|-------------------------------|  
|    1    |    1     |   One    |
|    2    |    1     |   Two    |
|    3    |    2     |  Three   |
|    4    |    3     |   Four   |
---------------------------------

scu_statement:

---------------------------------
|   id    |   code   |    status    |
|-----------------------------------|  
|    1    |    1     |      0       |
|    2    |    1     |      1       |
|    3    |    2     |      0       |
|    4    |    1     |      0       |
-------------------------------------

What I want to do is I want to select all the rows in table scu_banks and calculate how many rows I have with the status 0. The data should be represented like:

--------------------------------------------------------------
| scu_banks.type | scu_banks.name |   status  | scu_banks.id |
--------------------------------------------------------------
|       1        |      One       |     2     |      1       |
|       1        |      Two       |     0     |      2       | //There is no row with status 0
|       2        |     Three      |     0     |      3       |
|       3        |      Four      |     0     |      4       |
--------------------------------------------------------------

When I run my sql statement I get the following data:

---------------------------------------------------------------
| scu_banks.type | scu_banks.name |    status   | scu_banks.id |
--------------------------------------------------------------
|       1        |      One       |      2      |      1       |
---------------------------------------------------------------

The data I get in this case is correct. 2 it the total count of all the rows in table scu_statement. The statement also dont shows the other rows in the database.

Does someone know what is wrong with my sql statement?

Here is my sql statement:

SELECT b.type 'scu_banks.type',
b.name 'scu_banks.name',
count(y.status) 'status',
b.id 'scu_banks.id'
FROM scu_banks b
LEFT JOIN (SELECT s.code, count(s.status) status
           FROM scu_bankstatement s 
           WHERE status='0'
           GROUP BY s.code) y
           ON y.code = b.id

Upvotes: 1

Views: 1085

Answers (1)

Nick
Nick

Reputation: 147196

You need a GROUP BY in your outer query, otherwise the query simply counts status for all banks. You can also simplify your query by just LEFT JOINing the two tables on code/id and status = 0

SELECT b.type `scu_banks.type`,
b.name `scu_banks.name`,
COUNT(s.status) `status`,
b.id `scu_banks.id`
FROM scu_banks b
LEFT JOIN scu_statement s ON s.code = b.id AND s.status = 0
GROUP BY b.id, b.name, b.type

Output

scu_banks.type  scu_banks.name  status  scu_banks.id
1               One             2       1
1               Two             1       2
2               Three           0       3
3               Four            0       4

Demo on dbfiddle

Upvotes: 3

Related Questions