How to create an array of years ordered by year using SQL & PHP ?

Posted on 2008-11-13
Medium Priority
Last Modified: 2012-05-05

I'm using the following php/sql query to calculate the number of years between my earliest and most recent database records:
$query_years = mysql_query("SELECT YEAR(MAX(date)) - YEAR(MIN(date)) FROM locations");

This returns a single value and what i require instead is an array of all the YEARS in year order so that any year where there isn't a specific record for that year is omitted...
e.g. if my database had 2 records dated 1983 and 2008 - then it should just return these 2 years in chronological order, rather than all 25 years between the two dates.

 - please can you show me how this is done?

Many thanks
Question by:Daniish
LVL 12

Accepted Solution

Paul_Harris_Fusion earned 2000 total points
ID: 22952326
I'm not familiar with MySQL but if I assume that the YEAR function extracts the year part of the date then the following should work (based on Oracle and SQLServer sql)

Select distinct YEAR(date) from locations
order by date

The distinct qualifier removes duplicates from the result set.

An alternative is
Select y from
(Select YEAR(date) as yfrom locations order by date) inner
group by y

In this example the inner query potentially returns returns duplicate years and these are removed by the outer group by clause

Hope this helps
LVL 17

Expert Comment

ID: 22952723

$query_years = mysql_query("SELECT YEAR(MAX(date)) - YEAR(MIN(date)) FROM locations WHERE `date` BETWEEN YEAR(MAX(date)) AND YEAR(MIN(date)) ORDER BY `date`;");

Open in new window


Expert Comment

ID: 22955929
I'm not sure but try one of this:

$query_years = mysql_query("SELECT YEAR(MIN(date)),YEAR(MAX(date)) FROM locations ");


$query_years = mysql_query("SELECT DISTINCT YEAR(date) FROM locations ");

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Article by: Tammy
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief forma…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question