ColdFusion:特定ID的访问查询结果集

时间:2015-12-23 16:00:10

标签: coldfusion

我有一个查询;

    <cfquery datasource="forms" name="departments">
        select * from quizes.tb_depts
    </cfquery>

这将返回结果集。我想访问一个特定的结果集,其中该行的ID(主键)是我输入的内容。

示例部门[13]应该在ID = 13的结果集中得到结果。

有内置的方法吗?我看到departments.id [rownumber]这不是我想要的。

谢谢

5 个答案:

答案 0 :(得分:5)

您需要做几件事:

首先,让我们将您的ID分配给变量,因为每次执行此查询时它可能都不一样:

<cfset departmentID = 13 >

现在按如下方式调整<cfquery>块:

<cfquery datasource="forms" name="departments">
    select *
    from quizes.tb_depts
    where id = <cfqueryparam value="#departmentID#" cfsqltype="cf_sql_numeric">
</cfquery>

注意:作为最佳做法,您需要在<cfqueryparam>中确定变量的范围。对于我在这里完成的简单变量赋值,它将是#VARIABLES.departmentID#。如果此查询位于将ID作为参数传递的函数中,则它将为#ARGUMENTS.departmentID#

答案 1 :(得分:3)

这是我最终做的事情。我道歉我的措辞不明确。

<cfoutput>
    <cfquery datasource="cehssmforms" name="departments">
        select id, dept 
        from cehssmquizes.tb_depts 
        Order by id
    </cfquery>

    <cfset d = structNew()/>
    <cfloop query="departments">
        #departments.id# - #departments.dept# <br>
        <cfset d[departments.id] = departments.dept > 
    </cfloop>

    <cfdump var="#d#" >
    #d[2]#
</cfoutput>

我有1个查询部门,可以获取所有部门信息。我希望所有这些打包在一个关联数组/结构中,这样我就可以通过id列访问信息。

所以例如

ID名称

12 ABC

22 EFC

33 GTF

当我执行读取查询时,我想只获取所有记录一次,并在项目的不同位置使用它们,因为我需要它们。

d [12] = ABC

d [33] = GTF

我希望它清楚。我为这种困惑道歉

答案 2 :(得分:1)

这可能是另一种解决方法:

<!--- Query Object --->
<cfset departments = queryNew(
    "dept_ID, dept_name",
    "INTEGER, VARCHAR,",
    [
        { dept_ID: 9, dept_name: "Department 9" },
        { dept_ID: 13, dept_name: "Department 13" },
        { dept_ID: 22, dept_name: "Department 22" },
        { dept_ID: 55, dept_name: "Department 55" }
    ]
)>

<!--- Get a comma delimited list of ID's --->
<cfset listOfID = valueList(departments.dept_ID, ",")>

<!--- Your required ID --->
<cfset requiredID = 13>

<!--- Get its index in list. ListFind will return 0 if it is not found. --->
<cfset indexOfID = listFind(listOfID, requiredID)>

<!--- Found --->
<cfif indexOfID>
    <cfdump var="#departments.dept_name[indexOfID]#">
</cfif>

答案 3 :(得分:0)

映射是你最好的朋友,并且明显比查询查询更快。

<!--- make up some data --->
<cfset departments = queryNew(
    "ID     , Name   , Staff",
    "INTEGER, VARCHAR, INTEGER",
    [
        { ID: 12, Name: "Department A", Staff: 20 },
        { ID: 14, Name: "Department B", Staff: 22 },
        { ID: 33, Name: "Department C", Staff: 17 }
    ]
)>

<!--- map primary key from result set --->
<cfset departmentsMap = {}>
<cfloop query="departments">
    <cfset departmentsMap[departments.ID] = departments.currentRow>
</cfloop>

<!--- let's pick some random departments --->
<cfset depToPick = [ 33, 12, 77, 14 ]>

<cfloop array="#depToPick#" index="depID">

    <!--- skip departments that are not in the result set --->
    <cfif not structKeyExists(departmentsMap, depID)>
        <cfcontinue>
    </cfif>

    <!---
        fetch fields to display using the query's [column][row] accessor,
        departmentsMap[depID] returns the row index that corresponds to the the mapped primary key
    --->
    <cfset depName  = departments["Name"][departmentsMap[depID]]>
    <cfset depStaff = departments["Staff"][departmentsMap[depID]]>

    <cfoutput>#depName# (ID: #depID#) has a total of #depStaff# staff members.<br></cfoutput>

</cfloop>

答案 4 :(得分:-1)

我会使用查询查询

return None

仅仅是其他一些注意事项,由于多种原因在选择查询中使用*非常糟糕......

  1. 您可能需要在结果集中返回更多数据,从而增加网络流量和内存使用量

  2. ODBC有一个有趣的错误,它在使用*时缓存列的顺序,如果向该表添加一列,则会导致一些非常奇怪的结果*

  3. 我确信还有更多理由,所以我真的会考虑撤消......