MySQL 计数 DISTINCT

MySQL COUNT DISTINCT(MySQL 计数 DISTINCT)

本文介绍了MySQL 计数 DISTINCT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试收集昨天在我的 cp 中的不同访问次数,然后计算它们.

I'm trying to collect the number of distinct visits in my cp yesterday, then count them.

SELECT
    DISTINCT `user_id` as user,
    `site_id` as site,
    `ts` as time
FROM
    `cp_visits`
WHERE
    ts >= DATE_SUB(NOW(), INTERVAL 1 DAY)

出于某种原因,这是提取具有相同站点 ID 的多个结果....我如何只提取和计算不同的 site_id cp 登录?

For some reason this is pulling multiple results with the same site id....how do i only pull and count the distinct site_id cp logins?

推荐答案

 Select
     Count(Distinct user_id) As countUsers
   , Count(site_id) As countVisits
   , site_id As site
 From cp_visits
 Where ts >= DATE_SUB(NOW(), INTERVAL 1 DAY)
 Group By site_id

这篇关于MySQL 计数 DISTINCT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:MySQL 计数 DISTINCT

基础教程推荐