2017-01-16 81 views
0

我有兩個查詢,每個查詢輸出在單獨的表中的屏幕上。兩個quires都有相同數量的記錄,但我想檢查每個字段是否相等,如果不是,我想爲該表格行設置不同的背景顏色。這裏是我到目前爲止的代碼:比較兩個錶行值ColdFusion?

<cfquery name="oldData" datasource="test"> 
    SELECT 
     old_id, 
     old_userid, 
     old_first, 
     old_last, 
     old_dob 
    FROM OldTest 
</cfquery> 

<cfquery name="newData" datasource="test"> 
    SELECT 
     new_id, 
     new_userid, 
     new_first, 
     new_last, 
     new_dob 
    FROM NewTest 
</cfquery> 

這裏是兩個表代碼:

<table> 
     <tr> 
      <td valign="top"> 
       <table class="oldData"> 
        <caption>Old Data</caption> 
        <thead> 
         <tr> 
          <th>ID</th> 
          <th>UserID</th> 
          <th>First</th> 
          <th>Last</th> 
          <th>DOB</th> 
         <tr> 
        </thead> 
        <tbody> 
         <cfoutput query="oldData"> 
          <tr> 
           <td>#old_id#</td> 
           <td>#Trim(old_userid)#</td> 
           <td>#Trim(old_first)#</td> 
           <td>#Trim(old_last)#</td> 
           <td>#Trim(old_dob)#</td> 
          </tr> 
         </cfoutput> 
        </tbody> 
       </table> 
      </td> 
      <td valign="top"> 
       <table class="newData"> 
        <caption>New Data</caption> 
        <thead> 
         <tr> 
          <th>ID</th> 
          <th>UserID</th> 
          <th>First</th> 
          <th>Last</th> 
          <th>DOB</th> 
         <tr> 
        </thead> 
        <tbody> 
         <cfoutput query="newData"> 
          <tr> 
           <td>#new_id#</td> 
           <td>#Trim(new_userid)#</td> 
           <td>#Trim(new_first)#</td> 
           <td>#Trim(new_last)#</td> 
           <td>#Trim(new_dob)#</td> 
          </tr> 
         </cfoutput> 
        </tbody> 
       </table> 
      </td> 
     </tr> 
    </table> 

我不知道我是否應該輸出像之前比較數據行:

<cfif oldData.old_first[currentrow] NEQ newData.new_first[currentrow]> 
    <cfset rowColor = "red"> 
</cfif> 

或者有更好的方法來解決這個問題。如果有人知道更好的方式,請讓我知道。

回答

0

我建議查詢類似這樣的查詢:

select 'same' comparison, field1, field2, etc 
from query1, query2 
where query1.someField = query2.someField 
union 
select 'different' comparison, field1, field2, etc 
from query1 
where somefield not in (<cfqueryparam value="#valueList(query2.someField#") list = true> 

你的錶行的顏色可以用三元操作符來指定。

<tr bgcolor="#comparison is 'same' ? 'white' : 'red'#"> 
+0

我不確定我是否理解你的例子中的同情心是如何運作的。你能解釋一下嗎? –

+0

您是指Q的Q還是''標籤? –

+0

tr標籤在上面的答案中。 –

1

我會用循環:

<cfloop from="1" to="#oldData.recordCount#" index="r"> 
    Question #oldData.old_id[r]# = #oldData.new_id[r]#? Answer: #oldData.old_id[r] eq oldData.new_id[r]#<br> 
</cfloop> 

如果你希望你可以環繞紅素和TDS循環。

該方法還消除了對資源密集型QofQ的需求。