来自查询的列的SUM

时间:2014-01-22 12:51:27

标签: php sql

我正在尝试获取查询的1列的SUM。我希望在Ex,Est和WP匹配的地方以及Stat是有线,有绳或者SubDucted的地方。我的数据库如下所示

Ex          Est         WP      Sec     Len Stat    TM
Sacriston   IBGLEPTA    WP1-WP2 7901    7   Cabled  NoTM
Sacriston   IBGLEPTE    WP1-WP2 6182    80  Roped   NoTM
Sacriston   IBGLEPTE    WP1-WP2 7206    156 Cabled  NoTM
Sacriston   IBGLEPTE    WP1-WP2 7207    80  SubDucted   NoTM
Sacriston   IBGLEPTE    WP1-WP2 7207A   130 Cabled  NoTM
Sacriston   IBGLEPTE    WP1-WP2 7208    111 SubDucted   NoTM
Sacriston   IBGLEPTE    WP1-WP2 7208A   116 Desilt  NoTM
Sacriston   IBGLEPTE    WP1-WP2 7209    58  Overlay TM
Sacriston   IBGLEPTE    WP1-WP2 7209A   177 SubDucted   NoTM
Sacriston   IBGLEPTE    WP1-WP2 7341    38  Cabled  NoTM
Sacriston   IBGLEPTE    WP1-WP2 7342    189 Unproven    NoTM

我的查询是

$result = mysql_query("SELECT SUM(Length) AS totalm FROM btsec WHERE WP='$wpchose' AND Exchange='$exchose' AND Estimate='$jobchose'");
    $row = mysql_fetch_assoc($result);
    $totalm = $row['totalm'];

    $result1 = mysql_query("SELECT SUM(Length) AS totalroped FROM btsec WHERE WP='$wpchose' AND Exchange='$exchose' AND Estimate='$jobchose' AND Status='Roped' OR Status='SubDucted' OR Status='Cabled'"); 
    $row1 = mysql_fetch_assoc($result1);
    $roped = $row1['totalroped'];
    $ropedp = (int)(($roped / $totalm) * 100+.5);

这给出了不正确的结果,我最终得到了11,000%完成的事情?!

1 个答案:

答案 0 :(得分:1)

此查询不正确

$result1 = mysql_query("SELECT SUM(Length) AS totalroped FROM btsec WHERE WP='$wpchose' AND Exchange='$exchose' AND Estimate='$jobchose' AND Status='Roped' OR Status='SubDucted' OR Status='Cabled'"); 

应该是

$result1 = mysql_query("SELECT SUM(Length) AS totalroped FROM btsec WHERE WP='$wpchose' AND Exchange='$exchose' AND Estimate='$jobchose' AND ( Status='Roped' OR Status='SubDucted' OR Status='Cabled')");