Php 如何在MYSQL中取消选择当前年份

Php 如何在MYSQL中取消选择当前年份,php,mysql,sql,Php,Mysql,Sql,所以我在选择数据库中的年份时遇到了这个问题。 从2015年到2019年,我的工作年限为。我必须删除当前年度。。我在使用这个方法,但我似乎不能正确使用 //This is the PHP code that I use to get the years existing and throw it in the option input <?php session_start(); require_once 'connection.php'; $output = ""; $query

所以我在选择数据库中的年份时遇到了这个问题。 从2015年到2019年,我的工作年限为。我必须删除当前年度。。我在使用这个方法,但我似乎不能正确使用

//This is the PHP code that I use to get the years existing and throw it in the option input
<?php 

session_start();
require_once 'connection.php';

$output = "";

$query = "(SELECT DISTINCT DATE_FORMAT(`date_happened`,'%Y') AS year FROM `tbl_flood_info`)
          UNION
          (SELECT DISTINCT DATE_FORMAT(`date_happened`,'%Y') AS year FROM `tbl_fire_info`)
          UNION
          (SELECT DISTINCT DATE_FORMAT(`date_happened`,'%Y') AS year FROM `tbl_accident_info`)
          UNION
          (SELECT DISTINCT DATE_FORMAT(`date_happened`,'%Y') AS year FROM `tbl_crime_info`)
          ORDER BY year DESC";

$result = mysqli_query($connection, $query);

$numrows = mysqli_num_rows($result);
$x = 0;
if ($numrows > 0) { 
    while($row = mysqli_fetch_assoc($result)){  
        $output .= "<option>" .$row['year']. "</option>";

        $x++;
    }
}

echo $output;

mysqli_close($connection);
?>

//The ajax code to get the values from the PHP file
    $.ajax({
        type: "post",
        url: "../get_year.php",
        success: function(data) {
            $("#selectYear").append(data.trim());
            getFilter();
        }
    });

你可以试试下面的方法-

    SELECT DATE_FORMAT(`date_happened`,'%Y') AS year FROM `tbl_flood_info`
    where year(`date_happened`)<year(now())
    UNION
    SELECT DATE_FORMAT(`date_happened`,'%Y') AS year FROM `tbl_fire_info` where year(`date_happened`)<year(now())
    UNION
    SELECT DATE_FORMAT(`date_happened`,'%Y') AS year FROM `tbl_accident_info` where year(`date_happened`)<year(now())
    UNION
    SELECT DATE_FORMAT(`date_happened`,'%Y') AS year FROM `tbl_crime_info` where year(`date_happened`)<year(now())
    ORDER BY year DESC"
我将使用year函数,而不是转换为字符串:

SELECT year
FROM ((SELECT year(date_happened) as year
       FROM `tbl_flood_info`
      ) UNION
      (SELECT year(date_happened) as year
       FROM `tbl_fire_info`
      ) UNION
      (SELECT year(date_happened) as year
       FROM `tbl_accident_info`
      ) UNION
      (SELECT year(date_happened) as year
       FROM `tbl_crime_info`
      ) 
     ) t
WHERE year < YEAR(CURDATE());

UNION会删除重复项,因此所有选择的差异都是不必要的。

Wow没有意识到可以这样做。干杯