从单个表 - MySQL中获取不同记录的数量

时间:2012-02-13 12:39:04

标签: mysql sql count

我正在使用名为tbl_sales的表格,此表格包含字段activeis_featured。现在,我正在尝试记录active=0active=1is_featured=1is_featured=0的记录数。我怎样才能在一个查询中得到它。有人可以告诉我一个查询来获得这个。我是编程和自学过程的新手。需要解决方案提前致谢

5 个答案:

答案 0 :(得分:7)

尝试使用SUM

SELECT
  SUM(IF(active=0,1,0)) as nonActiveCount,
  SUM(IF(active=1,1,0)) as activeCount,
  SUM(IF(featured=0,1,0)) as nonfeaturedCount,
  SUM(IF(featured=1,1,0)) as featuredCount
FROM myTable

IF(active=0,1,0)表示“如果有效,则返回1.否则,返回0”。

围绕它的SUM将所有数字相加,如果非活动则为0,如果是活动的则为1(等等)。

答案 1 :(得分:1)

这样做。

select active, is_featured, count(*) from tbl_sales
group by active, is_featured
having (active=0 or active=1) and (is_featured=0 or is_featured=1);

<强>更新

此SQL将使用所有四种组合

对所有行进行分组
active=0; is_featured=0
active=0; is_featured=1
active=1; is_featured=0
active=1; is_featured=1 

并提供每组的计数。由于有一个having子句,它将限制只有行的值为0或1的列。

答案 2 :(得分:0)

例如

SELECT
  cnt.sq1 AS active,
  cnt.sq2 AS inactive,
  cnt.sq3 AS featured,
  cnt.sq4 AS not_featured
FROM (
  SELECT COUNT(*) AS cnt FROM tbl_sales WHERE active = 1
) AS sq1
JOIN FROM (
  SELECT COUNT(*) AS cnt FROM tbl_sales WHERE active = 0
) AS sq2
JOIN FROM (
  SELECT COUNT(*) AS cnt FROM tbl_sales WHERE is_featured= 1
) AS sq3
JOIN FROM (
  SELECT COUNT(*) AS cnt FROM tbl_sales WHERE is_featured= 0
) AS sq4

答案 3 :(得分:0)

SELECT active,is_featured,count(*) FROM tbl_sales GROUP By active,is_featured

示例输出:

+--------+-------------+----------+
| active | is_featured | count(*) |
+--------+-------------+----------+
|      0 |           0 |        7 |
|      0 |           1 |        6 |
|      1 |           0 |      204 |
|      1 |           1 |       66 |
+--------+-------------+----------+

答案 4 :(得分:0)

当您查看mySQL's GROUP BY modifiers时,您会找到一个包含多个GROUP BY的示例(这是我发现的唯一一个官方示例):

mysql> SELECT year, country, product, SUM(profit)
    -> FROM sales
    -> GROUP BY year, country, product;
+------+---------+------------+-------------+
| year | country | product    | SUM(profit) |
+------+---------+------------+-------------+
| 2000 | Finland | Computer   |        1500 |
| 2000 | Finland | Phone      |         100 |
| 2000 | India   | Calculator |         150 |
| 2000 | India   | Computer   |        1200 |
| 2000 | USA     | Calculator |          75 |
| 2000 | USA     | Computer   |        1500 |
| 2001 | Finland | Phone      |          10 |
| 2001 | USA     | Calculator |          50 |
| 2001 | USA     | Computer   |        2700 |
| 2001 | USA     | TV         |         250 |
+------+---------+------------+-------------+

看看WITH ROLLUP是如何运作的(你可能会发现它很有用)。

所以建立你的查询:

SELECT active, is_featured, COUNT(*) as cnt
FROM tbl_sales
GROUP BY active, is_featured WITH ROLLUP

应该总是返回4行,如下所示:

+--------+--------------+-----+
| active | is_feautured | cnt |
+--------+--------------+-----+
| 1      | 1            | 5   |
| 1      | 0            | 8   |
| 0      | 1            | 0   |
| 0      | 0            | 7   |
+--------+--------------+-----+

而不是简单的循环(例如在php中,无论你需要什么,都可以做到:

// Get results from DB
$sql = 'SELECT active, is_featured, COUNT(*) as cnt
    FROM tbl_sales
    GROUP BY active, is_featured WITH ROLLUP';

$q = mysql_query( $sql);

// Initialize values, two array keys 0: value 0, 1: value 1
$active = array( 0, 0);
$is_featured = array( 0, 0);

while( $row = mysql_fetch_assoc( $q)){
    $active[ $row['active']] += $row['cnt'];
    $is_featured[ $row['is_feautured']] += $row['cnt'];
}