0

我正在从 mysql 中提取一些数据并计算匹配的出现次数(似乎等同于WHERE foo='bar')。但是,当我循环遍历 PHP 中的数据时,我的计数远低于 mysql 中的计数。

MYSQL> SELECT COUNT(foo) FROM database.table WHERE foo='bar';

# PHP
while ($response = mysql_fetch_assoc($surveydata)){
    if ($response==='bar') {
        $bar++;
    }
}

数据可能包含一个或多个&,所以我只想匹配bar而不匹配bar & foobar。我怀疑 mysql 正在计数barbar & foobar而 php 只是计数bar而不是bar & foobar. php在返回1210,mysql在返回1783,所以手动统计看看谁是对的,确实很实用……</p>

我四处搜索,但很惊讶“mysql完全匹配”或“mysql完全相等”x,x没有相关

编辑

这是实际的sql

SELECT COUNT(`race`) FROM `database`.`table` WHERE `completion status`='complete';

和实际的php

mysql_query("SELECT `race`,`etcetera` FROM `database`.`table` WHERE `completion status`='complete';");

$demographics=array(
    "race"=>array(
        "White"=>array('consented'=>0,'partial'=>0,'completed'=>0),
        "Black"=>array('consented'=>0,'partial'=>0,'completed'=>0),
        "Hispanic"=>array('consented'=>0,'partial'=>0,'completed'=>0),
        "Asian"=>array('consented'=>0,'partial'=>0,'completed'=>0),
        "Pacific Islander"=>array('consented'=>0,'partial'=>0,'completed'=>0),
        "Multiracial"=>array('consented'=>0,'partial'=>0,'completed'=>0),
        "Other"=>array('consented'=>0,'partial'=>0,'completed'=>0)
    )
    //more
);

while ($dbrecord = mysql_fetch_assoc($surveydata)) {
    foreach ( $dbrecord as $dbfield=>$dbcellval ) {
        foreach ( $demographics as $demographic=>&$options ) {
            foreach ( $options as $option=>&$counter ) {
                if ( $option==="Multiracial" && strpos($dbcellval,'&') >0 && strpos($dbcellval,'&')!==false ) {
                    if ($dbrecord['consent']==="1"){
                        $demographics["race"]["Multiracial"]['consented']++;
                        if ($dbrecord['completion status']==="partial") {
                            $demographics["race"]["Multiracial"]['partial']++;
                        } // if
                        else if ($dbrecord['completion status']==="complete") {
                            $demographics["race"]["Multiracial"]['completed']++;
                        } // else if
                    } // if
                }
                else if ($option===$dbcellval){
                    if ($dbrecord['consent']==="1"){
                        $counter['consented']++;
                        if ($dbrecord['completion status']==="partial") {
                            $counter['partial']++;
                        } // if
                        else if ($dbrecord['completion status']==="complete") {
                            $counter['completed']++;
                        } // else if
                    } // if
                } // else if $option==$item
            } // foreach $options
        } // foreach $demographics
    } // foreach $dbrecord
} // while

来自的数据SELECT race FROM database.table如下:

White & Black
White
White & Asian
White & Asian & Black
Asian
Asian & Black
// etc
4

3 回答 3

2

你可以这样做:

MYSQL> SELECT COUNT(foo) FROM database.table WHERE BINARY foo='bar';

二进制会变魔术!!!

于 2012-10-22T15:51:46.463 回答
1

如果你想计算 where foois exactly的记录bar,你的 SQL 查询是正确的。

您的 PHP 代码有问题,您发布的代码根本不应该工作(应该算 0 条记录)。

于 2011-09-29T22:36:05.457 回答
0

MYSQL> SELECT COUNT(foo) AS rows FROM database.table WHERE foo='bar';

$response = mysql_fetch_assoc($surveydata);
echo $response['rows'];
于 2011-09-29T22:30:59.570 回答