2017-02-02 1 views
-1

다음과 같이 날짜 열과 함께 다른 열을 반환하는 두 가지 coldfusion 쿼리가 있습니다. (: 2 개 쿼리에서 반환 일이 개 다른 컬럼입니다 주) 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 , table4 
</cfquery> 
나는이 두 쿼리의 결과에 가입 할

최종 쿼리

결과가 날짜 내림차순으로 정렬 될 필요가있다. 나는이 두 가지 질의 모두에 대해 노동 조합의 옵션이 있다는 것을 알고 있으며 요청을 늦추기 때문에 그것을 사용하고 싶지 않습니다. 이것을 달성하기위한 다른 방법에 대한 아이디어.

  <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 문은 구조 'FROM'. 어떤 생각이 내가 무슨 말이냐해야한다 'FROM'가.

+1

내가 말은 ... DB를 (보다는 2 개)에 그것을 하나의 여행을 이외의 가능성을, 당신은 야해 조합 중 하나 "요청을 느리게"또는 결합 할 것입니다 SQL 또는 루핑 중 하나를 사용하는 경우 –

+0

실제로는 union 쿼리를 사용할 수 없습니다. 왜냐하면 query1은 4 개의 열을 반환하고 query2는 3을 반환하기 때문입니다. 결과에 참여한다고 말합니다. 어떤 방법으로? –

+1

@DanBracuk 다른 테이블에없는 컬럼을 나타 내기 위해 별칭을 만들 수 있습니다. –

답변

1

으로 기대 가 발생했습니다 "(. 잘못된 선택 문, 열 유형이 동일한만큼이 같은 UNION들을 수 있습니다.

<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> 
+0

다른 컬럼 타입이 있습니다. 해결하려고하는 에러가 거의 없습니다 – user747291

+0

'cast (title as varchar)'또는 비슷한 경우 사용할 수 있습니다 다른 유형 –

1

, 벤 나델 쓴 (인 - 메모리 쿼리의-쿼리를 사용하는 경우 특히) 당신이 CF 기반의 접근 방식을 찾고 있다면 QueryAppend UDF

https://www.bennadel.com/blog/114-coldfusion-queryappend-qone-qtwo.htm

NULL 날짜 (ColdFusion에서 "비어있는"것으로 간주)가 잘못 캐스팅되지 않도록 최근 수정했습니다.

https://gist.github.com/JamoCA/5a2adb52cbeb4e15337a7d899222072e

<!--- 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> 
+0

참고 : 일치하지 않는 열 이름에 별칭을 사용하여 두 쿼리간에 일관성을 유지해야 할 수도 있습니다. 나는 보통''Headline AS RecordType, Headline AS Title'과''Title ''AS RecordType, Title'을 추가합니다. 동일한 이름 (및 데이터 유형)을 사용하지 않는 경우 이들을 결합 할 수 없습니다. –

관련 문제