添加键的值

时间:2014-02-05 06:30:30

标签: mysql coldfusion

请考虑以下结构数组的快照:

enter image description here

我的数据库架构如下:

http://sqlfiddle.com/#!2/25eeb

我想计算结构键值的总和,并将总数存储到数据库中。例如,我想在阻塞列下存储146 + 1 = 147值,类似地,在弹跳列等中存储4 + 1 + 2 = 7。

到目前为止,我一直在以下列方式访问非常简单的密钥(如插入语句中所示):

 <cfquery datasource="XX.XX.X.XX" name="qCoulmnInsert">
INSERT INTO 
             mytable (blocked_int,
                      bounce_int,
                      date_dt,
                      deferred_int,
                      delivered_int,
                       drop_int                                                                                                                      

                                                                        )

  VALUES
              <!--- loop through your array --->
             <cfloop from="1" to="#arrayLen(cfData)#" index="i">
             ( <!--- 1 --->
              <cfif structKeyExists(cfData[i], "blocked")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["blocked"]["Other"]#">
              <cfelse>
                 NULL

              </cfif> ,
              <!--- 2 --->
              <cfif structKeyExists(cfData[i], "bounce")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["bounce"]["At & T"]#">
              <cfelse>
                 NULL
              </cfif> ,

我想知道,有没有办法总结按键的值,因为从图像中可以看出,按键不是固定的,因为阻止,反弹,延迟等等。

请告知。

错误:

运行下面提到的方法后,我得到以下错误:

The value Cox cannot be converted to a number.

The error occurred in C:myfile.cfm: line 38

36 :             <cfset total = 0>
37 :             <cfloop item="subkey" collection="#struct[key]#">
38 :                 <cfset total += subkey>
39 :             </cfloop>
40 :             <cfset totals[key] = total>

不确定Cox在这里是什么?

另外,如果我在INSERT语句的上下文中讨论,我将如何在<cfqueryparam>标签周围进行调整,我必须在其中定义总值?

请告知。

进一步问题:

好的,基于下面提到的例子我有以下输出:

enter image description here

我想在数据库的单个列中插入日期和值的值。 我想在所有日期重复相同的事情。

所以这就是我试图做的事情:

<cfquery datasource="10.11.0.130" name="qCoulmnInsert">
INSERT INTO 
   simplexresults.contactresults_email_account_summary_isps     (date_dt,
                                                                                                     blocked_int,
                                                                 bounce_int,
                                                                 deferred_int,
                                                                 delivered_int,
                                                                 drop_int,
                                                                 open_int,
                                                                 processed_int,
                                                                 request_int,
                                                                 spamreport_int,  
                                                                 uniqueopen_int
                                                                                                                                                              )

  VALUES
              <!--- loop through your structure --->
             <cfloop from="1" to="#StructCount(totals)#" index="i">
             ( <!--- 1 --->
              <cfif structKeyExists(totals[i], "blocked")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#totals[i].blocked#">
              <cfelse>
                 NULL

              </cfif> ,
              <!--- 2 --->
              <cfif structKeyExists(totals[i], "bounce")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#totals[i]["bounce"]">
              <cfelse>
                 NULL
              </cfif> ,

以上的事情不起作用。此外,由于日期现在可变,我该如何处理呢?

这是我的sqlfiddle:

http://sqlfiddle.com/#!2/d135f

请告知。

更新的问题:

我在“VALUES”子句之后使用了以下代码并收到一个错误:

VALUES

<cfloop collection="#totals#" item="theDate">
             ( 





              <!--- 1 --->
             <cfif structKeyExists(totals[theDate], "blocked")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#totals[theDate].blocked#">
              <cfelse>
                 NULL

              </cfif> ,

              <!--- 2 --->
              <cfif structKeyExists(totals[theDate], "bounce")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#totals[theDate].bounce#">
              <cfelse>
                 NULL
              </cfif> ,


              <!--- 3 --->
              <cfif structKeyExists(totals[theDate], "deferred")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#totals[theDate].deferred#">
               <cfelse>
                 NULL
              </cfif> ,

              <!--- 4 --->
              <cfif structKeyExists(totals[theDate], "delivered")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#totals[theDate].delivered#">
              <cfelse>
                 NULL
              </cfif> ,

              <!--- 5 --->
              <cfif structKeyExists(totals[theDate], "drop")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#totals[theDate].drop#">
               <cfelse>
                 NULL
              </cfif> ,

              <!--- 6 --->
              <cfif structKeyExists(totals[theDate], "open")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#totals[theDate].open#">
               <cfelse>
                 NULL
              </cfif> ,

              <!--- 7 --->
              <cfif structKeyExists(totals[theDate], "processed")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#totals[theDate].processed#">
              <cfelse>
                 NULL
              </cfif> ,

              <!--- 8 --->
              <cfif structKeyExists(totals[theDate], "request")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#totals[theDate].request#">
              <cfelse>
                 NULL
              </cfif> ,

              <!--- 9 --->
              <cfif structKeyExists(totals[theDate], "spamreport")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#totals[theDate].spamreport#">
              <cfelse>
                 NULL
              </cfif> ,

              <!--- 10 --->
              <cfif structKeyExists(totals[theDate], "unique_open")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#totals[theDate].unique_open#">
              <cfelse>
                 NULL
              </cfif> ,




              <!--- FOR DATES --->
              <cfif structKeyExists(totals[theDate], "startdate_dt")>
                <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#totals[theDate].startdate_dt#">
              <cfelse>
                 NULL
              </cfif>, 



              <cfif structKeyExists(totals[theDate], "enddate_dt")>
                <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#totals[theDate].enddate_dt#">
              <cfelse>
                 NULL
              </cfif>,



              <cfif structKeyExists(totals[theDate], "date")>
                <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#totals[theDate].date#">
              <cfelse>
                 NULL
              </cfif>  

              )

             <!---  <cfif i neq arrayLen(cfData)>,</cfif> --->
          </cfloop>

以下是我遇到的错误:

Error Executing Database Query.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( ' at line 86

The error occurred in C:\myfile.cfm: line 67
Called from C:\myfolder\application.cfc: line 211
Called from C:\myfile.cfm: line 67
Called from C:\myfoldert\application.cfc: line 211

65 :   VALUES
66 :               <!--- loop through your array --->
67 :              <cfloop collection="#totals#" item="theDate">
68 :              ( 
69 :              

我不知道为什么它会在#67行引发错误。因为,当我测试它如下, 我的输出没有任何错误:

<cfloop collection="#totals#" item="theDate">


Blocked:<cfoutput>#totals[theDate].blocked#</cfoutput>
Bounce: <cfoutput>#totals[theDate].bounce#</cfoutput>


</cfloop>

请告诉我这里我做错了什么。感谢

截至2007年2月更新的问题

当我使用以下内容时,我又收到一个错误:

VALUES

<cfloop from = "1" to="#arrayLen(dateArray)#" index="i">
             ( 

          <!--- get current date --->

         <cfset theDate = dateArray[i]>

         <!--- append a comma in between value sets --->

         <cfif i gt 1>,</cfif>



              <!--- 1 --->
             <cfif structKeyExists(dateArray[i], "blocked")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#dateArray[i].blocked#">
              <cfelse>
                 NULL

              </cfif> ,

              <!--- 2 --->
              <cfif structKeyExists(dateArray[i], "bounce")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#dateArray[i].bounce#">
              <cfelse>
                 NULL
              </cfif> ,


              <!--- 3 --->
              <cfif structKeyExists(dateArray[i], "deferred")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#dateArray[i].deferred#">
               <cfelse>
                 NULL
              </cfif> ,

              <!--- 4 --->
              <cfif structKeyExists(dateArray[i], "delivered")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#dateArray[i].delivered#">
              <cfelse>
                 NULL
              </cfif> ,

              <!--- 5 --->
              <cfif structKeyExists(dateArray[i], "drop")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#dateArray[i].drop#">
               <cfelse>
                 NULL
              </cfif> ,

              <!--- 6 --->
              <cfif structKeyExists(dateArray[i], "open")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#dateArray[i].open#">
               <cfelse>
                 NULL
              </cfif> ,

              <!--- 7 --->
              <cfif structKeyExists(dateArray[i], "processed")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#dateArray[i].processed#">
              <cfelse>
                 NULL
              </cfif> ,

              <!--- 8 --->
              <cfif structKeyExists(dateArray[i], "request")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#dateArray[i].request#">
              <cfelse>
                 NULL
              </cfif> ,

              <!--- 9 --->
              <cfif structKeyExists(dateArray[i], "spamreport")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#dateArray[i].spamreport#">
              <cfelse>
                 NULL
              </cfif> ,

              <!--- 10 --->
              <cfif structKeyExists(dateArray[i], "unique_open")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#dateArray[i].unique_open#">
              <cfelse>
                 NULL
              </cfif> ,




              <!--- FOR DATES --->
              <cfif structKeyExists(dateArray[i], "startdate_dt")>
                <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#dateArray[i].startdate_dt#">
              <cfelse>
                 NULL
              </cfif>, 



              <cfif structKeyExists(dateArray[i], "enddate_dt")>
                <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#dateArray[i].enddate_dt#">
              <cfelse>
                 NULL
              </cfif>,



              <cfif structKeyExists(dateArray[i], "date")>
                <cfqueryparam CFSQLTYPE="CF_SQL_DATE" value="#dateArray[i].date#">
              <cfelse>
                 NULL
              </cfif>  

              )

             <!---  <cfif i neq arrayLen(cfData)>,</cfif> --->
          </cfloop>

以下是错误

您试图将类java.lang.String类型的标量变量取消引用为具有成员的结构。

The error occurred in C:myfile.cfm: line 94

92 :               <!--- 1 --->
93 :               <!--- <cfif isStruct("blocked")> --->
94 :              <cfif structKeyExists(dateArray[i], "blocked")>
95 :                 <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#dateArray[i].blocked#">
96 :               <cfelse>

我在这里做错了吗?我假设coldfusion没有链接访问转换日期数组作为cfif检查中的Structkeyexists,但即使单独测试它,如下所示产生相同的错误:

<cfloop from = "1" to="#arrayLen(dateArray)#" index="i">

<cfoutput>#dateArray[i].blocked#</cfoutput>

</cfloop>

1 个答案:

答案 0 :(得分:3)

首先循环结构。其次检查该struct元素的内容本身是否为struct(因此我们可以删除'date'键)。然后循环遍历该结构,总计其键的值。也许有点像这样的东西。我使用了一个单独的结构来保存总数。然后我只循环该结构来更新数据库。

<cfset totals = {}>

<cfloop index="struct" array="#cfData#">
    <cfset totals[struct.date] = {}>
    <cfloop item="key" collection="#struct#">
        <cfif isStruct(struct[key])>
            <cfset total = 0>
            <cfloop item="subkey" collection="#struct[key]#">
                <cfset total += struct[key][subkey]>
            </cfloop>
            <cfset totals[struct.date][key] = total>
        </cfif>
    </cfloop>
</cfloop>