gpt4 book ai didi

mysql - 添加键的值

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:16 24 4
gpt4 key购买 nike

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

enter image description here

我的数据库架构如下:

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

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

到目前为止,我一直在通过以下方式(如 Insert 语句所示)非常简单地访问键:

 <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> ,

上面的东西是行不通的。另外,由于日期现在是可变的,我应该如何处理它?<​​/p>

这是我的 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>

请让我知道我在这里做错了什么。谢谢

截至 2 月 7 日的更新问题

当我使用以下命令时,又出现一个错误:

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 没有链接访问转换后的日期数组,因为 Structkeyexists 在 cfif 检查中存在,但即使像下面这样单独测试它也会产生相同的错误:

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

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

</cfloop>

最佳答案

所以首先遍历你的结构。其次检查该结构元素的内容本身是否是结构(因此我们可以消除“日期”键)。然后循环遍历该结构,将其键的值加起来。也许有点像这样。我使用了一个单独的结构来保存总数。然后,我将仅遍历该结构以更新数据库。

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

关于mysql - 添加键的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21570152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com