Oracle coldfusion将两个查询的结果合并在一起
我有两个coldfusion查询,如下所示,返回不同的列和日期列Oracle coldfusion将两个查询的结果合并在一起,oracle,coldfusion,Oracle,Coldfusion,我有两个coldfusion查询,如下所示,返回不同的列和日期列 <cfquery name="qry1" datasource="test"> select title, name, id, test_date from table1 , table2 </cfquery> <cfquery name="qry2" datasource="test"> select headline, itemid, create_dt from table3 , tab
<cfquery name="qry1" datasource="test">
select title, name, id, test_date from table1 , table2
</cfquery>
<cfquery name="qry2" datasource="test">
select headline, itemid, create_dt from table3 , table4
</cfquery>
我想连接这两个查询的结果,在最后一个查询中,结果需要按date desc排序注意:两个查询中返回的日期是两个不同的列。我知道可以选择合并所有这两个查询,我不想使用它,因为它会减慢请求的速度。有没有其他方法来实现这一点的想法
<CFQUERY NAME="getDetails" DBTYPE="query">
SELECT emp_id, url_shortcut, title, name, join_dt
,'' as item_id,'' as batch_id, '' as item_text
FROM get_related_info_one
UNION ALL
SELECT to_number('') as emp_id, '' as url_ shortcut, '' as title, '' as name
,item_date as join_dt, item_id, batch_id, item_text
FROM get_related_info_two
</CFQUERY>
我得到一个错误:
查询语法错误。
遇到。
Select语句不正确,
应为“发件人”,但实际遇到,
select语句应具有“FROM”构造。
你知道我错过了什么吗 只要列类型相同,就可以这样合并它们
<cfquery name="qry1" datasource="test">
select title, name, id, test_date
from table1 , table2
UNION
select headline, '' as name, itemid, create_dt
from table3 , table4
ORDER BY test_date
</cfquery>
如果您正在寻找一种基于CF的方法,尤其是在使用内存查询时,Ben Nadel编写了一个QueryAppend UDF 我最近对它做了一些调整,这样ColdFusion处理为空的空日期就不会被错误地转换
我是说。。。除了一次而不是两次访问db之外,任何其他操作都可能会减慢请求速度,您必须使用sql或循环将它们合并或加入,实际上您不能使用联合查询,因为query1返回4列,query2返回3列。你说你想加入结果。以什么方式?@DanBracuk当然可以,只需创建一个别名来表示不在另一个表中的列它们是不同的列类型尝试解决的错误很少如果它们是不同的类型,您可以使用casttitle作为varchar或类似的东西note:您可能需要为不匹配的列名使用别名,以便它们在两个查询之间保持一致。我通常将“headline”添加为记录类型,将headline添加为标题,将“Title”添加为记录类型,标题。如果它们不使用相同的名称和数据类型,您将无法组合它们。
<!--- 7/5/2006 QueryAppend By Ben Nadel https://www.bennadel.com/blog/114-coldfusion-queryappend-qone-qtwo.htm
1/5/2017 New "EmptyAsNull" option to prevent NULL values (dates & numbers) from being incorrectly recast
to an invalid "empty string" by ColdFusion's Query-of-Queries and throwing "Error casting an object
of type to an incompatible type" error. --->
<cffunction name="QueryAppend" access="public" returntype="void" output="false" hint="This takes two queries and appends the second one to the first one. This actually updates the first query and does not return anything.">
<cfargument name="QueryOne" type="query" required="true">
<cfargument name="QueryTwo" type="query" required="true">
<cfargument name="EmptyAsNull" default="" required="false">
<cfset var LOCAL = StructNew()>
<cfset LOCAL.Columns = ListToArray(ARGUMENTS.QueryOne.ColumnList)>
<cfset LOCAL.EmptyAsNull = 0>
<cfif isValid("boolean", ARGUMENTS.EmptyAsNull) AND ARGUMENTS.EmptyAsNull>
<cfset LOCAL.EmptyAsNull = 1>
</cfif>
<cfloop query="ARGUMENTS.QueryTwo">
<cfset QueryAddRow(ARGUMENTS.QueryOne)>
<cfloop ARRAY="#LOCAL.Columns#" index="LOCAL.ColumnName">
<cfif StructKeyExists(ARGUMENTS.QueryTwo, LOCAL.ColumnName) AND (NOT LOCAL.EmptyAsNull OR LEN(ARGUMENTS.QueryTwo[LOCAL.ColumnName][ARGUMENTS.QueryTwo.CurrentRow]))>
<cfset ARGUMENTS.QueryOne[LOCAL.ColumnName][ARGUMENTS.QueryOne.RecordCount] = ARGUMENTS.QueryTwo[LOCAL.ColumnName][ARGUMENTS.QueryTwo.CurrentRow]>
</cfif>
</cfloop>
</cfloop>
<cfreturn>
</cffunction>