cfloop和查询查询

时间:2017-06-07 15:09:04

标签: coldfusion qoq

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

section1 section2 section3 section4 - >选择一个部分来选择其中的状态

让我们说第1节被选中 - >需要显示与该部分相关联的State1 state2 state3 - >选择一个状态以查看与其相关的数据。

选择说State3 - >显示相关的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>

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

2 个答案:

答案 0 :(得分:1)

你是说这样的意思吗?

    <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>

答案 1 :(得分:1)

我认为您可以使用group属性,如下所示      

<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>