简体   繁体   中英

Select only year from MySql

I need to select only the year from the record date.

SELECT *, DATE_FORMAT('release','%Y') AS release_year FROM books

but doesnt work. The result in phpmyadmin is NULL.

SELECT *, YEAR(`release`) AS release_year FROM books

I think release is a MySQL keyword. Try to wrap it around ``

Don't put release in quotes. You're trying to extract the year from a literal string 'release' , not the value in the column ` release `.

And as @invisal states, RELEASE is a reserved word in MySQL , so you have to delimit it with back-ticks.

Your problem :

DATE_FORMAT( 'release' ,'%Y')

in Mysql that is a string.

Fix :

SELECT *, DATE_FORMAT(`release`,'%Y') AS release_year FROM books;

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