I have a MySQL table with a datetime column named ‘created’ which contains the date and time the record was inserted. This table has about 30 records right now. I want to get all occuring years (unique, not per record) from this table. Is there a way to do this using a MySQL query?
SELECT DISTINCT YEAR(created) FROM table
Something on these lines should work (Warning: untested code ahead)
select year(created) as year, count(*) as count from table_name group by year
This will also give number of records for each year.
SELECT EXTRACT(year from created) as year FROM your_table GROUP BY year
should work 🙂
SELECT DISTINCT YEAR(Date) AS MYR
ORDER BY MYR
Select EXTRACT(year from your_column_name) as MYEAR from your_table_name group by EXTRACT(year from your_column_name)
The above worked for me…