MySQL Get 'Most Active User' From Multiple Table - mysql

If i have multiple tables like on the picture below, then I want to get the most active user (counted from posted_by column), for example on this picture Mike is the most active user, what query should i run? Thank you

SELECT posted_by, count(*) AS total
FROM(SELECT posted_by FROM article
UNION ALL
SELECT posted_by FROM ebook
UNION ALL
SELECT posted_by FROM forum_thread) AS counted
GROUP BY posted_by
ORDER BY total DESC
LIMIT 1

Since all tables got the columns, you can use UNION ALL. Also I suggest to use UserID on posted_by field instead of name. If you want to output the name just join the tables with user table.

Related

MySQL view count and sum and other operations from multiple tables

Here is my situation:
I have 4 tables that all contains a column called score in all of these tables my goal for a view to create operations to the result of the 4 tables getting the following values:
Total score
Total number of rows
average (total score / number of rows)
Now i know that i would be able to create the view as:
(SELECT * FROM table1 where condition) + (SELECT * FROM table2 where condition)
So on and so forth.
but for each of the three goals i have i would have to nested select all tables atleast 2 times.
So my question is how do you handle a case like this? is there any operation in sql that makes this an easy task or am i bound to do something redundant?
Update
So my full case is that every use in my system has something called a division_id now i want to use this ID to find out what the score is for each division:
(PLEASE IGNORE THE _COPY)
You could use a UNION to join the 4 tables, since there is no join condition. There are a couple of ways that you could do this with the division field. Probably the most concise is:
select division_id, count(*), avg(scores.score), sum(scores.score) from
user join
(select id as user_id, score from user
UNION ALL
select user_id, score from test_score
UNION ALL
select user_id, score from task_score
UNION ALL
select user_id, score from offline_score) as scores
on user.id = scores.user_id
group by division_id
Link to SQLFiddle

Read each line from a result set and setting the value

I have table(T1) with userid(int), user_name(char) and contact(char) as columns.
I need to iterate over the table to find out which user_name is present multiple times in the table and the count. And I will be using the count to delete the user_name if the occurrence is more than three times.
I tried using
select user_name,count(user_name) from T1 group by user_name having count(*)>3
The output is
user_name EXPR_1
chris 4
Fred 5
Now if I need to use the values in EXPR_1. How to iterate over this result set without creating a new/temp table.
Thanks
Use your query as a sub-select:
DELETE FROM T1 WHERE user_name IN (
SELECT user_name
FROM T1
GROUP BY user_name
HAVING COUNT(user_name) > 3
)
Response to comment:
Well, now I'm confused as to what you're asking for. If all you want to do is sort by highest count, then just do this:
SELECT
user_name,
COUNT(user_name) AS numOccurrences
FROM T1
GROUP BY user_name
HAVING COUNT(user_name) > 3
ORDER BY numOccurrences DESC
Now when you iterate through them, it will be ordered by the number of occurrences, highest to lowest.
Are you asking how to iterate over the results? If so, that completely depends on what tool you're using to execute this query and how you're sending it to your other application. Please elaborate on that.

Get biggest COUNT(DISTINCT) with condition mysql

right now I'm trying to return the biggest COUNT(DISTINCT column)-number from a mysql table.
It's hard to describe, so I'll give you an example:
My table has the following columns: s_id, k_id, p_id.
Now I want to count the different s with the condition that every entry has the same p_id, too. I need this to prepare a HTML-Table (so i know how many Columns this table will have).
Data Example:
This is what I got, so far:
SELECT COUNT(DISTINCT k_id) AS a FROM `table`
the problem with this is, that there may be 4 different k_ids but 3 of them are related to p_id = 1 and the last one is releated to p_id = 2.
a returns 4 instead of 3.
Thanks for support!
I think you want this:
select p_id, count(distinct s_id) as cnt
from table
group by p_id
order by cnt desc
limit 1;
Please consider this:
select max(count(distinct(k_id))) from table
group by p_id

MySQL Distinct Records With Count

I have a photos table, with two columns in that table named "id" and "user_id". Obviously one user can have many photos. I'd like to run a query that can give me the photo count for each user.
Any help is appreciated.
select user_id, count(*) as photo_count from photos group by user_id
Use:
SELECT t.user_id,
COUNT(*) AS photo_count
FROM PHOTOS
GROUP BY t.user_id
use count as your aggregate function and groupby clause to give the count of specific attribute,
here is the sample:
SELECT user_id, count(*) 'Photos Count'
from photos
group by user_id

How to group by and order correctly

I have a problem ordering my results correctly when using the group by. It seems to show the first entry in the database instead of the most recent in the group.
Example:
id(autoincrement) | name
1 | anne
2 | james
3 | anne
4 | brad
As you can see I have "anne" entered multiple times which is why I am using the group by. I would then like it to display the "anne" that is the most recent, which would be the entry "3". Instead it displays the first "anne"(1)
My query
"Select * FROM TABLE GROUP BY name ORDER BY id DESC
Any help would be greatly appreciated.
The problem is that you're selecting all the fields (using * is seldom a good idea) so each row is unique therefore there is nothing to group on.
Try:
SELECT
Name,
MAX(ID)
FROM
TABLE
GROUP BY
Name
A possible solution:
SELECT id, name
FROM TABLE
WHERE id IN (SELECT MAX(id) FROM TABLE GROUP BY name)
Try this; it will work:
SELECT * FROM TABLE
INNER JOIN (SELECT MAX(id) AS id
FROM TABLE
group by name)
ids ON TABLE.id = ids.id
Try
SELECT DISTINCT name
FROM table
ORDER BY id DESC
Building on another anwer already provided, this SQL will avoid having to GROUP BY Name:
SELECT
DISTINCT Name,
MAX(ID)
FROM
TABLE