Skip to main content
November 26, 2008
Question

Multiple Table Query

  • November 26, 2008
  • 2 replies
  • 520 views
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...

This topic has been closed for replies.

2 replies

Inspiring
November 26, 2008
You can accomplish the same thing with select distinct. Do your tables have primary keys? Given that you are grouping on two "id" fields, I don't see how you would get duplicates.
Inspiring
November 26, 2008
First, this:
* 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

doesn't accomplish anything.

Second, when I read what you posted about the two airdates tables, I'm not convinced that the table structure achieves what the comments and fieldnames describe as the objective.

You say none of your projects have a current airdate. Do these tables have any data at all? If not, it's going to be hard to write a query when you can't ensure you got the correct results.
November 26, 2008
Actually without that Group By in the query, my results would sometimes return duplicated. I'd have two of the same projects listed, despite having a group attribute in my cfoutput...