【问题标题】:Retrieving count of column mySQL PHP检索列 mySQL PHP 的计数
【发布时间】:2011-12-14 06:27:33
【问题描述】:

大家好,我有一个名为interests 的mysql 表,有4 列。 interestID、名称、categoryID interest_desc 和日期。 categoryID 列链接到一个单独的表。如何使用 mysql 查询来检查某个类别中有多少兴趣?

我猜我使用了某种 count() 查询?

谢谢大家

更新 -

$count_query_v1 = "SELECT categoryID, COUNT(*) AS total FROM interests GROUP by categoryID; ";     $answer = mysql_query($count_query_v1) or die(mysql_error()); echo $answer;

越来越近但仍不完美,我想用最感兴趣的ID呼应categoryID

【问题讨论】:

    标签: php mysql database count


    【解决方案1】:
    select category_name, count(*) as total
    from interests i left join category c on c.category_id = i.category_id
    group by i.category_id;
    

    【讨论】:

      【解决方案2】:

      计数+分组,
      假设interestID 是唯一的主键,
      并且每个兴趣都与单个类别相关(如您所展示的)

      select categoryID, count(*) as total
      from interests
      group by categoryID;
      
      // the above example is a simple group by ID without using inner join
      

      输出:-

      categoryID, total
      

      【讨论】:

      • 试图打印出 categoryID 和总兴趣 $count_query_v1 = "SELECT categoryID, COUNT(*) AS total FROM interest GROUP by categoryID; "; $answer = mysql_query($count_query_v1) 或死(mysql_error());回声 $answer;
      【解决方案3】:

      SELECT COUNT(interestID) FROM Interest WHERE categoryID = 'yourvalue';

      SELECT COUNT(interestID), categoryID FROMinterests GROUP BY categoryID;

      【讨论】:

      • 我将如何使用最感兴趣的 ID 来呼应类别
      • 您想回显:数字,类别名称,categoryID 属于另一个表,比如说类别?
      • 我想得到最感兴趣ID的类别
      • SELECT MAX(COUNT(interestID)), categoryID FROM interest GROUP BY categoryID;
      【解决方案4】:

      由于您使用的是插入查询,每个查询都会插入一条记录,因此只需使用计数器变量计算您运行的插入查询的数量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-27
        • 2016-04-16
        • 1970-01-01
        • 2016-02-13
        • 1970-01-01
        • 1970-01-01
        • 2011-07-19
        • 1970-01-01
        相关资源
        最近更新 更多