简体   繁体   中英

Select specific year from MySQL

I have column which name is date . This column's format is like D/M/Y (14/11/2016) . I just want to select 2016 year. How can I do?

I'm using this code right now, what must I add?

mysql_query("SELECT * FROM blog ORDER BY id DESC");

Your query could be:

"SELECT * FROM blog WHERE YEAR(date)='2016' ORDER BY id DESC";

if your date column is a date data type. But since you are not using the standard mysql date format (YYYY-MM-DD) I guess it is not.

Please note that you are using a deprecated MYSQL_* API and you should switch to MYSQLI or PDO

我建议您不要使用mysql_query而是改用PDO或MySQLi,但要根据您的问题:

 mysql_query("SELECT * FROM blog WHERE YEAR(Date) = 2016 ORDER BY id DESC")
mysql_query("SELECT * FROM blog WHERE YEAR(Date) = 2016 ORDER BY id DESC");

or

mysql_query("SELECT * FROM blog WHERE date >= "00/00/2016" AND date <= "31/12/2016" ORDER BY id DESC");

or

mysql_query("SELECT DATE_FORMAT(date, '%d %m %Y') AS your_date FROM blog WHERE your_date LIKE "%2016" ORDER BY id DESC");

试试这个代码:

SELECT YEAR(STR_TO_DATE('14/11/2016', '%d/%m/%Y'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM