Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在表中选择“最小年”、“最大年”,然后定义其余年份_Mysql_Select_Range_Max_Min - Fatal编程技术网

Mysql 在表中选择“最小年”、“最大年”,然后定义其余年份

Mysql 在表中选择“最小年”、“最大年”,然后定义其余年份,mysql,select,range,max,min,Mysql,Select,Range,Max,Min,我必须选择列的最小和最大年份,并定义剩余年份。这可以使用sql查询本身来完成。我不希望使用php代码(这将是最后一个选项) 因此,它应该自动定义范围。使用任何查询都可以做到这一点吗?如果您想列出最小年份和最大年份之间的所有年份,则应执行以下操作: select somegunk from sometable where somedate between (select max(year(someotherdate)) from sometable) and

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


因此,它应该自动定义范围。使用任何查询都可以做到这一点吗?

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

select somegunk from sometable    
   where somedate between 
       (select max(year(someotherdate)) from sometable) 
   and (select min(year(someotherdate)) from sometable);
SELECT yearCol
 from yourTable
 where yearCol between
   (select min(yearCol) from yourTable)
 and
  (select max(yearCol)FROM yourTable);

您需要在表中保留这段时间内所有年份的记录。

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

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);

定义其余的是什么意思?一些解释可能会有所帮助
select * from sequence where id between (select min(Tax_filing) from tax) and (select max(Tax_filing) from tax);