Html 选择2个表,但我想限制第二个表上的行
现在我正在尝试学习Mysql,但是我被这个问题困扰了,我有两个表 台式设备Html 选择2个表,但我想限制第二个表上的行,html,mysql,Html,Mysql,现在我正在尝试学习Mysql,但是我被这个问题困扰了,我有两个表 台式设备 id location machine 1 32 Roam 2 33 Polisher 3 34 Vacuum 表维护 id maintenance date entry 1 07-27-2009 1 1 07-27-2009 2 2 08-22-2015
id location machine
1 32 Roam
2 33 Polisher
3 34 Vacuum
表维护
id maintenance date entry
1 07-27-2009 1
1 07-27-2009 2
2 08-22-2015 3
2 08-22-2017 4
3 05-25-2016 5
3 05-25-2018 6
我想选择带有最近维护日期的表1,但其中一些表的日期相同,因此它将基于条目号,假设最近条目是最新维护日期
id location machine maintenance date entry
1 32 Roam 07-28-2009 2
2 33 Polisher 08-22-2017 4
3 34 Vacuum 05-25-2018 6
我做了一项研究,这可以通过2个选择小组来完成
Select * From Maintenance, Equipment
where Maintenance.id = Equipment.id group by maintenance.id Order By Maintenance.Entry DESC
它显示了我几乎想要但是
id location machine maintenance date entry
1 32 Roam 07-28-2009 1
2 33 Polisher 08-22-2017 4
3 34 Vacuum 05-25-2018 6
条目编号为1,因此它不是最近的服务。确实尝试了DESC,但它只是反转了仍然不是最新的条目您可以尝试这个-
Select * From Maintenance m inner join
Equipment on m.id = Equipment.id
where maintenancedate in (select max(maintenancedate) from Maintenance m1
where m.id=m1.id)
选择m.id=Equipment.id中的maintenancedate,从维护组中选择maxmaintenancedate(按id从维护组中选择maxmaintenancedate)