• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

cfloop two tables

I have cfloop query and I need to add a column from another table into the cfloop. There are similar keys in each table but what is the best way to structure the query output?
0
aspiegel
Asked:
aspiegel
  • 2
1 Solution
 
Scott BennettCommented:
just use two loops. Something like this:
<cfquery name="FirstQuery" datasource="DSN">
SELECT something FROM aTable
</cfquery>

<cfquery name="SecondQuery" datasource="DSN">
SELECT somethingelse FROM anotherTable
</cfquery>

<cfloop query="FirstQuery">
      #firstQuery.something#
      <cfloop query="SecondQuery">
            #SecondQuery.somethingelse#
      </cfloop>
</cfloop>


or if your second query depends will be based on data from the first query:

<cfquery name="FirstQuery" datasource="DSN">
SELECT something FROM aTable
</cfquery>

<cfloop query="FirstQuery">
      #firstQuery.something#
      <cfquery name="SecondQuery" datasource="DSN">
      SELECT somethingelse FROM anotherTable where something = '#firstQuery.something#'
      </cfquery>
      <cfloop query="SecondQuery">
            #SecondQuery.somethingelse#
      </cfloop>
</cfloop>

how you sturcture the loops depends on the data and how you want it to be presented.
0
 
Scott BennettCommented:
you question is really vague so if that doesn't give you an idea of what to do, then give a sample of the queries you are doing and how you want to display the data.
0
 
aspiegelAuthor Commented:
That worked great. Thanks for your help and quick response.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now