I have 2 tables on 2 different databases :
db1.table1
+--------------+--------------------------+
| Username | Message |
+--------------+--------------------------+
| jamesbond | I need some help |
| jamesbond | I need some help |
| jamesbond | I need some help |
| jamesbond | Mission accomplished |
+--------------+--------------------------+
db2.table2
+--------------------------+--------------+
| Message | Status |
+--------------------------+--------------+
| I need some help | Ok |
| I need some help | Ok |
| I need some help | Bad |
+--------------------------+--------------+
when I do ‘INNER JOIN’ those tables using this SQL syntax :
SELECT A.Username, A.Message
SUM(CASE WHEN `status` = 'Ok' THEN 1 ELSE 0 END) AS StatOK,
SUM(CASE WHEN `status` = 'Bad' THEN 1 ELSE 0 END) AS StatBAD
FROM db1.table1 as A
INNER JOIN db2.table2 as B
ON A.Message = B.Message
WHERE A.Username = 'jamesbond'
GROUP BY A.Username, A.Message
I got this result :
+--------------+--------------------------+--------+---------+
| Username | Message | StatOK | StatBAD |
+--------------+--------------------------+--------+---------+
| jamesbond | I need some help | 2 | 1 |
+--------------+--------------------------+--------+---------+
how to get result like this (message without status on DB2 still appear, but the SUM result can be ZERO or NULL) :
+--------------+--------------------------+--------+---------+
| Username | Message | StatOK | StatBAD |
+--------------+--------------------------+--------+---------+
| jamesbond | I need some help | 2 | 1 |
| jamesbond | Mission accomplished | NULL | NULL |
+--------------+--------------------------+--------+---------+
You need a
Left Outer Join, It will get the rows from Left tables and if it doesn’t match with Right table then You’ll get NULL.