MySQL LEFT使用第二个表中的max和other参数连接两个表

时间:2014-07-07 19:00:31

标签: mysql left-join

拥有这些表并需要加入它们以创建最终列表。

accounts
-----------------------------------------------------
| COMPANY_ID | COMPANY         | ACCOUNT_OWNER      |
-----------------------------------------------------
| 1          | bmw             | 100                |
-----------------------------------------------------
| 2          | audi            | 100                |
-----------------------------------------------------
| 3          | mercedes        | 100                |
-----------------------------------------------------
| 4          | porsche         | 200                |
-----------------------------------------------------

users
--------------------------------
| USER_ID    | NAME            |
--------------------------------
| 100        | bob             |
--------------------------------
| 200        | sally           |
--------------------------------
| 300        | mark            |
--------------------------------

log_type
--------------------------------
| TYPE_ID    | TYPE            |
--------------------------------
| 1          | sales           |
--------------------------------
| 2          | follow up       |
--------------------------------
| 3          | marketing       |
--------------------------------


logs (logs for each account)
---------------------------------------------------------------------------
| COMPANY_ID | TYPE            |  USER     | DATE          | NOTE         |
---------------------------------------------------------------------------
| 1          | 2               |  200      | 2014-06-05    | 123          |
---------------------------------------------------------------------------
| 1          | 2               |  100      | 2014-06-03    | ABC          |
---------------------------------------------------------------------------
| 1          | 2               |  100      | 2014-06-01    | 789          |
---------------------------------------------------------------------------
| 1          | 3               |  100      | 2014-06-01    | XYZ          |
---------------------------------------------------------------------------
| 2          | 3               |  100      | 2014-01-01    | Hello        |
---------------------------------------------------------------------------
| 2          | 2               |  100      | 2014-01-01    | Hello        |
---------------------------------------------------------------------------

想要这些结果 - 显示一个用户的帐户表(在本例中为bob)。 - 显示附加到帐户的最后一个日志。 (如果没有,则显示无论如何/ NULL) - 仅拉取“跟进”类型的日志((log_type.TYPE = 2) - 首先按最旧的附加日志对列表进行排序。 (logs.DATE asc)

---------------------------------------------------------------------------
| COMPANY    | TYPE            |  USER     | DATE          | NOTE         |
---------------------------------------------------------------------------
| audi       | follow up       |  bob      | 2014-01-01    | Hello        |
---------------------------------------------------------------------------
| bmw        | follow up       |  bob      | 2014-06-03    | ABC          |
---------------------------------------------------------------------------
| mercedes   | NULL            |  bob      | NULL          | NULL         |
---------------------------------------------------------------------------

目前的代码看起来像这样......

select c.COMPANY, lt.TYPE, u.NAME, l.DATE, l.NOTE from accounts c 
  LEFT JOIN users u ON c.ACCOUNT_OWNER = u.USER_ID 
  LEFT JOIN logs l ON ( (c.ID_COMPANY = l.COMPANY) && l.TYPE = '2' && u.USER_ID = 100
  LEFT JOIN log_type lt ON l.TYPE = lt.TYPE_ID 
  WHERE u.USER_ID = 100 && l.TYPE = '2' 
  GROUP BY c.COMPANY_ID
  ORDER BY l.DATE asc

0 个答案:

没有答案