如果您只想获取math
带有 的主题userid = 'abc' AND flag = '0'
,可以使用以下查询:
SELECT COUNT(subject) AS math
FROM books WHERE userid = 'abc' AND flag = '0' AND subject = 'math';
查看SQLFiddle
否则,如果您只想要带有 的主题userid = 'abc' AND flag = '0'
,那么您query
给出的完全可以做到这一点:
SELECT COUNT(subject) AS math
FROM books WHERE userid = 'abc' AND flag = '0';
查看SQLFiddle
为了在php
使用中显示,mysql_fetch_assoc
您可以提供列名以访问如下值:
$sql = "SELECT COUNT(subject) AS math
FROM books WHERE userid = 'abc' AND flag = '0'";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No math subjects exist";
exit;
}
while ($row = mysql_fetch_assoc($result)) {
echo $row["math"];
}