结合两个具有单独索引的查询

时间:2018-06-26 13:29:55

标签: mysql

我有两个查询,它们从两个不同的表中提取数据,但是我需要它们来提取相同的报告。我之间有一个共享密钥,第一个表有一个条目,该条目对应第二个表中的许多条目。

我的第一个查询:

SELECT Proposal_ID,
    substr(Proposal_Name, 1, 3) AS Prefix,
    substr(Proposal_Name, 4, 6) AS `Number`,
    Institution,
    CollegeCode,
    DepartmentCode,
    Proposer_FirstName,
    Proposer_LastName

FROM proposals.proposal
WHERE Institution = 'T';

样本数据:

+----+--------+--------+-------+----------+----------+-----------+----------+
| ID | Prefix | Number | Inst. | CollCode | DeptCode | FirstName | LastName |
+----+--------+--------+-------+----------+----------+-----------+----------+
| 18 | SYP    | 4675   | T     | AS       | SOC      | Linda     | McGaff   |
+----+--------+--------+-------+----------+----------+-----------+----------+
| 20 | GEO    | 4340   | T     | AS       | SGS      | Teddy     | Graham   |
+----+--------+--------+-------+----------+----------+-----------+----------+

第二个查询:

SELECT Parent_Proposal,
    SUBSTRING_INDEX(GROUP_CONCAT(`status`.`Status_Code` ORDER BY `status`.`Status_Time` DESC), ',', 1) AS status_code,
    SUBSTRING_INDEX(GROUP_CONCAT(`status`.`Status_Time` ORDER BY `status`.`Status_Time` DESC), ',', 1) AS status_timestamp
FROM proposals.`status`    
GROUP BY `status`.Parent_Proposal

样本数据:

+-----------------+-------------+----------------------+
| Parent_Proposal | Status_Code | Status_Time          |
+-----------------+-------------+----------------------+
| 18              | 40          | 2016-11-09 06:30:35  |
+-----------------+-------------+----------------------+
| 20              | 11          | 2017-03-20 10:26:31  |
+-----------------+-------------+----------------------+

我基本上需要基于Status_Timestamp提取最新的Status_Code和Status_Timestamp,然后通过Parent_Proposal列将其与第一个表相关联。

有没有一种方法可以将结果子集分组而不将所有数据分组在一起?

预期结果:

+----+--------+--------+-------+----------+----------+-------+--------+-------------+----------------------+
| ID | Prefix | Number | Inst. | CollCode | DeptCode | FName | LName  | Status_Code | Status_Time          |
+----+--------+--------+-------+----------+----------+-------+--------+-------------+----------------------+
| 18 | SYP    | 4675   | T     | AS       | SOC      | Linda | McGaff | 40          | 2016-11-09 06:30:35  |
+----+--------+--------+-------+----------+----------+-------+--------+-------------+----------------------+
| 20 | 11     | GEO    | 4340  | AS       | SGS      | Teddy | Graham | 11          | 2017-03-20 10:26:31  |
+----+--------+--------+-------+----------+----------+-------+--------+-------------+----------------------+

感谢您的帮助和见解!

1 个答案:

答案 0 :(得分:1)

我想你想要这个。只需将两个表连接在一起,然后再对status表上的子查询进行附加连接,以查找每个父投标的最新记录。

SELECT
    p.Proposal_ID,
    SUBSTR(p.Proposal_Name, 1, 3) AS Prefix,
    SUBSTR(p.Proposal_Name, 4, 6) AS Number,
    p.Institution,
    p.CollegeCode,
    p.DepartmentCode,
    p.Proposer_FirstName,
    p.Proposer_LastName,
    s1.Status_Code,
    s1.Status_Time 
FROM proposals.proposal p
LEFT JOIN proposals.status s1
    ON p.ID = s1.Parent_Proposal
INNER JOIN
(
    SELECT Parent_Proposal, MAX(Status_Time) AS Max_Status_Time
    FROM proposals.status
    GROUP BY Parent_Proposal
) s2
    ON s1.Parent_Proposal = s2.Parent_Proposal AND s1.Status_Time = s2.Max_Status_Time
WHERE
    p.Institution = 'T';