我一直在尝试查询一些信息,但我似乎无法提出查询。以下是表格信息和我当前的查询。
公司表:
+-----------+-----------+------------+
| CompanyID | Address | PostalCode |
+-----------+-----------+------------+
| a-coy | 31 street | 111111 |
| b-coy | 32 street | 111222 |
+-----------+-----------+------------+
维修表:
+----------+-----------+----------------+------------+----------+
| SerialNo | CompanyID | PersonInCharge | RepairDate | RepairBy |
+----------+-----------+----------------+------------+----------+
| 15000 | a-coy | John | 01-May-17 | Jason |
| 15001 | a-coy | John | 02-May-17 | Jack |
+----------+-----------+----------------+------------+----------+
我的目的是查询只有 1 个结果的最新维修工程条目。
我目前的查询如下:
SELECT
c.companyid, c.address, r.repairdate, r.repairby
FROM
company c
INNER JOIN
repair r ON c.companyID = r.companyID
WHERE
c.companyid = 'a-coy'
我得到以下结果:
+-----------+-----------+------------+----------+
| CompanyID | Address | RepairDate | RepairBy |
+-----------+-----------+------------+----------+
| a-coy | 31 street | 01-May-17 | Jason |
| a-coy | 31 street | 02-May-17 | Jack |
+-----------+-----------+------------+----------+
但我想要的结果只是具有最新修复日期的条目,它看起来完全像这样:
+-----------+-----------+------------+----------+
| CompanyID | Address | RepairDate | RepairBy |
+-----------+-----------+------------+----------+
| a-coy | 31 street | 02-May-17 | Jack |
+-----------+-----------+------------+----------+