Home  >  Article  >  Backend Development  >  mysql - 一个计算php结果的问题。求大牛,求高手

mysql - 一个计算php结果的问题。求大牛,求高手

WBOY
WBOYOriginal
2016-06-06 20:23:151078browse

<code>//从part_time数据库中查找
$sql="select * from part_time where agents=6";
$result=mysql_query($sql);

while($row=mysql_fetch_assoc($result)){
    $id=$row['id'];//循环出所有agents=2的id
    
    $resu="select count(*) from userinfo where part_person=$id";
    $re=mysql_query($resu);
    $roo=mysql_fetch_assoc($re);
    $number= $roo['count(*)']; //计算出userinfo中是相同兼职人员(part_time)的人数
    echo $number;
    echo "----";
}
//现在需要把$number 相加得到最终的数字。应该怎么做?求大牛解答</code>

回复内容:

<code>//从part_time数据库中查找
$sql="select * from part_time where agents=6";
$result=mysql_query($sql);

while($row=mysql_fetch_assoc($result)){
    $id=$row['id'];//循环出所有agents=2的id
    
    $resu="select count(*) from userinfo where part_person=$id";
    $re=mysql_query($resu);
    $roo=mysql_fetch_assoc($re);
    $number= $roo['count(*)']; //计算出userinfo中是相同兼职人员(part_time)的人数
    echo $number;
    echo "----";
}
//现在需要把$number 相加得到最终的数字。应该怎么做?求大牛解答</code>

while前面定义一个$number,然后直接$number+=$row['count(*)']即可。

另外我将代码给你精简了下。。。

<code class="php">$sql = 'select count(*) as total from userinfo where part_person IN (select id from part_time where agents = 6)';
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
echo $row['total'];</code>

定义个$sum=0;
while中加上$sum+=$number

$number += $roo['count(*)'];

哇塞,循环语句里你这样反复调用数据库,访问量大估计数据库会受不了。可以使用join查询。

试试 aggregate

<code>$number += $roo['count(*)'];</code>

不过这代码有点。。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn