是否可以运行SQL查询一次并包括详细输出和按类别计数?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了是否可以运行SQL查询一次并包括详细输出和按类别计数?相关的知识,希望对你有一定的参考价值。
是否可以运行一次SQL并显示详细输出+名称数量?下面的代码按照我的意愿工作,但它运行两个查询,所以这可能不是那么有效..
<cfquery name="myta" datasource="zett">
Select w.t_firstname, w.t_lastname, w.t_total
from table_bst a, table_zr w
where 1=1
....
</cfquery>
<cfquery name="meto" datasource="zett">
SELECT a.t_firstname as Firstname, COUNT(*) AS Status
from table_bst a, table_zr w
where 1=1
....
</cfquery>
<cfif meto.recordcount EQ 0>
<table><tr><td style="color:#FF0000">There is currently an error</td></tr></table>
<cfelse>
<cfoutput>
<table>
<cfloop query="meto">
<cfset temp = ValueList(myta.status)
<tr><td>FirstnameList: <cfoutput>#ListLen(temp)#</cfoutput></td></tr>
</table>
</cfoutput>
</cfif>
答案
以完成您正在寻找的内容的最简单且以ColdFusion为中心的方法是简单地使用查询查询:
主要问题:
<cfquery name="myta" datasource="zett">
SELECT w.t_firstname, w.t_lastname, w.t_total
FROM table_bst a
INNER (OR LEFT/RIGHT OUTER) JOIN table_zr w ON a.? = w.?
WHERE
[
1=1 is only really useful if you have some sort of if/else logic in your
WHERE clause, and it's used to make sure there is a value there if none
of the if/else conditions are met.
]
AND/OR [other stuff]
</cfquery>
查询查询:
<cfquery name="meto" dbtype="query">
SELECT t_firstname AS FirstName, count(*) AS Status
FROM myta
GROUP BY FirstName
</cfquery>
OUTPUT:
<table>
<cfif meto.recordcount EQ 0>
<tr><td style="color:#FF0000">There is currently an error</td></tr>
<cfelse>
<cfoutput query="meto">
<tr>
<td>#Firstname#:#Status#</td>
</tr>
</cfoutput>
</cfif>
</table>
https://trycf.com/gist/529c0bf10b7c6d7420eae399572744bb/lucee5?theme=monokai
以上是关于是否可以运行SQL查询一次并包括详细输出和按类别计数?的主要内容,如果未能解决你的问题,请参考以下文章