基于类别获取最后一行的MySQL查询

基于类别获取最后一行的MySQL查询,mysql,greatest-n-per-group,Mysql,Greatest N Per Group,我有以下表格结构: Category ------------------- id slug values category sort Incidents ------------------- id scid title impact date servicestatus incidentsstatus details createdon editedon 在事件表中,一个类别有多个条目。scid是表category(id)中给出的类别id 我想显示事件表中具有servicestatus

我有以下表格结构:

Category
-------------------
id
slug
values
category
sort


Incidents
-------------------
id
scid
title
impact
date
servicestatus
incidentsstatus
details
createdon
editedon
在事件表中,一个类别有多个条目。scid是表category(id)中给出的类别id

我想显示事件表中具有servicestatus字段值的所有类别名称

有点

Service      Status
-------    ----------
Internet     1 
Email        0
Server1      1
请检查并建议我们可以用它做什么,我在category.id和insidents.scid的基础上尝试了两个表之间的连接,但这会显示重复的结果吗


谢谢

如果我理解正确,“servicestatus”列将包含0或1?还是

如果要查看是否存在任何“开放”事件,您可以将SQL设置为

SELECT 
    c.category, max(i.servicestatus) 
FROM 
    Category as c 
INNER JOIN 
    Incidents as i ON c.id = i.scid 
GROUP BY 
    c.category
MAX()
sql命令将从每个类别中的事件表行返回最高值

\T

使用此查询:

select cat.value, ins.servicestatus from Incidents ins, category cat where ins.scid = cat.id group by cat.id;

如果要从事件表中查看当前状态,应使用以下内容:

select c.Category, i.servicestatus 
from category as c inner join incidents as i on c.id = i.scid 
where i.date = (select max(date) from incidents as i2 where i.scid = c.id)

检查以下问题/答案: