将3个表的结果与MAX(date)合并

时间:2019-02-11 22:00:58

标签: php mysql

我试图使用sql join合并3个表,同时使用MAX(date)从2个表中提取最新记录。

我已经使用INNER JOINS和LEFT JOINS尝试了几种不同的查询,但还没有得出预期的结果

SELECT clients.name, clients.company, latest_jobs.contractor, latest_jobs.job_type, latest_jobs.last_date, sales_calls.last_sales_date, sales_calls.client
FROM
 (SELECT
   contractor,
   MAX(start_date) AS last_date,
   job_type,
  FROM jobs
  GROUP BY contractor) AS latest_jobs
INNER JOIN clients ON latest_jobs.contractor = clients.name
(SELECT
   MAX(sales_date) AS last_sales_date,
   client,
  FROM sales_calls 
  GROUP BY client) AS latest_jobs
 LEFT JOIN sales_calls ON sales_calls.client = clients.name
ORDER BY clients.name

客户表

|-------------------| 
| name              | 
|-------------------|
| client 1          |        
|-------------------|
| client 2          |   
|-------------------|
| client 3          |         
|-------------------|

工作表

|-------------------|---------|-------------| 
| job_type          | company | start_date  |
|-------------------|---------|-------------|
| job_type 1        |client 1 | 2019-02-01  |
|-------------------|---------|-------------|
| job_type 3        |client 2 | 2019-02-02  |
|-------------------|---------|-------------|
| job_type 2        |client 3 | 2019-02-03  |
|-------------------|---------|-------------| 
| job_type 4        |client 2 | 2019-02-05  |
|-------------------|---------|-------------|
| job_type 2        |client 1 | 2019-01-18  |
|-------------------|---------|-------------|
| job_type 3        |client 2 | 2019-01-15  |
|-------------------|---------|-------------|
| job_type 2        |client 3 | 2019-02-08  |
|-------------------|---------|-------------|

销售表

|---------|-------------| 
| company | sales_date  |
|---------|-------------|
|client 1 | 2019-02-01  |
|---------|-------------|
|client 2 | 2019-02-02  |
|---------|-------------|
|client 3 | 2019-02-03  |
|---------|-------------| 
| client 2 | 2019-02-05 |
|---------|-------------|
|client 1 | 2019-01-18  |
|---------|-------------|
|client 2 | 2019-01-15  |
|---------|-------------|
|client 3 | 2019-02-08  |
|-----------------------|

预期结果

|----------|-----------------|-----------------|-----------------|
| Name     |  last_job_date  | last_job_type   | last_sales_date |
|----------|-----------------|-----------------|-----------------|
| client 1 |  2019-02-01     | job_type 1      |  2019-02-01     |
|----------|-----------------|-----------------|-----------------|
| client 2 |  2019-02-05     | job_type 4      |  2019-02-05     |
|----------|-----------------|-----------------|-----------------|
| client 3 |  2019-02-02     | job_type 3      |  2019-02-08     |
|----------|-----------------|-----------------|-----------------|

2 个答案:

答案 0 :(得分:0)

您可以使用此查询,JOINclient到最新的工作日期和最新的销售日期(使用派生表生成),也可以jobsjob_type表中以获取{ {1}}完成上一份工作:

SELECT c.name, 
       lj.last_job_date,
       j.job_type AS last_job_type,  
       ls.last_sales_date
FROM client c
JOIN jobs j ON j.company = c.name
JOIN (SELECT company, MAX(start_date) AS last_job_date
      FROM jobs
      GROUP BY company) lj ON lj.last_job_date = j.start_date AND lj.company = c.name
JOIN (SELECT company, MAX(sales_date) AS last_sales_date
      FROM sales
      GROUP BY company) ls ON ls.company = c.name

输出(请注意,根据您的样本数据,您预期的结果不正确)

name        last_job_date   last_job_type   last_sales_date
client 1    2019-02-01      job_type 1      2019-02-01
client 2    2019-02-05      job_type 4      2019-02-05
client 3    2019-02-08      job_type 2      2019-02-08

Demo on dbfiddle

答案 1 :(得分:0)

规格完全不清楚。我们在客户表中看到对列namecompany的引用,示例查询引用了一个未定义的名为contracor的列。因此,我们对表之间的关系的理解有些混乱。

所以我们只是猜测。但这是对我们要实现的目标的猜测。

SELECT c.name
     , c.company
     , j.contractor
     , j.job_type
     , j.start_date       AS last_job_date
     , s.last_sales_date
     , s.client
  FROM clients c

  LEFT
  JOIN ( SELECT ls.client
              , MAX(ls.sales_date) AS last_sales_date
           FROM sales_calls ls
          GROUP
             BY ls.client
       ) s
    ON s.client = c.name

  LEFT
  JOIN ( SELECT llj.company
              , MAX(llj.start_date) AS last_start_date
           FROM jobs llj
          GROUP
             BY llj.company
       ) lj
    ON lj.company = c.company

  LEFT
  JOIN jobs j
    ON j.company    = lj.company
   AND j.start_date = lj.last_start_date

 ORDER
    BY c.name

我们从clients获取所有行,并按name对其进行排序。

我们使用内联视图获取每个客户的最新sales_date,然后将结果(作为外部联接)连接到clients表中的行。

类似地,我们以内联视图的形式从每个公司的工作表中获取最新的开始日期。该结果将加入到clients表的行中。

我们还重新连接到jobs表,以从具有最新start_date的行的列中获取值。 (如果同一公司和start_date有多个行,则有可能返回多个行,并且有一些方法可以解决。现在,我们的查询假设(company,start_date)元组是唯一的