Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mySQL query to count number of unique users posting to database

Tags:

php

mysql

In a database we have about 1000 registered users. I'd like to know how many of those users have actually written a question or posted an answer. All the information can be take from the tblQA table and the userid is "intPosterID", the questions & answers each have their own ID, "PostID". Is there a query that can be run to count how many unique users have posted a question or answer?

like image 540
BigMike Avatar asked Aug 23 '10 22:08

BigMike


People also ask

How do I count unique values in MySQL?

To count distinct values, you can use distinct in aggregate function count(). The result i3 tells that we have 3 distinct values in the table.

How do I count the number of users in MySQL?

Counting the distinct userIDs can be done via: SELECT COUNT( DISTINCT intPosterID ) FROM tblQA; COUNT( DISTINCT field ) returns a count of the number of rows with different values for the given field - intPosterID in this case.

Does count * Return the number of rows?

Answer: COUNT function is an aggregate function that can be used in 3 ways. COUNT(*) – This would COUNT all the rows returned by the SELECT QUERY.

What is Rowcount in MySQL?

rowcount. This read-only property returns the number of rows returned for SELECT statements, or the number of rows affected by DML statements such as INSERT or UPDATE .


1 Answers

Counting the distinct userIDs can be done via:

SELECT COUNT( DISTINCT intPosterID ) FROM tblQA;

COUNT( DISTINCT field ) returns a count of the number of rows with different values for the given field - intPosterID in this case.

like image 132
ConroyP Avatar answered Sep 22 '22 11:09

ConroyP