Question
Multiple Table Query
Hey All!
It's been a while since I last posted a message in these forums. But it looks like I need your help again.
So here is the problem:
I have to create a list of the companies' projects, yet to compile the complete list I have to query 4 tables, with a one to many and many to many relation ship.
Here is what I have so far.
the main table is the projects table, hence I start with that. Right now I can query the table, plus the companies table that each project is linked to. Also, each project may have a link to multiple companies, so I normalized the data by creating a companies link table:
SELECT projects.project_id, project.project_name, compproj_link.company_id
/* I only get the company id and convert it to an actual name when I output */
FROM projects LEFT JOIN compproj_link ON compproj_link.project_id = projects.project_id
/* here I also filter to get only active projects */
WHERE project.status in(1,2)
/* I group by the project id since there might be several companies linked to a specific project */
GROUP BY projects.project_id, project.project_name, compproj_link.company_id
ORDER BY project_name
This works perfectly fine when I output with the group attribute, but now I have to link another table... called the airdates. The reason is that I want to order my list by the contents of this table (which may or may not be present)...
this airdates that is linked to my projects is structured like so:
airdates
airdates_id - auto num
project_id - the project it is linked to hence could be linked to many projects
then there are the actual air dates
airdateslist
airdateslist_id auto num
airdates_id - since for each air date record could be multiple dates
airdate_date - date field
airdate_backupdate - another date field
airdate_current - this is a yes/no, and specifies whether this date is most current.
Hence, by doing two more LEFT OUTER JOINS in my main query would work fine, BUT I need to get the most current date from the airdateslist that is set for a particular project. and when I do this I get no records because as of yet, no project has an airdate that is current.
So my question is if I can somehow add those airdates tables to my main query, and be able to sort by the current date, IF it is present, and if not, still have all my projects displayed?
PS. I think I have confused myself even more when writing this out, so if you have any questions please write. Thank you in advance!
Vega...
It's been a while since I last posted a message in these forums. But it looks like I need your help again.
So here is the problem:
I have to create a list of the companies' projects, yet to compile the complete list I have to query 4 tables, with a one to many and many to many relation ship.
Here is what I have so far.
the main table is the projects table, hence I start with that. Right now I can query the table, plus the companies table that each project is linked to. Also, each project may have a link to multiple companies, so I normalized the data by creating a companies link table:
SELECT projects.project_id, project.project_name, compproj_link.company_id
/* I only get the company id and convert it to an actual name when I output */
FROM projects LEFT JOIN compproj_link ON compproj_link.project_id = projects.project_id
/* here I also filter to get only active projects */
WHERE project.status in(1,2)
/* I group by the project id since there might be several companies linked to a specific project */
GROUP BY projects.project_id, project.project_name, compproj_link.company_id
ORDER BY project_name
This works perfectly fine when I output with the group attribute, but now I have to link another table... called the airdates. The reason is that I want to order my list by the contents of this table (which may or may not be present)...
this airdates that is linked to my projects is structured like so:
airdates
airdates_id - auto num
project_id - the project it is linked to hence could be linked to many projects
then there are the actual air dates
airdateslist
airdateslist_id auto num
airdates_id - since for each air date record could be multiple dates
airdate_date - date field
airdate_backupdate - another date field
airdate_current - this is a yes/no, and specifies whether this date is most current.
Hence, by doing two more LEFT OUTER JOINS in my main query would work fine, BUT I need to get the most current date from the airdateslist that is set for a particular project. and when I do this I get no records because as of yet, no project has an airdate that is current.
So my question is if I can somehow add those airdates tables to my main query, and be able to sort by the current date, IF it is present, and if not, still have all my projects displayed?
PS. I think I have confused myself even more when writing this out, so if you have any questions please write. Thank you in advance!
Vega...
