cfloop 和查询的查询

cfloop and query of queries

我在页面上有如下场景。我有一个从 cfc all_data 返回的查询,它有列、部分、状态、数据。现在页面设计如下所示。

section1 section2 section3 section4 -> select一节到select里面的状态

假设 section1 是 selected -> 需要显示与该部分关联的 State1 state2 state3 -> select 一个状态以查看与其相关的数据。

说 State3 是 selected -> 显示相关的 State3 数据。

所以基本上我需要 3 个 cfloops 来完成上述任务。我在做`

<cfquery name="section" dbtype="query">
 select distinct section from all_data 
</cfquery>`

对于第一个循环,我循环 'section' 查询以显示所有部分。

<cfloop query ="section">
    <cfquery name="state" dbtype="query">
    select distinct state from all_data where section = section.section
    </cfquery>
</cfloop>

为了状态显示我像上面一样循环。对于作为数据显示的循环 3,我尝试了多种方法,但似乎没有任何方法是正确的。这是正确的方法吗?任何见解表示赞赏。

你的意思是这样的吗?

    <cfloop query ="section">
        <cfquery name="state" dbtype="query">
          select distinct state from all_data where section = section.section;
        </cfquery>
        <cfloop query ="state">
            <cfquery name="getdata" dbtype="query">
              select * from all_data where section = section.section 
              and state = state.state;
            </cfquery>
            <cfdump var=#getdata#>
        </cfloop>
    </cfloop>

我想你可以使用如下的组属性

<cfset myQuery = QueryNew("Section, State, Data", "VarChar, VarChar, VarChar")> 

<cfset newRow = QueryAddRow(MyQuery, 5)> 

<!--- Set the values of the cells in the query ---> 
<cfset temp = QuerySetCell(myQuery, "Section", "Section 1", 1)> 
<cfset temp = QuerySetCell(myQuery, "State", "State 1", 1)> 
<cfset temp = QuerySetCell(myQuery, "Data", "Data 1", 1)> 
<cfset temp = QuerySetCell(myQuery, "Section", "Section 1", 2)> 
<cfset temp = QuerySetCell(myQuery, "State", "State 2", 2)> 
<cfset temp = QuerySetCell(myQuery, "Data", "Data 2", 2)> 
<cfset temp = QuerySetCell(myQuery, "Section", "Section 1", 3)> 
<cfset temp = QuerySetCell(myQuery, "State", "State 2", 3)> 
<cfset temp = QuerySetCell(myQuery, "Data", "Data 3", 3)> 
<cfset temp = QuerySetCell(myQuery, "Section", "Section 2", 4)> 
<cfset temp = QuerySetCell(myQuery, "State", "State 2", 4)> 
<cfset temp = QuerySetCell(myQuery, "Data", "Data 2", 4)> 
<cfset temp = QuerySetCell(myQuery, "Section", "Section 2", 5)> 
<cfset temp = QuerySetCell(myQuery, "State", "State 2", 5)> 
<cfset temp = QuerySetCell(myQuery, "Data", "Data 3", 5)> 

 <cfoutput query ="myQuery" group="Section">
    </br>#Section# <!--- You will get distinct Sections here --->
    <cfoutput group="Section">
            </br>#State#,
            <cfoutput>#Data#,</cfoutput>
    </cfoutput>
 </cfoutput>