当需要 var 范围时,如何在 n 条记录集上使用查询的查询 UNION?
我希望能够对未知数量的记录集进行 UNION 查询。 但是,在执行查询查询时,记录集名称中不允许使用点或括号。
例如,这会失败:
<cfquery name="allRecs" dbtype="query">
SELECT * FROM recordset[1]
UNION
SELECT * FROM recordset[2]
</cfquery>
使用诸如“recordset1”之类的动态变量名称可以工作,但这是在函数中并且需要在 var 范围内,因此我无法动态构建变量名称而不在持久对象中产生内存泄漏。
还有其他想法吗?
I would like to be able to do a query of a query to UNION an unknown number of recordset. However when doing a query-of-query dots or brackets are not allowed in record set names.
For example this fails:
<cfquery name="allRecs" dbtype="query">
SELECT * FROM recordset[1]
UNION
SELECT * FROM recordset[2]
</cfquery>
Using dynamic variable names such as "recordset1" work but this is in a function and needs to be var-scoped so I can't build up the variable names dynamically without producing memory leaks in a persisted object.
Any other ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
困难的任务。 我可以想象一个基于
GetColumnNames()
的嵌套循环解决方案,使用QueryAddRow()
和QuerySetCell()
。 它不会是最有效的,但也不是很慢。 当然,这取决于任务的大小。当您创建“创建一个组合两个记录集的函数”来接受例如十个参数时,您的“创建一个组合两个记录集的函数”可以变得更加高效。 即时修改 SQL:
Difficult task. I could imagine a solution with a nested loop based on
GetColumnNames()
, usingQueryAddRow()
andQuerySetCell()
. It won't be the most efficient one, but it is not really slow. Depends on the size of the task, of course.Your "create a function that combines two recordsets" could be made much more efficient when you create it to accept, say, ten arguments. Modify the SQL on the fly:
经过快速浏览后,我发现了这一点:
CFLib.org 上的 queryConcat。 它使用 queryaddrow/querysetcell 连接两个查询。
我添加了一个快速功能(没有错误检查或数据验证,所以我不会按原样使用它):
作为测试,我将其放在一起:
事实上,这确实给了你 fred/sammy/fred。
它可能不是最有效的实现,但如果您愿意,您可以随时更改插入/联合代码以使其更快。 大多数情况下,我的目标是自己编写尽可能少的代码。 :-)
After a quick bit of poking around, I found this:
queryConcat at CFLib.org. It uses queryaddrow/querysetcell to concatenate two queries.
I added a quick function (with no error checking, or data validation, so I wouldn't use it as-is):
As a test, I threw this together:
Which does, in fact, give you fred/sammy/fred.
It's probably not the most efficient implementation, but you can always alter the insert/union code to make it faster if you wanted. Mostly, I was aiming to write as little code as possible by myself. :-)
此处添加的所有解决方案都应该适合您,但我还要提到,根据您正在使用的数据量和您正在使用的数据库,您最好尝试找到一种在数据库端执行此操作的方法。 对于非常大的记录集,将记录写入临时表并再次选择它们可能是有益的,但无论哪种方式,如果您可以以任何方式重写查询以让数据库首先处理这个问题,您将过得更好。
all of the solutions added here should work for you, but I would also mention that depending on how much data you are working with and the database you are using, you might be better off trying to find a way to do this on the database side. With very large record sets, it might be beneficial to write the records to a temporary table and select them out again, but either way, if you can in any way rewrite the queries to let the database handle this in the first place you will be better off.
发布问题后,我想出了几个解决方案,但可能有更好的解决方案
我可以将动态命名的变量写入参数范围,然后在查询中引用它们,而无需它们的范围
After posting the question I came up with a couple solutions but there might be a better one out there
I could write dynamically named variables to the arguments scope and then reference them without their scope in query
Create a function that accepts 2 recordsets as arguments and returns one combined recordset. This could be looped over to progressively add a recordset at a time. I'm sure this is very inefficient compared to doing all UNIONs in one query though.