在表格中选择最小,最大年份并定义其余部分

时间:2013-01-09 14:01:15

标签: mysql select range max min

我必须选择列的最小和最大年份并定义剩余年份。可以使用sql查询本身来完成。我不想使用PHP代码(这将是最后一个选项)

 Give the range from (select min(Tax filing), max(Tax filing) from tax);

 //Simply writing "Define the range from(nested query)".. please give some query tags which apply.. 

enter image description here

所以它应该自动定义范围。可以使用任何查询来完成吗?

3 个答案:

答案 0 :(得分:2)

select somegunk from sometable    
   where somedate between 
       (select max(year(someotherdate)) from sometable) 
   and (select min(year(someotherdate)) from sometable);

答案 1 :(得分:0)

如果要列出最小年和最大年之间的所有年份,则应执行以下操作:

SELECT yearCol
 from yourTable
 where yearCol between
   (select min(yearCol) from yourTable)
 and
  (select max(yearCol)FROM yourTable);

您需要在yourTable中记录所有年份的记录。

答案 2 :(得分:0)

您可以创建一个序列表,在其中插入所有可能的值:

create table sequence (id integer not null auto_increment primary key) auto_increment=1900;

insert into  sequence values(null); -- x times

并使用此查询:

select * from sequence where id between (select min(Tax_filing) from tax) and (select max(Tax_filing) from tax);