简体   繁体   English

MYSQL sql在count大于x的表中查找id

[英]MYSQL sql to find id's in a table with count greater than x

I have a table which has the raw data. 我有一个包含原始数据的表。 In order to check for unwanted extra data I am trying to determine what id's (tickerid is the column, earningsdata is the table) have more than 50 rows of data. 为了检查不需要的额外数据,我试图确定哪些id(tickerid是列,incomedata是表)有超过50行的数据。 It would have to loop through all the current ticker id's (from a different table, earningstickers) and select only those id's that have more than 50 rows of data. 它必须遍历所有当前的股票代码ID(来自不同的表,赚钱者)并且只选择那些具有超过50行数据的id。 I can do a select * by id but I'm not sure how to iterate through all the id's 我可以通过id做一个select *但是我不知道如何迭代所有的id

You can use GROUP BY to group by each tickerid , which will give you access to aggregate information related to each tickerid such as COUNT / SUM / AVG /etc... 您可以使用GROUP BY按每个tickerid ,这样您就可以访问与每个tickerid相关的汇总信息,例如COUNT / SUM / AVG / ...

For your purposes, we must use COUNT . 出于您的目的,我们必须使用COUNT

Then the HAVING clause filters out the tickerid s that have 50 rows or less: 然后HAVING子句过滤掉50行或更少行的tickerid

SELECT tickerid
FROM earningsdata
GROUP BY tickerid
HAVING COUNT(*) > 50

This will just give you a list of the tickerid s that have more than 50 rows, however if you want to display all rows and information of the tickerid s that are in this list, you can join the table against this list by incorporating the above query into: 这将只给出一个包含超过50行的tickerid列表,但是如果要显示此列表中所有行和列表中的tickerid信息,可以通过合并上面的列表来加入该列表。查询到:

SELECT a.*
FROM earningsdata a
INNER JOIN
(
    SELECT tickerid
    FROM earningsdata
    GROUP BY tickerid
    HAVING COUNT(*) > 50
) b ON a.tickerid = b.tickerid
SELECT tickerid 
FROM earningstickers 
WHERE tickerid 
IN 
    (SELECT tickerid
     FROM earningsdata
     GROUP BY tickerid
     HAVING COUNT(*) > 50
     )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM