简体   繁体   中英

How can i count the number of data that match the string text i have from this query?

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

So from the query above i will get the lo_categories that match with the choice.How do i get the number of categories,eg

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

($_GET["choice"]) is from the html checkbox with multiple choices. And this is my question table.

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

Updated with this ,there's error.Please help i'm so lost!!!

$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 />'; 
}

Please help me i'm a beginner in this..Thanks in advance!!!

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

通常情况如下:

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

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

mysql_num_rows($result)this will return the noumber of row your reslut set contains

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM