Skip to main content
Participating Frequently
December 8, 2006
Question

How to use sql to get two pieces of data in one cfquery

  • December 8, 2006
  • 2 replies
  • 359 views
OK, that was probably a bad title, but ...

I am building a new "report" page for an existing application. In that application, I have three tables that hold information about a "request".

One table (tblstatus) holds "status" values with "stat_id" being the primary key and "status" being the value. So, stat_id 1=status "new", stat_id 2=status "denied", stat_id 3=status "approved", etc.

A second table (tblcssp) has information put in by a person with approval authority on this request. The table has a field (cssp_stat_id) that gets a 2 or 3 based on that person's determination.

The third table (tblcsfp) holds the same information by a third person who also has approval authority. It too has a field (csfp_stat_id) that holds a 2 or 3 based on that person's determination.

I need to output one line of information for each request that shows the status of each request with the approval or disapproval of each of those two people. But, I can't figure out how to do this without getting a conflict within my cfquery. If I use:

<cfquery name=qGetSTAT>
select stat_id, status, cssp_stat_id, csfp_stat_id
from tblstatus, tblcssp, tblcsfp
where tblstatus.stat_id=tblcssp.cssp_stat_id and tblstatus.stat_id=tblcsfp.csfp_stat_id
</cfquery>

the table only shows the translation of the determination from the tblcssp table in both of the fields. Basically, the translation of the data in the tblcsfp table never gets shown.

How can I take the data from two different tables and translate it via the one "status" table and display them both on one line of a table. I have not included all the html because that's not an issue.

Do I need to do two different queries? If so, how do I include the output in one cfoutput?

By the way, the tblcssp and tblcsfp tables always have the same number of rows. They are currently up to 995 rows each.

Thank you.
    This topic has been closed for replies.

    2 replies

    Inspiring
    December 8, 2006
    Oh, by the way, if you want to show the requests, regardless of whether or not your other tables contain the approval/disapproval info yet ('outstanding' requests or something?), use a 'left join' instead. That way, you'll get all the results from your first table, regardless of whether or not you have matching info in the other tables.
    Inspiring
    December 8, 2006
    You need to have a field in each table that has a 'request_id' to use as the primary key. That way, you can request any information from any table about any request_id, simply by having:

    select a.a_field, b.another_field, c.yet_another_field
    from first_table a, second_table b, third_table c
    where b.request_id = a.request_id
    and c.request_id = a.request_id

    If you're only looking for a particular request_id, you simply add:

    and a.request_id = the_request_id_you_want

    You might want to look up some info about database 'normalisation'.