mysql:完全匹配?
我从 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 正在计算 bar
和 bar & foobar
而 php 只计算 bar
而不是 bar & foobar
. 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以这样做:
BINARY 发挥了魔力!!!
You can do this:
BINARY does the magic!!!
如果您想计算
foo
恰好是bar
的记录,那么您的 SQL 查询是正确的。您的 PHP 代码有问题,您发布的代码根本不应该工作(应该计数 0 条记录)。
If you want to count the records where
foo
is exactlybar
, 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).
MYSQL> SELECT COUNT(foo) AS rows FROM database.table WHERE foo='bar';
MYSQL> SELECT COUNT(foo) AS rows FROM database.table WHERE foo='bar';