php mysql选择月份和年份

时间:2016-05-12 08:49:56

标签: php mysql

在我的MySQL数据库中,有一列是“Y / m / d”格式的employment_date,我想要的是,只选择employment_date的年份和月份。以下是我的数据库查询。

display: flex; align-items: center;

但它不起作用,在我的表格中选择月份和年份的正确方法,请帮助!

5 个答案:

答案 0 :(得分:1)

您的查询应该是: -

// return month and date in single row
SELECT EXTRACT( YEAR_MONTH FROM `employment_date` ) 
FROM employee WHERE emp_id='$userID';

OR

// return month and date in two different row
SELECT YEAR(employment_date) AS 'year', MONTH(employment_date) AS 'month'
FROM employee Where emp_id='$userID';

警告: -

  

mysql_ *函数被删除并从PHP7中删除。所以使用   mysqli_*或更好PDO

答案 1 :(得分:1)

在查询中删除一个选择

$res=mysql_query("Select Select YEAR (employment_date) AND MONTH(employment_date) from employee where emp_id='$userID'") or die("query error".mysql_error());

 $res=mysql_query("Select YEAR(employment_date) AND MONTH(employment_date) from employee where emp_id='$userID'") or die("query error".mysql_error());

答案 2 :(得分:1)

 Select YEAR (employment_date) AS 'year', MONTH(employment_date) AS 'month' from employee where emp_id='$userID'"

希望它有所帮助并尝试使用mysqlipdo

答案 3 :(得分:1)

试试这个

如果您想要月份和年份相同的列

SELECT DATE_FORMAT(employment_date, '%Y:%m') as employment_date from employee where emp_id='$userID'

(或)

如果你想要单独的列

SELECT YEAR(employment_date) AS 'year', MONTH(employment_date) AS 'month'
FROM employee Where emp_id='$userID';

如果您只想要月份和日期

  SELECT MONTH(employment_date) AS 'month', DATE(employment_date) AS 'date'
  FROM employee Where emp_id='$userID';

答案 4 :(得分:0)

function checkDate($userID){
 $res=mysql_query("SELECT YEAR (employment_date) AS employment_year AND MONTH(employment_date) AS employment_month FROM employee WHERE emp_id='$userID'") OR die("query error".mysql_error());
 $array = mysql_fetch_array($res);
 $value_year=$array['employment_year'];
$value_month=$array['employment_year'];

 print_r($value_year . "_" . $value_month);
}

也许这会有帮助吗?