I am running a query for my application, that is really making me wish I used ORM. My table are structured as follows:
tabs
- id
- name
- sort
fields
- id
- label
- tabid
As you can assume there is a one-to-many relationship between fields and tabs. What I would like to do is, using pure SQL if possible, create a query that has the tabs and underneath each tab shows a subquery of all fields.
Currently I am just doing the following, but I was wondering if there is something better to do.
<cfquery name="local.tabQuery" attributeCollection="#Variables.dsn#">
SELECT id,name FROM tabs ORDER BY sort
</cfquery>
<cfset local.tabs = [] />
<cfloop query="local.tabQuery">
<cfquery name="local.fields" attributeCollection="#Variables.dsn3">
SELECT * FROM fields WHERE tabid = <cfqueryparam value="#local.tabQuery.id#" cfsqltype="cf_sql_integer" />
<cfquery>
<cfset arrayAppend(local.tabs, local.fields) />
</cfloop>
Note: That is not my actual code, but that should, in theory, work just fine.
You want grouped output.