我正在尝试使用此方法在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,但仍然没有用。请帮助大家。

最佳答案

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

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

关于php - 在mysql中从日期选择年份时为零值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14632045/

10-16 19:01