SQL - GROUP BY有计数,连接多个表

时间:2017-12-12 17:58:17

标签: mysql sql join count having

我有4张桌子:

Entries:
id
year
day

AnalysisText:
id
text
entries_id

AnalysisType:
id
type

AnalysisTextTypes:
id
analysistext_id
analysistype_id

示例数据:

entries     
id  year    day
1   2010    1
2   2011    4
3   2012    6


AnalysisText        
id  text    entries_id
1   blah    2
2   more blah   1
3   blah blah   1

AnalysisType        
id  type    
1   A   
2   B   
3   C   

AnalysisTextTypes       
id  analysistext_id analysistype_id
1   1   1
2   1   2
3   2   3
4   3   1

我需要获取具有多个analysistext的所有analysistype值,以及与这些entries相关联且具有多个analysistext的所有analysistype my_date text type 2011-4 blah A 2011-4 blah B analysisText 1}}。

所以在上面的数据中,我们想要结果

analysisType

因为那是具有多种类型的文本。

下面的查询有效,但使用子查询来识别超过select CONCAT(e.year, '-', e.day) as my_date, x.text, y.type from entries e join analysistext x on e.id = x.entries_id join analysistext_types xy on xy.analysistext_id = x.id join analysistype y on y.id = xy.analysistype_id where xy.analysistext_id in ( select analysistext_id from analysistext_types group by analysistext_id having count(analysistype_id) > 1 ) order by my_date, analysis_text, type; 的{​​{1}}值感觉效率低下:

SELECT

我想要一个查询,其中我们group by来自连接表的感兴趣字段,并直接对要连接的表执行having countpandas。这可能吗?

1 个答案:

答案 0 :(得分:0)

您可以使用自联接来完全避免子查询

select concat(e.year, '-' ,e.day), at.text ,ant.type
from AnalysisText at
join entries e on at.entries_id = e.id
join AnalysisTextTypes att on att.analysistext_id = at.id
join AnalysisType ant on att.analysistype_id = ant.id
join AnalysisTextTypes att2 on att2.analysistext_id = att.analysistext_id
group by att2.analysistext_id
having count(att2.analysistext_id) > 1