Mysql 计数排除最大和最小发生次数

Mysql 计数排除最大和最小发生次数,mysql,sql,database,count,Mysql,Sql,Database,Count,例如,如果我们去: 放 SELECT City, count(City) as Occurrences FROM Customers GROUP by City ORDER BY count(City) DESC 但我真正想要的是排除最大值和最小值(忽略硬编码的最大值和最小值的6和1值),如 没有硬编码6和1,获得所需输出的方法是什么?试试这个 SELECT City, count(City) as Occurences FROM Customers, (SELECT MAX(Occur

例如,如果我们去:

SELECT City, count(City) as Occurrences
FROM Customers 
GROUP by City
ORDER BY count(City) DESC

但我真正想要的是排除最大值和最小值(忽略硬编码的最大值和最小值的6和1值),如

没有硬编码6和1,获得所需输出的方法是什么?

试试这个

SELECT City, count(City) as Occurences FROM Customers,
(SELECT MAX(Occur) AS Ma,MIN(Occur) AS Mi FROM (SELECT City, count(City) as Occur
FROM Customers GROUP by City)) as T 
GROUP BY City HAVING Occurences!=T.Ma AND Occurences!=T.Mi ORDER BY Occurences DESC
试试这个

SELECT City, count(City) as Occurences FROM Customers,
(SELECT MAX(Occur) AS Ma,MIN(Occur) AS Mi FROM (SELECT City, count(City) as Occur
FROM Customers GROUP by City)) as T 
GROUP BY City HAVING Occurences!=T.Ma AND Occurences!=T.Mi ORDER BY Occurences DESC
你可以试试这个

select c1.city, c1.cnt from (
select city, count(*) cnt from customers c
group by city
) c1 inner join 
(select max(cnt) max_cnt, min(cnt) min_Cnt from (
select city, count(*) cnt from customers c
group by city
)) c2
on c1.cnt!=c2.max_cnt and c1.cnt!=c2.min_cnt
;
因为MySQL没有按函数划分的OVER..分区,这可能很有用。 另一种方法可以是rownums和ordering,但我更喜欢这种方法

select c1.city, c1.cnt from (
select city, count(*) cnt from customers c
group by city
) c1 inner join 
(select max(cnt) max_cnt, min(cnt) min_Cnt from (
select city, count(*) cnt from customers c
group by city
)) c2
on c1.cnt!=c2.max_cnt and c1.cnt!=c2.min_cnt
;
因为MySQL没有按函数划分的OVER..分区,这可能很有用。
另一种方法可以是rownums和订购,但我更喜欢这个

谢谢!这就是我一直在寻找的答案:)谢谢!这就是我一直在寻找的答案:)