在循环内构建选择列表
如何不在循环内使用查询的查询来解决必须为每一行构建选择列表的问题?
在此示例中,每个客户都有一个该客户特有的操作列表:
<cfquery name="qryAction" datasource="myDataSource">
SELECT ActionID,CustID,ActionName FROM AvailableActions
</cfquery>
<cfquery name="qryOrderHeader" datasource="myDataSource">
SELECT CustID FROM OrderHeader
</cfquery>
<html>
<body>
<cfform preservedata="yes">
<cfloop query="qryOrderHeader">
<cfquery name="qry3" dbtype="query">
SELECT ActionID,ActionName FROM qryAction
WHERE CustID = #qryOrderHeader.CustID#
</cfquery>
<cfselect name="ActionID" query="qry3" display="ActionName" value="ActionID" />
</cfloop>
</cfform>
</body>
</html>
以下是 SQL(如果它有助于说明我的示例)。
use tempdb
GO
create table Cust(
CustID Int Identity Primary Key,
CustName Varchar(255)
)
GO
INSERT INTO Cust(CustName) values('One')
INSERT INTO Cust(CustName) values('Two')
GO
create table AvailableActions(
ActionID Int Identity Primary Key,
CustID Int,
ActionName Varchar(255)
)
GO
INSERT INTO AvailableActions(CustID,ActionName) VALUES(1,'Insert')
INSERT INTO AvailableActions(CustID,ActionName) VALUES(1,'Edit')
INSERT INTO AvailableActions(CustID,ActionName) VALUES(1,'Delete')
INSERT INTO AvailableActions(CustID,ActionName) VALUES(2,'Insert')
INSERT INTO AvailableActions(CustID,ActionName) VALUES(2,'Edit')
GO
CREATE TABLE OrderHeader(
OrderHeaderID Int Identity Primary Key,
CustID Int
)
INSERT INTO OrderHeader(CustID) VALUES(1)
INSERT INTO OrderHeader(CustID) VALUES(2)
INSERT INTO OrderHeader(CustID) VALUES(2)
我想最好的结果是在 qryOrderHeader 中包含 ActionID 和 ActionName。
How do you NOT use a query of queries inside a loop to solve the problem of having to build a select list for each row?
In this example, every customer has a list of actions unique to that customer:
<cfquery name="qryAction" datasource="myDataSource">
SELECT ActionID,CustID,ActionName FROM AvailableActions
</cfquery>
<cfquery name="qryOrderHeader" datasource="myDataSource">
SELECT CustID FROM OrderHeader
</cfquery>
<html>
<body>
<cfform preservedata="yes">
<cfloop query="qryOrderHeader">
<cfquery name="qry3" dbtype="query">
SELECT ActionID,ActionName FROM qryAction
WHERE CustID = #qryOrderHeader.CustID#
</cfquery>
<cfselect name="ActionID" query="qry3" display="ActionName" value="ActionID" />
</cfloop>
</cfform>
</body>
</html>
Here's the SQL if it helps illustrate my example.
use tempdb
GO
create table Cust(
CustID Int Identity Primary Key,
CustName Varchar(255)
)
GO
INSERT INTO Cust(CustName) values('One')
INSERT INTO Cust(CustName) values('Two')
GO
create table AvailableActions(
ActionID Int Identity Primary Key,
CustID Int,
ActionName Varchar(255)
)
GO
INSERT INTO AvailableActions(CustID,ActionName) VALUES(1,'Insert')
INSERT INTO AvailableActions(CustID,ActionName) VALUES(1,'Edit')
INSERT INTO AvailableActions(CustID,ActionName) VALUES(1,'Delete')
INSERT INTO AvailableActions(CustID,ActionName) VALUES(2,'Insert')
INSERT INTO AvailableActions(CustID,ActionName) VALUES(2,'Edit')
GO
CREATE TABLE OrderHeader(
OrderHeaderID Int Identity Primary Key,
CustID Int
)
INSERT INTO OrderHeader(CustID) VALUES(1)
INSERT INTO OrderHeader(CustID) VALUES(2)
INSERT INTO OrderHeader(CustID) VALUES(2)
I suppose the best result would be to include ActionID and ActionName in qryOrderHeader.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用 cfoutput 和 group 属性来手动构建选择列表(而不是使用 cfselect)非常简单:
请注意,您需要将 CustID 指定为第一个(或唯一)排序依据,以使嵌套的 cfoutput 正常工作。没有测试过,所以可能有错别字。
It's pretty straightforward to build the select lists manually (instead of using cfselect) by using cfoutput with the group attribute:
Note you need to specify the CustID as the first (or only) order by in order to make the nested cfoutputs work correctly. Not tested, so there may be typos.