1

我正在使用循环查询并在存在要加入的记录时加入表。

当没有记录加入该行时,有没有办法让该行完全返回?

编辑:我错过了一个 if 语句。我正在遍历记录并检查设置选项,如果该设置选项存在于记录中并且查询“someRecord”中没有相应的记录,那么这些是我不想返回的记录。

<cfloop query="myquery">
    <cfif listfindnocase(myquery.setup_option,"required_information")>
        <cfquery name="someRecord" dbtype="query">
            select * from second_table
            where key_id = '#myquery.key_id#'
        </cfquery>
        <cfif someRecord.recordcount eq 0>

        <!--- Need something here to remove this row from returning in the query --->

        </cfif>
    </cfif>
</cfloop>
4

3 回答 3

5

好的,这里的模型答案是:不要在 CFML 中执行此操作,而是在 DB 上执行此操作。CF 用于字符串生成,而不是数据操作。

这个问题有点误导,因为它最初询问如何从查询中删除行,这 - 事实证明 - 不是要求(参见问题的评论)。我已经在下面回答了这个问题。

要简单地退出循环的迭代,请使用<cfcontinue>. 这会立即结束循环的当前迭代,并返回到代码块的顶部并开始下一次迭代。使用您自己的代码示例:

<cfloop query="myquery">
    <cfif listfindnocase(myquery.setup_option,"required_information")>
        <cfquery name="someRecord" dbtype="query">
            select * from second_table
            where key_id = '#myquery.key_id#'
        </cfquery>
        <cfif someRecord.recordcount eq 0>
            <cfcontinue>
        </cfif>
        <!--- handle the rows you *do* want to process here --->
    </cfif>
</cfloop>

然而,要回答如何从查询中删除行的问题,没有优雅的方法可以做到这一点。你有两个不优雅的选择:

// pseudocode, for brevity
newQuery = queryNew(oldQuery.columnList)
loop (oldQuery)
    if the row is not wanted
        continue
    /if
    add a row to newQuery
    add the row data to newQuery
/loop

或者:

listOfRowsToExclude = someMechanismToArriveAtSaidList()
<cfquery name="newQuery" type="query">
    SELECT   *
    FROM     oldQuery
    WHERE    id NOT IN (listOfRowsToExclude)
    ORDER BY [same clause as for oldQuery]
</cfquery>

然而,到目前为止,最好的建议是在它所属的数据库中进行数据处理。您不应该将这种逻辑放在 a) 您的 CFML 应用程序中;b)在您的视图代码中,这就是我怀疑这一切的地方。

将您的逻辑与您的显示分开。并将您的数据处理逻辑与应用程序逻辑分开。

于 2014-12-29T16:59:59.607 回答
1

与其循环查询以生成多个SELECTs(正如其他人提到的那样,它使用了不必要的资源),不如尝试使用一个IN子句 usingValueList来提取 ID 列表:

<cfset myqueryIDs = ValueList(myquery.key_id)>
<cfif listfindnocase(myquery.setup_option,"required_information")>
    <cfquery name="allRecords" dbtype="query">
        select * from second_table
        where key_id IN (#myqueryIDs#) <!--- Assuming numeric IDs --->
    </cfquery>
</cfif>
于 2014-12-29T19:19:37.607 回答
0

我假设在您关闭 cfif 之后还有其他事情要做,您应该反转您的 if 语句并执行以下操作:

<cfloop query="myquery">
    <cfquery name="someRecord" dbtype="query">
        select * from second_table
        where key_id = '#session.key_id#'
    </cfquery>

    <cfif someRecord.recordcount neq 0>

      <!--- do what you would do when record count NOT equal to zero --->
    </cfif>
</cfloop>
于 2014-12-29T16:13:15.607 回答