Mysql 只输出最后循环数据
Posted
技术标签:
【中文标题】Mysql 只输出最后循环数据【英文标题】:Mysql only outputs last loops data 【发布时间】:2013-05-30 16:14:34 【问题描述】:我正在尝试输出当天呼叫的所有拨号活动中座席呼叫的统计信息。一天内拨打了多个广告系列,目前为 3 个。我从一个表名中获取活动数据,该表名会随活动而变化,例如 custom_16546546 和 custom_1564654。我运行一个查询来获取列名的数字部分,然后将其作为变量传递给另一个输出用户统计信息的查询。这是它只显示来自我可以告诉的最后一个查询循环信息的数据的地方。我已经显示了第一个查询中的数组数据,以确认正在存储表名并且看起来很好。
$today = "2013-05-29";
$customquery = "SELECT distinct( entry_list_id) FROM vicidial_list where entry_list_id > 0 and last_local_call_time between '$today 00:00:00' and '$today 23:59:59';" ;
$customresult = mysql_query($customquery) or die(mysql_error());
$customrows = mysql_num_rows($customresult);
while ($customrowResult = mysql_fetch_array($customresult))
$customID[] = $customrowResult["entry_list_id"];
$z = 0;
if(!$customID)
//echo " Please Select a valid date range: ".$today. " - ".$today." is not valid" ;
else
while($z<$customrows)
$query = "SELECT
vicidial_users.user,
vicidial_users.full_name,
vicidial_log.list_id,
COUNT(CASE WHEN (vicidial_log.status = 'SALE') THEN 1 ELSE null END) as sumcountSamnt,
COUNT(CASE WHEN (vicidial_log.status = 'SALE' AND custom_$customID[$z].up_amt <> '') THEN 1 ELSE null END) as sumcountupAmnt,
COUNT(CASE WHEN (vicidial_log.status = 'SALE' AND custom_$customID[$z].cc_num <> '') THEN 1 ELSE null END) as sumccverifiedcountAmnt,
COUNT(CASE WHEN (vicidial_log.status = 'SALE' AND custom_$customID[$z].bank_act <> '') THEN 1 ELSE null END) as sumbankverifiedcountAmnt,
SUM(CASE WHEN (vicidial_log.status = 'SALE' AND custom_$customID[$z].cc_num <> '') THEN custom_$customID[$z].s_amount ELSE null END) as sumccverifiedAmnt,
SUM(CASE WHEN (vicidial_log.status = 'SALE' AND custom_$customID[$z].bank_act <> '') THEN custom_$customID[$z].s_amount ELSE null END) as sumbankverifiedAmnt,
SUM(CASE WHEN (vicidial_log.status = 'SALE') THEN custom_$customID[$z].d_amt ELSE null END) as sumDamnt,
SUM(CASE WHEN (vicidial_log.status = 'SALE') THEN custom_$customID[$z].up_amt ELSE null END) as sumUpamnt,
SUM(CASE WHEN (vicidial_log.status = 'SALE') THEN custom_$customID[$z].md_amt ELSE null END) as sumMdamnt,
SUM(CASE WHEN (vicidial_log.status = 'SALE') THEN custom_$customID[$z].s_amount ELSE null END) as sumSamnt
FROM
vicidial_log
INNER JOIN
vicidial_users
ON
(vicidial_log.user = vicidial_users.user)
INNER JOIN
custom_$customID[$z]
ON
(vicidial_log.lead_id = custom_$customID[$z].lead_id)
WHERE
call_date
BETWEEN
'$today 00:00:00'
AND
'$today 23:59:59'
AND
vicidial_log.user != 'VDAD'
AND
vicidial_users.user_group != 'TX-Training'
GROUP BY
vicidial_log.user, vicidial_log.campaign_id
";
//This query is used to sum loop data data
$queryResult = mysql_query($query) or die(mysql_error()); //This line perfomrs query error handling
$z++;
这是数据显示方式的 sn-p
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result))
echo "<tr>\n";
//--fullname and userID
echo "<td> ".$row["full_name"]. " - ".$row["user"]."</td>\n";
//Agent total sales amount
if ($row["sumcountSamnt"] == '0')
echo "<td>$nosalestxt</td>\n";
else
echo "<td> $".$row["sumSamnt"]."</td>\n";
所以我希望能够显示每个用户从他们拨打的所有广告系列中获得的总数据。
提前感谢您的帮助
编辑:我使用 while 循环中的 &row 来输出数据,是的,克里斯我想将它存储为一个数组,但我想我只是错过了它,再次感谢
【问题讨论】:
你期待什么?您运行那个巨大的查询,获取结果,然后立即再次循环。你不处理数据,你不输出它。所以最后,你会得到循环的 LAST 迭代的结果...... 除了@MarcB 所说的,请不要再使用mysql_*
函数。它们不再被维护并且是deprecated as of php 5.5.0。阅读this 以获得更详细的解释。相反,请使用PDO 或MySQLi 并了解prepared statements。 This article 可以帮助您决定使用哪个 API。
【参考方案1】:
根据您提供的代码 sn-ps,您似乎在每次执行循环后覆盖 $queryResult 的值。也许您打算执行查询并将结果放入一个数组(或类似的东西)中以便循环并稍后显示。
我还注意到您在输出中引用了 $row[],您是如何为该变量赋值的?
【讨论】:
以上是关于Mysql 只输出最后循环数据的主要内容,如果未能解决你的问题,请参考以下文章
spring-data-jpa循环保存数据,为啥只保存了最后一条数据