从连接表中获取结果

Get result from joined tables

我有 2 个表:

Table 1:

| jobid | jobname |
|     1 | job a   |
|     2 | job b   |

Table 2:

| id | jobid | statusid | statusdate          | desc   |
|  1 |     1 |      100 | 2019.04.25 10:00:00 | first  |
|  2 |     2 |      100 | 2019.04.25 11:00:00 | first  |
|  3 |     2 |      100 | 2019.04.25 12:00:00 | second |

表 2 中的作业可以有多个相同 "statusid",但不同 "statusdate" 和 "desc"

我需要像这样获取最后一个 "statusid" = 100 的职位列表:

| 1 | job a | 1 | 1 | 100 | 2019.04.25 10:00:00 | first  |
| 2 | job b | 3 | 2 | 100 | 2019.04.25 12:00:00 | second |
SELECT * FROM table1
INNER JOIN table2 ON table1.id = table2.jobid
GROUP BY table1.id

此查询 return 错误结果如:

| 1 | job a | 1 | 1 |   | 100 | 2019.04.25 10:00:00 | first |
| 2 | job b | 3 | 2 | 2 | 100 | 2019.04.25 11:00:00 | first |

您应该可以通过执行以下操作来实现:

Table

drop table if exists table1;
create table table1 (jobid int, jobname char(10));
insert into table1 values (1, 'job a'), (2, 'job b');

drop table if exists table2;
create table table2 (
    id int,
    jobid int,
    statusid int,
    statusdate timestamp,
    `desc` char(10)
);
insert into table2 values
(1,1,100,'2019.04.25 10:00:00','first')
,(2,2,100,'2019.04.25 11:00:00','first')
,(3,2,100,'2019.04.25 12:00:00','second');

查询

select
    t1.*,
    t2.*
from table1 t1
inner join (
    select jobid, max(statusdate) as maxstatusdate
    from table2
    group by jobid
) tn on t1.jobid = tn.jobid
inner join table2 t2 on tn.jobid = t2.jobid and tn.maxstatusdate = t2.statusdate;

结果

jobid   jobname id  jobid   statusid    statusdate          desc
1       job a   1   1       100         25.04.2019 10:00:00 first
2       job b   3   2       100         25.04.2019 12:00:00 second

说明

  • 对于每个工作 ID,找到最大状态日期
  • 将其加入 table1 以从 table1 获取信息。公共字段是 jobid
  • 将他们的结果加入到表 2 中,其中包含您想要的所有剩余信息。公共字段是 jobid 和 statusdate。由于我们将 max status date 别名为不同的名称,请确保我们在 join
  • 中使用正确的名称

示例:https://rextester.com/HRSWZ89705

SELECT
  t1.*,t2.* FROM
  (SELECT
    MAX(id) as id
  FROM
    table2
  WHERE statusid = 100
  GROUP BY jobid) AS f
  JOIN table2 t2
    ON t2.id = f.id
  JOIN table1 t1
    ON t2.jobid = t1.jobid

子查询select找到statusid为100的行的最后一个id,然后根据这个id加入实际的table。

您可以根据需要使用正确的连接重新排序。

DROP TABLE IF EXISTS table1;

CREATE TABLE table1
(jobid INT NOT NULL PRIMARY KEY
,jobname VARCHAR(12) UNIQUE
);

INSERT INTO table1 VALUES
(1,'job a'),
(2,'job b'),
(3,'job c');

DROP TABLE IF EXISTS table2;

CREATE TABLE table2
(id SERIAL PRIMARY KEY
,jobid INT NOT NULL
,statusid INT NOT NULL
,statusdate DATETIME NOT NULL
,description VARCHAR(12) NOT NULL
);

INSERT INTO table2 VALUES
(1,1,100,'2019-04-25 10:00:00','first'),
(2,2,100,'2019-04-25 11:00:00','first'),
(3,2,100,'2019-04-25 12:00:00','second');

SELECT a.*
     , b.id x_id
     , b.statusid
     , b.statusdate
     , b.description 
  FROM table1 a 
  LEFT 
  JOIN 
     ( SELECT x.* 
         FROM table2 x
         JOIN 
            ( SELECT MAX(id) id 
                FROM table2 
               WHERE statusid = 100 
               GROUP 
                  BY jobid
            ) y 
           ON y.id = x.id
     ) b
    ON b.jobid = a.jobid 
;


+-------+---------+------+----------+---------------------+-------------+
| jobid | jobname | x_id | statusid | statusdate          | description |
+-------+---------+------+----------+---------------------+-------------+
|     1 | job a   |    1 |      100 | 2019-04-25 10:00:00 | first       |
|     2 | job b   |    3 |      100 | 2019-04-25 12:00:00 | second      |
|     3 | job c   | NULL |     NULL | NULL                | NULL        |
+-------+---------+------+----------+---------------------+-------------+