mysql:完全匹配?

发布于 2024-12-07 07:32:19 字数 3409 浏览 0 评论 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 & foob​​ar.我怀疑 mysql 正在计算 barbar & foob​​ar 而 php 只计算 bar 而不是 bar & foob​​ar. Php 返回 1210,mysql 返回 1783,因此手动计数以查看谁是正确的非常实用......

我用谷歌搜索了一下,但很惊讶没有找到相关的内容“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

I'm pulling some data from mysql and counting occurrences of matches (seemingly equivalent to WHERE foo='bar'). However, as I loop thru the data in PHP, my count is much lower than the count in mysql.

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

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

The data might contain one or more &, so I want to match only bar and not bar & foobar. I suspect mysql is counting bar and bar & foobar whereas php is only counting bar and not bar & foobar. Php is returning 1210, and mysql is returning 1783, so it's exactly practical to manually count to see who is right…

I googled around, but was surprised nothing relevant came up for "mysql exact match" or "mysql exactly equal" x,x

EDIT

here's the actual sql

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

and the actual 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

the data from SELECT race FROM database.table looks like:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月无声 2024-12-14 07:32:19

你可以这样做:

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

BINARY 发挥了魔力!!!

You can do this:

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

BINARY does the magic!!!

芯好空 2024-12-14 07:32:19

如果您想计算 foo 恰好是 bar 的记录,那么您的 SQL 查询是正确的。

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

If you want to count the records where foo is exactly bar, your SQL query is correct.

There's something wrong with your PHP code and the code you posted shouldn't work at all (should count 0 records).

歌入人心 2024-12-14 07:32:19

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

$response = mysql_fetch_assoc($surveydata);
echo $response['rows'];

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

$response = mysql_fetch_assoc($surveydata);
echo $response['rows'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文