I'm using MySQL and PHP. I have 2 tables.
The first one has dates in it.
This table is called dates
+-------+-------------+----------+---------+
| id | unixTime | month | year |
+------------------------------------------+
| 1 | 1443657600 | 10 | 2015 |
| | | | |
| 2 | 1443657600 | 11 | 2015 |
| | | | |
| 3 | 1443657600 | 12 | 2015 |
| | | | |
| 4 | 1443657600 | 01 | 2016 |
| | | | |
+-------+-------------+----------+---------+
The table below will show if you query SELECT * FROM analytics WHERE clientfk=36
. There's only 3 entries because client #36 stopped being a client in January.
This table is called analytics
+------------------------------------------------+
| id datefk Value clientfk |
+------------------------------------------------+
| 156 1 "some value" 36 |
| |
| 157 2 "another value" 36 |
| |
| 157 3 "thrid value" 36 |
| |
+------------------------------------------------+
I want to join(?) union(?) these tables and get an output that looks like this: (NB, the null
value)
+------------------------------------------------+
| month year client value |
+------------------------------------------------+
| 10 2015 36 "Some value" |
| |
| 11 2015 36 "Another value" |
| |
| 12 2015 36 "third value" |
| |
| 1 2016 36 NULL |
+------------------------------------------------+
I tried the query below, among a myriad of other things but always either returns nothing or just 3 rows
SELECT *
FROM analytics a
WHERE a.clientfk = 36
AND NOT EXISTS (SELECT null FROM dates d
WHERE d.id = a.datefk)