MySQl基于多种因素的唯一计数

时间:2016-03-15 13:58:48

标签: mysql

我需要根据ip和类别为我的网站计算不同的点击次数。

我可以计算不同的ip,但无法获得不同的类别。我有大约250个类别。

我这样做。

我的表结构。

 |id | category  |     ip     |    date    |
 | 1 |    234    | 1698170031 | 1457966671 |
 | 2 |    234    | 1698170032 | 1458041287 |
 | 3 |    233    | 1698170033 | 1458041298 |
 | 4 |    233    | 1698170034 | 1458041334 |
 | 5 |    102    | 1698170035 | 1458042584 |
 | 6 |    102    | 1698170036 | 1458042614 |
 | 7 |    102    | 1698170037 | 1458042865 |
 | 8 |    102    | 1698170038 | 1458043089 |
 | 9 |    102    | 1698170039 | 1458043171 |
 | 10|    102    | 1698170040 | 1458043181 |

SELECT COUNT(DISTINCT `ip`) AS `total` FROM `stats`.`hits`;
//this is working fine showing 10 hits 

SELECT COUNT(DISTINCT `ip`) AS `hits` FROM `stats`.`hits` GROUP BY `category`;
//this is not working showing 1 hit 

//expected result
category 234 = 2 hits
category 233 = 2 hits
category 102 = 6 hits

我也知道获得不同类别命中的另一种方法

//adding to main query
.
.
COUNT(DISTINCT CASE WHEN `category` = '234' THEN `ip` END) `234`,
COUNT(DISTINCT CASE WHEN `category` = '233' THEN `ip` END) `233`,
.
.

但是查询会很长

请建议一种方法来获取DISTINCT类别。

由于

3 个答案:

答案 0 :(得分:1)

SELECT 
category,
COUNT(DISTINCT ip)
FROM
hits
GROUP BY category;
像你已经尝试过的那样。这不会返回1行。你搞砸了什么,我不知道,但这是解决方案。

证明(但是懒得格式化):

root@localhost:playground > CREATE TABLE t
    ->     (`id` int, `category` int, `ip` int, `date` int)
    -> ;

INSERT INTO t
    (`id`, `category`, `ip`, `date`)
VALUES
    (1, 234, 1698170031, 1457966671),
    (2, 234, 1698170032, 1458041287),
    (3, 233, 1698170033, 1458041298),
    (4, 233, 1698170034, 1458041334),
    (5, 102, 1698170035, 1458042584),
    (6, 102, 1698170036, 1458042614),
    (7, 102, 1698170037, 1458042865),
Query OK, 0 rows affected (0.02 sec)

root@localhost:playground >
root@localhost:playground > INSERT INTO t
    ->     (`id`, `category`, `ip`, `date`)
    -> VALUES
    ->     (1, 234, 1698170031, 1457966671),
    ->     (2, 234, 1698170032, 1458041287),
    ->     (3, 233, 1698170033, 1458041298),
    ->     (4, 233, 1698170034, 1458041334),
    ->     (5, 102, 1698170035, 1458042584),
    ->     (6, 102, 1698170036, 1458042614),
    ->     (7, 102, 1698170037, 1458042865),
    ->     (8, 102, 1698170038, 1458043089),
    ->     (9, 102, 1698170039, 1458043171),
    ->     (10, 102, 1698170040, 1458043181)
    -> ;
Query OK, 10 rows affected (0.00 sec)
Records: 10  Duplicates: 0  Warnings: 0

root@localhost:playground > SELECT
    -> category,
    -> COUNT(DISTINCT ip)
    -> FROM
    -> t
    -> GROUP BY category;
+----------+--------------------+
| category | COUNT(DISTINCT ip) |
+----------+--------------------+
|      102 |                  6 |
|      233 |                  2 |
|      234 |                  2 |
+----------+--------------------+
3 rows in set (0.00 sec)

答案 1 :(得分:0)

循环怎么样(我将使用PDO表示法)

$query = $dbc->prepare("SELECT DISTINCT category FROM stats");
$query->execute();
while ($category = $query->fetch(PDO::FETCH_ASSOC))
{
$countquery = $dbc->prepare("SELECT COUNT(DISTINCT CASE WHEN category = :category THEN ip END");
$countquery->bindParam(':category', $category);
$countquery->execute();
echo "(results of query here)";
}

答案 2 :(得分:0)

这是您的查询

SELECT COUNT(DISTINCT ip)AS hits FROM statshits GROUP BY category;

//这不显示1点击

需要小修改

SELECT COUNT(DISTINCT ip)FROM table_name GROUP BY category;

+ ---------------------- +

| COUNT(DISTINCT ip)|

+ ---------------------- +

| 6 |

| 2 |

| 2 |

+ ---------------------- +

3行(0.00秒)