group by and sort by biggest groups - mysql

Suppose I have a blog where users can make a comment. If the comment is spam, people can vote it deleted.
When that happens, a row is inserted in this table:
SPAM_REPORTS
comment_id - ip
The table is unique on comment_id, ip.
Now I want to get outputted the comment_id's ordered by those with the maximum number of reports.
Suppose SPAM_REPORTS is:
comment_id ip
6 888.xxx.xxx.xxx
5 111.xxx.xxx.xxx
5 222.xxx.xxx.xxx
6 444.xxx.xxx.xxx
1 333.xxx.xxx.xxx
5 555.xxx.xxx.xxx
I want the output to be:
comment_id count
5 3
6 2
1 1

try this
select comment_id , count(*) as count from SPAM_REPORTS
group by comment_id
order by count desc
DEMO HERE

Related

mysql complex request by distinct pair (non commutative)

I'm trying to create an sql (mariadb) request that select multiples columns but need two columns to be a unique pair but making sure the pair selected has its created_at value the least than the other duplicata pairs.
Here is what my table approximately looks like :
id
from_user_id
to_user_id
created_at
1
1
2
1000000005
2
2
1
1000000002
3
2
3
1000000008
4
5
6
999999999
5
6
5
100000006
I made this table precise to explain the request I want.
So I want to select the distinct pair (from_user_id, to_user_id) implying that the couple (1,2) which could also be (2,1) should be unique. The second rule is it should pick the couple with the minimum created_at value.
So the result table I want is :
id
from_user_id
to_user_id
created_at
2
2
1
1000000002
3
2
3
1000000008
4
5
6
999999999
2,1,1000000002 because the created_at is lesser than the other same couple case (1,2,1000000005).
In this case if I want only the values above created_at:999999999 to be selected I just have to add one condition.
I really hope my question is clear. I'm struggling to make distinct pairs work with other columns.
Thanks in advance for your answers.
WITH
cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY GREATEST(from_user_id,to_user_id),
LEAST(from_user_id,to_user_id)
ORDER BY created_at) rn
FROM table
)
SELECT *
FROM cte
WHERE rn = 1

SQl:how to write a query for least times id is presnt in the table

I have a table called "UserPlay" which as values like this
th_id route_id
1 1
1 2
1 2
1 3
1 3
I just want least time rout_id is used
I have to get output as this
th_id route
1 1
If I understand correctly, you want the route_id with the lowest count:
select route_id, count(*)
from UserPlay u
group by route_id
order by count(*) asc
limit 1;
You can get the list of the_id on it by including group_concat(the_id).

Select User Having Maximum Rating

I have a table which stores the rating given by the user.
eg:
UserId Rating
3 1
3 2
1 1
1 2
1 3
1 4
2 1
2 23
2 4
I need to retrieve the 10 users who have rated the maximum number of times.
eg:
1 rated 4 times
2 rated 3 times
3 rated 2 times...
Any idea how to write a query using mysql?
You have to group by userID, count the grouped rows, and order by the count in descending order, then you limit the query to 10 rows:
SELECT userID, count(*) times
FROM users
GROUP BY userID
ORDER BY times DESC
LIMIT 10
If you need the output exactly as shown above, try this:
SELECT CONCAT_WS(' ', userID, 'rated', count(*), 'times')
FROM users
GROUP BY userID
ORDER BY count(*) DESC
LIMIT 10
See this fiddle.

mysql subquery issue on comments voting

I have MySQL query which I think needs a subquery. I'd like to count the total number of "up" votes on each of many comments and determine whether a given user has already voted on each comment:
Here are my tables:
Comments Table:
comment_id comment acct_id topic_id comment_date
5 hello5 2 1 9:00am
7 hello7 3 1 10:00am
Votes Table:
comment_id vote acct_id topic_id
5 1 1 1
7 1 4 1
5 1 5 1
here's the output i'm getting:
comment_id commenter comment voter sum did_i_vote
5 2 hello5 1 2 1
7 3 hello7 4 1 1
Here's the desired output:
comment_id commenter comment voter sum did_i_vote
5 2 hello5 **5** 2 1
7 3 hello7 4 1 1
Here's my query:
SELECT votes.acct_id as voter, comments.comment_id, comment, comments.acct_id as
commenter, SUM(vote) as sum, vote as did_i_vote
from votes
right join comments
on votes.comment_id=comments.comment_id
join accounts on comments.acct_id=accounts.acct_id
where topic_id=1
group by comments.comment_id order by comment_date desc
You'll notice these 2 outputs are identical except for voter.
What my query is missing is a way to determine whether a given user, for example with voter=acct_id=5, was the one who voted on any of the comments. Without that condition, the query picks the first voter in the list which for comment_id=5 is voter=1.
So my question is I think I need to insert the following subquery:
SELECT from votes where voter='X'
I'm just not sure where or how. Putting it in parentheses in between the from and votes above eliminates the sum() function so I'm stuck.
Any thoughts?
If I've understood you correctly from your comments above, I think all you need to do is (outer) join the votes table to your query another time, this time only on the votes of the account in question:
SELECT
comments.comment_id AS comment_id,
comments.acct_id AS commenter,
comment AS comment,
-- votes.acct_id AS voter, -- ambiguous
SUM(votes.vote) AS sum,
my_votes.vote IS NOT NULL AS did_i_vote
FROM
votes
RIGHT JOIN comments ON votes.comment_id=comments.comment_id
JOIN accounts ON comments.acct_id=accounts.acct_id -- what purpose ?
LEFT JOIN votes AS my_votes ON
my_votes.commentid=comments.comment_id
AND my_votes.acct_id=#my_acct_id
WHERE topic_id = 1 -- ambiguous
GROUP BY comments.comment_id
ORDER BY comment_date DESC

How to count the number of duplicate records in a database?

Consider the following "tweets" table
tweet_id user_id text
-----------------------------------------
1 1 look at my tweet
2 1 look at my tweet
3 1 a very different tweet
4 1 look at my tweet
5 1 look at my tweets
6 2 a cool tweet
7 2 this is my tweet
8 1 hello
9 1 hello
For each user, I want to count the number of duplicate tweets. In the example above, user_id 1 has a total of 5 tweets, of which 2 are unique (tweet_id 3 and 5) and 3 are duplicate (1, 2 and 4). So the outcome of the query for user 1 should be "3".
[EDIT]
Look at user_id 1. The tweet "look at my tweet" appears 3 times, the tweet "hello" 2 times. The total number of duplicate tweets is then 3 + 2 = 5.
For the first part, you can use the following query
select user_id, sum(count)
from
(
select user_id, text, count(tweet_id) count
from tweets
group by
user_id, text
having count(tweet_id) > 1
) t
group by user_id
The inner query finds all users and tweets that have occured more than once. The outer query adds up the duplicate values for each user
Try this:
Select count(text)-count(distinct text) from tweets where user_id=1
select count(*) as count, text from table group by text order by user_id desc;
You will then need a server side function to group by user_id