Jump to content

help with multiple table query please


chiprivers

Recommended Posts

I have a database containing information I have collated with reference to my family tree.  The database includes the tables below:

individuals -  individual_ID, surname_birth, first_names_birth, sex  (contains basic details for each person)

births - individual, date_day, date_month, date_year (contains details of birth and connects to individuals using individual_ID = individual)

links - link_from, link_to, link (records connections between people where link_from and link_to are the individual_ID's of the people and link is the relationship between the two individuals)

link_types - link, link_group (this puts the link entries into categories where links.link = link_types.link and then the link_group would be one of either "Parent", "Sibling", "Spouse" or "Child")

What I am struggling to do is extract the name and birth details of any individual that shares a common link_group and is related to a selected individual, and then sort the results in order of birth date.

I am thinking along the lines of:

SELECT surname_birth, first_names_birth, sex, date_day, date_month, date_year, link FROM individuals, births, links, link_types WHERE individuals.individual_ID = births.individual AND individuals.individual_ID = links.link_to AND links.link_from = [slected person] AND links.link = link_types.link AND link_types.link_group = [selected group] ORDER BY births.date_year, births.date_month, births. date_day ASC

Any help would be very much appreciated, many thanks.
Link to comment
Share on other sites

The query you posted appears fine as long as for every link_from and link_to relationship you have the reverse as well.

ie
[code]
link_from  | link_to  | link
-----------------------------
1          | 2        | 3
2          | 1        | 3
[/code]

Do you notice any problems with the resullts of the query?

The following syntax may be easier to follow.

[code]
SELECT
surname_birth, first_names_birth, sex,
date_day, date_month, date_year, link
FROM
links AS l
INNER JOIN
individuals AS i
ON
l.link_to = i.individual_ID
INNER JOIN
link_types AS lt
ON
l.link = lt.link
INNER JOIN
births AS b
ON
i.individual_ID = b.individual
WHERE
l.link_from = [selected_person]
AND
lt.link_group = [selected_group]
ORDER BY
b.date_year, b.date_month, b.date_day
[/code]

You should also consider using a single DATE column to store the birth dates.
Link to comment
Share on other sites

I'll give that query a try and post my results. 

I was going to use one column for the date to hold a date stamp but that would not allow dates prior to 1970.  Obviously, working on a family tree, there are going to be a large number of dates prior to 1970.  DO you have any other suggestions for the date?  I know my solution means having three columns but it seems straight forward enough without any causing any problems.
Link to comment
Share on other sites

[quote author=chiprivers link=topic=114513.msg466090#msg466090 date=1163181544]
I was going to use one column for the date to hold a date stamp but that would not allow dates prior to 1970.  Obviously, working on a family tree, there are going to be a large number of dates prior to 1970.  DO you have any other suggestions for the date?
[/quote]

The DATE type in MYSQL is not the same as a unix timestamp. The DATE type has no issues with dates prior to 1970.

[quote=http://dev.mysql.com/doc/refman/4.1/en/date-and-time-type-overview.html]
DATE

A date. The supported range is '1000-01-01' to '9999-12-31'. MySQL displays DATE values in 'YYYY-MM-DD' format, but allows you to assign values to DATE columns using either strings or numbers.
[/quote]

http://dev.mysql.com/doc/refman/4.1/en/date-and-time-types.html
Link to comment
Share on other sites

Would there be any advantage of changing my dates to this storage type?

I have already stored a lot of info in my database and so unless there is any notable advantage, I will probably keep as is.

Many thanks on the help with the select statement, it is working perfectly. ;D
Link to comment
Share on other sites

I did try and use the date and datetime column types in MySQL on a previous project but I didn't get along with them very well because the default format is not UK standard.  I couldn't find a simple way around that which is why I switched to using timestamp, for that project the restrictions did not pose a problem.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.