【问题标题】:how to count my comment from mysql如何计算我来自 mysql 的评论
【发布时间】:2014-01-08 17:15:55
【问题描述】:

我如何在我的代码中包含计数来计算和回显在我的页面上发布的评论总数,并且我希望它能够自行更新。请帮帮我。这是代码

SELECT * COUNT(id) as count  FROM test WHERE id =id.

这是 MySQL 数据

id(primary key) | name   | comment       

       1    | john   | some  

       2    | mark   | text      

       3    | David  | text   

       4    | Chris  | text   

       5    | Joel   | other

【问题讨论】:

  • 你使用框架吗?你使用香草 PHP 吗?如果没有一些重要信息,我们无法为您提供帮助……您的问题太模糊了。
  • SELECT COUNT(id) as counts FROM test

标签: php mysqli


【解决方案1】:

您需要对数据进行分组。假设你想计算每个用户做了多少 cmets:

SELECT name, COUNT(*) as count  FROM test GROUP BY name;

您需要选择名称和计数,以便稍后输出。

如果你只想知道整张表有多少个cmets

SELECT COUNT(*) as count  FROM test

【讨论】:

    【解决方案2】:
    $count = $db->exec("SELECT count(ID) from test");
    

    【讨论】:

      【解决方案3】:

      查询后立即使用FOUND_ROWS()

      SELECT * FROM table1 WHERE  ...;    
      SELECT FOUND_ROWS();
      

      【讨论】:

        【解决方案4】:
        // Following query will get you the total number of comments in your table
        
        SELECT COUNT(*) AS total_comments FROM comments;
        
        +----------------+
        | total_comments |
        +----------------+
        |             12 |
        +----------------+
        
        
        
        // This will always give you total_comments = 1 if id exists in table (since id is primary key)
        SELECT COUNT(*) AS total_comments FROM comments WHERE id = 1;
        
        +----------------+
        | total_comments |
        +----------------+
        |              1 |
        +----------------+
        

        参考这个tutorial

        要使用 mysqli 处理 PHP,请处理 tutorial

        【讨论】:

          猜你喜欢
          • 2021-10-27
          • 2012-01-17
          • 1970-01-01
          • 1970-01-01
          • 2020-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多