从联接表中获取结果

时间:2019-04-25 14:54:42

标签: mysql mariadb groupwise-maximum

我有2张桌子:

表1:

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

表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中的工作可以具有多个相同的“状态”,但具有不同的“状态”和“描述”

我需要这样获得最后一个“状态” = 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

此查询返回错误的结果,例如:

| 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 |

3 个答案:

答案 0 :(得分:0)

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

表格

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
  • 将其结果加入具有所需所有剩余信息的table2中。常见字段是jobid和statusdate。由于我们将最大状态日期别名为另一个名称,因此请确保在连接中使用正确的名称

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

答案 1 :(得分:0)

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子查询查找状态ID为100的行的最后一个id,然后根据该id联接实际表。

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

答案 2 :(得分:0)

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        |
+-------+---------+------+----------+---------------------+-------------+