sql-多个表选择最后一个值名称

时间:2019-11-27 14:17:11

标签: sql mariadb zabbix groupwise-maximum

我在mariadb数据库中有五个表,我想选择每个名称表的最后一个值,

enter image description here

我执行此sql命令以获取主机,名称和严重性,

我尝试了这个sql命令:

select host, e.name, e.severity
from hosts
left join items i on (i.hostid = hosts.hostid)
inner join functions f on (f.itemid = i.itemid)
inner join triggers t on (t.triggerid = f.triggerid)
INNER JOIN events e ON (e.objectid = t.triggerid)
group by hosts.hostid;

我想要最后一个主机,名称和严重性, 我想要这个结果在sql中:

enter image description here

1 个答案:

答案 0 :(得分:0)

我以triggerid在triggers表中递增的假设来回答这个问题

select host, e.name, e.severity
from hosts
left join items i on (i.hostid = hosts.hostid)
inner join functions f on (f.itemid = i.itemid)
inner join triggers t on (t.triggerid = f.triggerid)
INNER JOIN events e ON (e.objectid = t.triggerid)
where t.triggerid = (select max(t.triggerid) from triggers)