mySQL query to count number of unique users posting to database - php

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?

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.

Count posts per user :
SELECT COUNT(PostID), intPosterID FROM tblQA GROUP BY intPosterId
numbers of results = number of users or run ConroyP query

COUNT(DISTINCT columnname) can be used for that :
SELECT COUNT(DISTINCT intPosterId) FROM tblQA;

This should do it.
select count(intPosterID)
from tblQA
group by intPosterID;

Related

mysql merge two queries, get count of each value from two seperate columns?

$AcceptEventCount = mysql_query("SELECT COUNT(*) as count, tblDevices.name, tblEvents.sentdeviceid FROM tblDevices,tblEvents WHERE tblDevices.deviceid = tblEvents.sentdeviceid
GROUP BY tblEvents.sentdeviceid ORDER BY count DESC");
$DeclineEventCount = mysql_query("SELECT COUNT(*) as cnt, tblDevices.name, tblDeclinedEvents.deviceid FROM tblDevices,tblDeclinedEvents WHERE tblDevices.deviceid = tblDeclinedEvents.deviceid
GROUP BY tblDeclinedEvents.deviceid ORDER BY cnt DESC");
I'm new to merging two queries in php mysql. I tried unions with it but i'm not able to get the desired result.
Basically I want to get a count of the accepted events and the declined events of the devices from the table tbldevices. Three tables are involved here. can anyone help me with this?
this is what i tried!
SELECT COUNT() as count, tblDevices.deviceid,null,tblEvents.sentdeviceid
from tblEvents,tblDevices
WHERE tblEvents.sentdeviceid = tblDevices.deviceid
GROUP BY tblEvents.sentdeviceid
UNION
SELECT COUNT() as cnt,tblDevices.deviceid,tblDeclinedEvents.deviceid,null
from tblDevices,tblDeclinedEvents
WHERE tblDeclinedEvents.deviceid = tblDevices.deviceid
GROUP BY tblDeclinedEvents.deviceid
I think it is not possible (at least without using sub query). The reason is that group by needs to be done on fields, but accepted and declined events are not identified by a field but by tables.
If you can't modified the table structure, having 2 queries is a reasonable options. If you can change the table structure, I would recommend adding a column that store the validation status.

How Would I Nest This MySQL Select Query?

I have a table with user payment details, and another table with a list of users. I want to get the balance of all users from the payment details, but only for users that are not banned (which is a column in the users table).
I am somewhat new to nested queries so I am not sure how to do this?
Here is what I have tried so far...
mysql_query("
SELECT SUM(balance)
FROM payment_details
WHERE (SELECT ban
FROM users
WHERE username=username
) != '1'
")
Note: Username is a column in both tables.
The above query does not work.
To Recap: There are two tables: payment_details and users. I want to add together the balance column for all the users that are not banned.
Don't use a subquery here. Instead, use a JOIN.
SELECT SUM(payment_details.balance) FROM payment_details
JOIN users ON payment_details.username = users.username
WHERE ban != '1'
Agreed, a join is probably what you want.
However to answer the specific question, you could try a query like:
SELECT SUM(payment_details.balance)
FROM payment_details
WHERE payment_details.username in (
SELECT users.username
FROM users WHERE ban != '1'
);
Note that it wasn't clear from the question whether you wanted the sum for each individual user, or the total sum for all users. The above query provides the latter -- not grouped by user.
the query is:
SELECT SUM(balance) FROM payment_details JOIN users ON payment_details.username=users.username WHERE users.ban !='1' group by payment_details.username
by the way, working on the users ids would be much better than on the usernames

MySQL SUM each Distinct Client_ID

Goal is to create a top ten list of returning clients. I have a "projects" table that also has a Client_ID for each project.
What I need is for the SQL query to return the top ten results for the Client_ID's that appear the most in the projects table.
I've tried this:
select COUNT(DISTINCT Client_ID) AS 'Top Clients' FROM projects;
But truthfully I am just not sure how I can do this.
thanks for any help!
Using this question MySQL: Count occurrences of distinct values
SELECT Client_ID, COUNT(*) as TopClient FROM projects GROUP BY Client_ID ORDER BY TopClient DESC LIMIT 10;
Does this do the trick?

PHP/MySQL - Counting items within a query

I have the following results for my database table:
The Query:
SELECT
service_titles.user_id, service_titles.slide_id, service_titles.name as title_name ,service_names.name as service_name
FROM service_names
INNER JOIN service_titles ON service_names.title_id = service_titles.id
So what needs to happen is:
If the user has 2 unique service titles, then the max number of service_names for that title will be 6
If the user has 1 service title, the the max number of service_names for that title will be 16
I will be using PHP for all of the coding, but I am wondering how I would go about this. I need a way to count how many unique service_titles there are for that user and slide, and then count how many service items there are for each title.
Thanks for any help!
SELECT COUNT(DISTINCT service_titles.name)
FROM service_names
INNER JOIN service_titles ON service_names.title_id = service_titles.id
GROUP BY service_titles.user_id, service_titles.slide_id
That'll get you the number of distinct title_names for each user_id/slide_id combo.
SELECT COUNT(DISTINCT service_names.name)
FROM service_names
INNER JOIN service_titles ON service_names.title_id = service_titles.id
GROUP BY service_titles.user_id, service_titles.slide_id
... and that's the number of distinct service_names for same. If you want both in one query, you can put both COUNTs together, since you're using the same GROUP BY regardless.
You could use a CASE statement within your query to change the max number of service_names.
See MySQL CASE statement reference
To do this in the SQL itself would be quicker than evaluating it in PHP.
To count how many distinct titles you can try:
SELECT user_id, COUNT(DISTINCT name)
FROM service_titles
GROUP BY user_id

SQL order by, group by, having

I'm using a database to store results of an election with the columns id, candidate, post_time and result. Results are put in the database during 'counting the votes'. When a new update is available, a new entry will be inserted.
From this database, I would like to create a table with the most recent results (MAX post_time) per candidate (GROUP BY candidate), ordered by result (ORDER BY result).
How can I translate this to a working SQL-statement?
(I've tried mysql order and groupby without success)
I've tried:
SELECT *, MAX(time_post)
FROM [database]
GROUP BY candidate
HAVING MAX(time_post) = time_post
ORDER BY result
Assuming that you don't have multiple results per candidate at same time, next should work:
select r.candiate, r.result
from results r
inner join (
select candidate, max(post_time) as ptime
from results
group by candidate
) r2 on r2.candiate=r.candidate and r2.ptime=r.post_time
order by r.result
Note that MAX will not select the record with the maximum time, but it will select the maximum value from any record. So
SELECT MAX(a), MAX(b) FROM example
where exmple contains the two records a=1, b=2 and a=4, b=0, will result in a=4, b=2, which wasn't in the data. You should probably create a view with the latest votes only from each candidate, then query that. For performance, it may be sensible to use a materialized view.
Is the post_time likely to be the same for all the most recent results? Also does each candidate only appear once per post_time?
This could be achieved by just using a SELECT statement. Is there a reason you need the results in a new table?
If each candidate only appears once per post_time:
SELECT candidate, result
FROM table
WHERE post_time = (SELECT MAX(post_time) FROM table)
If you want to count how many times a candidate appears in the table for the last post_time:
SELECT candidate, count(result) as ResultCount
FROM table
WHERE post_time = (SELECT MAX(post_time) FROM table)
GROUP BY candidate
By what i see from ur attempts i'd think you should use this
SELECT MAX(post_time) FROM `table` GROUP BY candidate ORDER BY result
but the MAX statment only return a single value therefore i dont see why ORDER BY would be needed.
if you want multiple results try looking up the TOP statment
One way (tied results shown):
SELECT t.*
FROM tableX AS t
JOIN
( SELECT candidate
, MAX(time_post) AS time_post
FROM tableX
GROUP BY candidate
) AS m
ON (m.candidate, m.time_post) = (t.candidate, t.time_post)
ORDER BY t.result
and another one (no ties, only one row per candidate shown):
SELECT t.*
FROM
( SELECT DICTINCT candidate
FROM tableX
) AS d
JOIN
tableX AS t
ON t.PK = --- the Primary Key of the table, here
( SELECT ti.PK --- and here
FROM tableX AS ti
WHERE ti.candidate = d.candidate
ORDER ti.time_post DESC
LIMIT 1
)
ORDER BY t.result

Categories