Hi people
I have been struggling with a query that I am trying to run
I have a table which stores information about people
The table definition is:
The table holds for every person, his birth yearCode:CREATE TABLE `people` ( `Individual_ID` int unsigned not null, `BirthYear` int default '', PRIMARY KEY (`Individual_ID`), ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Given a specific person, I would like to get up to 3 other people from the table, ordered according to their closeness in birthyear to that person
That is order all the people according to how close their birth year is to that person
Here is what I mean by birth year closeness
assume that person 1 was bon on 1970
and we have the following people in the database:
person 2, born in 1973
person 3, born in 1966
person 4, has an empty birthyear
person 5, born in 1970
person 6, born in 1980
The 3 people returend will be (in that order)
person 5, born in 1970 (0 years apart)
person 2, born in 1973 (3 years apart)
person 3, born in 1966 (4 years apart)
People with an empty birthyear should be considered last
I would like to get the results in a single query
I thought of something along the line of: for each considered person, subtract his birthyear from the
birth year of the selected person and take absolute value of that
now order the value according to decreasing value. So people with absolute value of 0 will be first
any help would be appreciated, as I am struggling
with the order by clause
thanks









Bookmarks