mysql根据不同的值计算,MySQL在多列上计算不同的值

本文介绍如何使用SQL查询来统计多个列中不同值的出现次数。作者面临的问题是在用户登录记录表中,需要找出每个用户登录过哪些计算机以及登录次数。通过在GROUP BY子句中列出所有相关列,可以实现这一目标。查询结果将展示每个用户的登录计算机及对应的登录次数。
摘要由CSDN通过智能技术生成

I wrote a script that runs each time a user logs into a computer in our domain. This script makes a record of the user as well as the computer they logged into. Any number of users can log into any number of computers.

I just inherited this IT environment from a consultant who is no longer around, and I'm writing this little query so when I get a call from a user, I can search by that user's name and reasonably predict which computer they are using by the number of times they've logged into any given computer.

Here's a sample of the data in the 'login' table:

COMPUTER USER

ncofp02 lee

ncofp02 lee

ncofp02 andy

ncodc01 andy

ncodc01 andy

ncodc01 lee

What I'm banging my head on is the logic to count distinct values across multiple columns. I'd like to see a result like this:

COMPUTER USER COUNT

ncofp02 lee (2)

ncofp02 andy (1)

ncodc01 lee (1)

ncodc01 andy (2)

Is there a way to accomplish this with a single query within mysql, or should I start looping some php? (booooo!)

解决方案

Just list multiple columns in the GROUP BY clause.

SELECT computer, user, count(*) AS count

FROM login

GROUP BY computer, user

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值