如何计算与此查询中的字符串文本匹配的数据数量?

时间:2011-04-08 07:14:41

标签: php mysql html

mysql_query("SELECT DISTINCT lo_category FROM questions where question_text = '" . mysql_real_escape_string($_GET["choice"]) . "'");

所以从上面的查询我会得到与选择匹配的lo_categories。我如何得到类别的数量,例如

Category Cognitive : 3 matches
Category Psychomotor : 1 matches
Category Perception : 0 matches

($_GET["choice"])来自html复选框,有多个选项。 这是我的问题表。

question_id  question_text               keyword  lo_category lo_domain
      1     Define the word paraphrase    Define  Knowledge   Cognitive 
      2     describe the meaning of NLP  describe Knowledge   Cognitive 
      3     describe the meaning of NLP  describe Perception  Psychomotor 
      4     describe the meaning of NLP  describe Receiving   Affective 
      5     Define this                    Define Knowledge   Cognitive

更新了这个,有错误。请帮助我好丢失!!!

$yes=mysql_query("select lo_category, count(*) from questions where question_text='" . mysql_real_escape_string($_GET["choice"]) . "' group by lo_category order by lo_category");

while ($row = mysql_fetch_assoc($yes)) {
    echo '<b>Category :</b> ' . $row['lo_category'] . ',' . $row['count(*)'] . '<br />'; 
}

请帮助我,我是这方面的初学者。提前谢谢!!!

3 个答案:

答案 0 :(得分:1)

select lo_category, count(lo_category) as countOflo_category group by lo_category

答案 1 :(得分:0)

通常情况如下:

select lo_category, count(*) from questions where question_text=<choice> group by lo_category order by lo_category

答案 2 :(得分:0)

$ result = mysql_query(“SELECT DISTINCT lo_category FROM questions where question_text ='”。mysql_real_escape_string($ _ GET [“choice”])。“'”);

mysql_num_rows($ result)这将返回你的reslut集包含的行的数量