从mysql中的日期选择年份时的零值

时间:2013-01-31 17:51:55

标签: php mysql database

我正在尝试使用PHP在MySQL中选择年份:

session_start();
$school_id = $_SESSION['school_id'];
$section_id = $_POST['section_id'];
$today  = date("Y");

$select_grades_deped = "SELECT * 
             FROM deped_grade_archive 
             WHERE school_id='$school_id'
             AND section_id='$section_id'
             AND year(submission_date) ='$today' ";

$query_check_grades= mysql_query($select_grades_deped) or die (mysql_error());

$check_count = mysql_num_rows($query_check_grades);

if($check_count == 0){
    $return['checked'] = "0";
}
else{
    $return['checked'] = "1";
}

echo json_encode($return);

但不幸的是,当我尝试以下内容时:

SELECT year( submission_date )
FROM deped_grade_archive
WHERE school_id = '$school_id'
AND section_id = '$section_id'

它从我的表中返回零值。我将submission_date的数据类型更改为date数据类型,因为它之前是varchar,但仍然没有用。请帮帮我们。

2 个答案:

答案 0 :(得分:1)

将其更改为date数据类型时,如果未设置列的默认值(例如CURRENT_TIMESTAMP),则值为0000-00-00。因此,YEAR操作将返回0,如图所示。

您需要将submission_date列更新为您希望为每行指定的时间。

答案 1 :(得分:0)

尝试就好了

           SELECT YEAR('submission_date')

ref