如何编写sql查询以获取范围内的项目

时间:2016-04-25 10:49:34

标签: mysql sql

我想获得没有最小值和最大值的值,所以没有在NumberOfRepeating列中输入2和29。

我的查询是:

SELECT Note, COUNT(*) as 'NumberOfRepeating'
  WHERE COUNT(*) <> MAX(COUNT(*))AND COUNT(*) <> MIN(COUNT(*))
   FROM Note GROUP BY Note;

4 个答案:

答案 0 :(得分:1)

   SELECT Note, COUNT(*) as 'NumberOfRepeating'
FROM Notes

GROUP BY Note
HAVING count(*) < 
(

SELECT max(t.maxi)
FROM (select
Note, COUNT(Note) maxi FROM Notes
 GROUP BY Note
 ) as t 
)

 AND
 count(*) >
(

SELECT min(t.min)
FROM (select
Note, COUNT(Note) min FROM Notes
 GROUP BY Note
 ) as t 
)

试试这段代码。

答案 1 :(得分:0)

一种方法会使用order bylimit两次:

select t.*
from (select t.*
      from t
      order by NumberOfRepeating asc
      limit 99999999 offset 1
     ) t
order by NumberOfRepeating desc
limit 99999999 offset 1;

答案 2 :(得分:0)

enter image description here试试这段代码,

Select * from Note where NumberOfRepeating < (select MAX(NumberOfRepeating) from Note ) AND NumberOfRepeating > (select MIN(NumberOfRepeating) from Note );

在代码中,与表格一样Note是表格的名称,NumberOfRepeating是列名称,如表格所示。

答案 3 :(得分:0)

试试这个。它应该工作

SELECT *
FROM ( SELECT Note, COUNT(*) as 'NumberOfRepeating'
      FROM Notes 
      GROUP BY Note
      ORDER BY NumberOfRepeating DESC
      LIMIT 1, 2147483647
    ) T1
ORDER BY T1.NumberOfRepeating
LIMIT 1, 2147483647