如果另一个表中没有匹配项,则返回NULL行

时间:2016-02-01 17:46:51

标签: php mysql join

我正在使用MySQL和PHP。我有4张桌子。

第一个有日期。该表称为日期

+-------+-------------+----------+---------+
|  id   |  unixTime   |  month   |  year   |
+------------------------------------------+
|  1    | 1443657600  |    10    |   2015  |
|       |             |          |         |
|  2    | 1443657600  |    11    |   2015  |
|       |             |          |         |
|  3    | 1443657600  |    12    |   2015  |
|       |             |          |         |
|  4    | 1443657600  |    01    |   2016  |
|       |             |          |         |
+-------+-------------+----------+---------+

如果您查询SELECT * FROM analytics WHERE clientfk=36 AND metricfk=3,下表将显示。由于客户#36在1月份停止成为客户,因此只有3个条目。

此表称为分析

+----------------------------------------------------------+
|  id       datefk          value       clientfk   metricfk|
+----------------------------------------------------------+
|  156        1          "some value"       36         3   |
|                                                          |
|  157        2        "another value"      36         3   |
|                                                          |
|  157        3         "thrid value"       36         3   |
|                                                          |
+----------------------------------------------------------+

我有另一个名为metrics的表,看起来像这样。它是一个指标列表,将用于显示所有日期analytics的所有值。最后一张表只是客户

+--------------------------------+  
|  id         metric             |
+--------------------------------+
|   1       "Some metric"        |
|                                |
|   2      "Another metric"      |
|                                |
|   3      "A third metric"      |
|                                |
|   4      "One more metric"     |
+--------------------------------+

+--------------------------------+  
|  id         clientName         |
+--------------------------------+
|   1       "Some client"        |
|                                |
|   2      "Another client"      |
|                                |
|   3      "A third client"      |
|                                |
|   4      "One more client"     |
+--------------------------------+

我需要根据指标将输出组合成这些表,但包括没有数据的行。例如,客户端#36在1月份停止成为客户端,因此我需要NULL行。像这样:

+------------------------------------------------+
|  month      year      client      value        |
+------------------------------------------------+
|   10         2015       36      "Some value"   |
|                                                |
|   11         2015       36     "Another value" |
|                                                |
|   12         2015       36      "third value"  |
|                                                |
|   1          2016       36         NULL        |<-- this row has NULL
|                                                |
|   10         2015       37      "value 1"      |
|                                                |
|   11         2015       37      "value 2"      |
|                                                |
|   12         2015       37       "value"       |
|                                                |
|   1          2016       37   "One more value"  |
+------------------------------------------------+

我尝试过很多内容,JOINLEFT JOINCOALESCEasking the wrong question,但我对MySQL的了解通常仅限于{{1} }

1 个答案:

答案 0 :(得分:1)

试试:body-params。更多信息:http://dev.mysql.com/doc/refman/5.7/en/join.html

修改 试试这个sql:

LEFT OUTER JOIN