返回从特定的一年中的所有记录,符合特定标准

问题描述 投票:-1回答:1

我有行,在一个表,其中有一个地方得到了病人感染了病毒,当她被治好了日期。我也有一列中,病毒的ID,这是一个外键,并点到另一个表,其中的病毒名称存储我已经想出迄今:

SELECT myColumns
FROM myTable
WHERE dateColumn BETWEEN '2018-01-01' AND '2018-12-31';

该datecolumn已存储,例如日期:

patient infected date
2002-01-22 13:25:41
patient healed date
2002-01-24 10:35:21

我尝试这样做,是有说100倍最通常所见的病毒,即已经感染,并在同一年被治好了,沿着他们的头衔。

像(ORDER BY出现的次数)

virus1 | name of the virus | number of occurrences 
virus2 | name of the virus | number of occurrences 

上述查询应该怎样成为?

有患者数据,该病毒ID一起表:

Column  Type    Comment
dateInfected    datetime NULL    
dateHealed  datetime NULL    
idOfVirus   bigint(20) NULL 

具有病毒的名称表:

Column  Type    Comment
id  bigint(20)   
virusName   text NULL
mysql
1个回答
0
投票

您可以通过病毒的ID连接表,发现出现次数的计数,排序计算和应用所需的极限(假设其100按你的问题),您可以使用

SELECT V.id as "Virus Id", V.virusName as "Virus Name", count(V.id) as "occurences"
FROM virus V,infect I 
WHERE V.id = I.idOfVirus
GROUP BY V.id, V.virusName
ORDER BY count(V.id) DESC
LIMIT 100
© www.soinside.com 2019 - 2024. All rights reserved.