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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:23:15
Original
1172 people have browsed it

<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>
Copy after login
Copy after login

回复内容:

<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>
Copy after login
Copy after login

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>
Copy after login

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

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

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

试试 aggregate

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

不过这代码有点。。

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
MySQL stops process
From 1970-01-01 08:00:00
0
0
0
Error when installing mysql on linux
From 1970-01-01 08:00:00
0
0
0
phpstudy cannot start mysql?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template