Find users who have min and max rating? - mysql

I have a Ratings(User_ID, Rating) table, The minimum rating for user is 1 and the maximum rating is 12
My question is: How to get users, who have both ratings, 1 and 12 ?

If I correctly understand, you need this:
select user_id from t where
rating IN(1,12)
group by user_id
having count(distinct rating) = 2
If there no chance that same rating may repeated for same user, then you can use count(rating) instead.

Related

mysql select rows with limit but also include the users with same votes beyond limit

The table name is user_votes and we get the top five users according to vote count with this query
select * from user_votes order by votes desc limit 5
it works fine but now let's say the 5th user and the 6th user have the same vote count. Then this query will select only TOP 5 but will neglect the 6th user. We want to include the 6th user too as he has the same number of votes as the 5th
As in the attached image, the bottom 2 users with user_id [1,5] has the same votes, there is one more user with the same votes but query won't show it
The goal is to include all the users who have vote count 2 even though limit is 5. So minimum row count returned is 5 in case all the users have different votes but maximum depends on the last row votes column value. In this case its 2
Mysql version is Ver 8.0.25
This is a job for the DENSE_RANK() windowing function. Here's a fiddle.
WITH ranking AS (
SELECT user_id,
votes,
DENSE_RANK() OVER (ORDER BY votes DESC) ranking
FROM vote
)
SELECT *
FROM ranking
WHERE ranking <= 5
ORDER BY ranking;
These windowing functions allow you to specify various things like ranking easily.

Calculate Average of Text Value Mysql [duplicate]

Using PHP and MySQL, I want to query a table of postings my users have made to find the person who has posted the most entries.
What would be the correct query for this?
Sample table structure:
[id] [UserID]
1 johnnietheblack
2 johnnietheblack
3 dannyrottenegg
4 marywhite
5 marywhite
6 johnnietheblack
I would like to see that "johnnietheblack" is the top poster, "marywhite" is second to best, and "dannyrottenegg" has the least
Something like:
SELECT COUNT(*) AS `Rows`, UserID
FROM `postings`
GROUP BY UserID
ORDER BY `Rows` DESC
LIMIT 1
This gets the number of rows posted by a particular ID, then sorts though the count to find the highest value, outputting it, and the ID of the person. You'll need to replace the 'UserID' and 'postings' with the appropriate column and field though.
I believe this should work...
SELECT user_id, COUNT(*) FROM postings ORDER BY COUNT(*) GROUP BY user_id LIMIT 1
Assuming posting is a tuple (user_id, recipient_user_id), where each row represents one posting, from user_id to recipient_user_id:
select user_id, count(*) as posts
from postings
group by user_id
having count(*) = max(count(*)) ;

How to use SUM and COUNT mysql in one query

Not sure if this is possible but I have the following
SELECT SUM(rating) as rating FROM details WHERE client_id = '$id'
The rating column contains individual ratings of users (1,2,3,4,5) stars. I need to sum them all to be able to calculate the average, but I would also like to get how many stars of each delimiter that user got as well.
For instance
The user has 3 records and the ratings are (3,4,3). With a SUM of 10 out of 3 records, I get 3.3 average. But I would like as well
3 stars = 2
4 stars = 1
Is it possible to do this with one query?
If I got your question right, you can use AVG() function instead of SUM().
In the following query, it calculates the average rating of this client_id, also counts number of each star as required in question.
SELECT
AVG(rating) as rating,
COUNT(CASE WHEN rating=1 THEN 1 END) as star_1,
COUNT(CASE WHEN rating=2 THEN 1 END) as star_2,
COUNT(CASE WHEN rating=3 THEN 1 END) as star_3,
COUNT(CASE WHEN rating=4 THEN 1 END) as star_4,
COUNT(CASE WHEN rating=5 THEN 1 END) as star_5
FROM
details
WHERE
client_id = ID_HERE
GROUP BY
client_id
SELECT client_id ,sum(if(rating='1',1,0)) onestar,sum(if(rating='2',1,0)) twostar,sum(if(rating='3',1,0)) threestar,sum(if(rating='4',1,0)) fourstar,sum(if(rating='5',1,0)) fivestar, SUM(rating) sumrate,AVG(rating) as avgrate FROM details WHERE client_id = '$id'
or for all clients
SELECT client_id ,sum(if(rating='1',1,0)) onestar,sum(if(rating='2',1,0)) twostar,sum(if(rating='3',1,0)) threestar,sum(if(rating='4',1,0)) fourstar,sum(if(rating='5',1,0)) fivestar, SUM(rating) sumrate,AVG(rating) as avgrate FROM details WHERE group by client_id
It is a bit unclear what exactly you are after, but this might work:
SELECT rating, count(rating) FROM details WHERE client_id = '$id' GROUP BY rating;
Also, not sure how you are getting $id, but if you are getting it from the user, you should be using prepared statements rather than inserting it directly in your SQL code.
You don't need to sum your rating. I believe what you want is to key off the rating. I'd suggest this.
This will group every rating then tell you how many times this client is rated something that many times. This would also help you account for any instances where they made a decimal vote (i.e. 4.5 stars)
SELECT
concat(rating, ' Stars') as rating,
count(*) as count
FROM details
WHERE client_id = '$id'
Group by concat(rating, ' Stars');
I think this should do it. Also, you said you're summing them only to take the average. Why not use AVG() instead.
SELECT COUNT(rating), AVG(rating) as rating FROM details WHERE client_id = '$id' GROUP BY rating

mysql sum with format

I want to rating my property to restrict the user to rate it only once.
So i am saving rating points and user id in the table.
Now i want the total avg rating of that property.
I am using my query like this
SELECT SUM(rating_number) as rating_number, FORMAT((SUM(total_points) / SUM(rating_number),1) as average_rating FROM post_rating WHERE post_id = 9781 AND status = 1
But i am getting errors. How can i get that total average rating.
Thanks
Use this
SELECT SUM(rating_number) as rating_number,
FORMAT((SUM(total_points) / SUM(rating_number)),2) as average_rating
FROM post_rating WHERE post_id = 9781 AND status = 1

MYSQL sum the total up and down votes by all users for the items bought by a single user

I'd like to sum the total up and down votes on only the items bought by a single user. I have a big table so I don't want to sum all votes made by everyone for EVERY item, just the items that a particular user bought.
Here's my query so far:
select SUM(purchaseyesno) AS tots, SUM(rating=1) AS yes, SUM(rating=0) AS no, item_id
from items_purchased
where purchaser_account_id=12373
group by item_id
as you can expect, these sums are only the summing user 12373's info, so its just one value. I'm not sure how to get ALL the purchases of item_ids that are bought by user 12373.
I'm sure there is some kind of subquery,nesting thing I need to include but I'm clueless.
here's how I'd like my data to look, item_id=3,4,5 are all bought by user=12373. Whereas item_id=1,2,6 were bought by other users.
item_id tots yes no
3 7 4 2
4 5 1 3
5 1 0 1
thoughts?
select item_id, SUM(purchaseyesno) tots, SUM(rating = 1) yes, SUM(rating = 0) no
from items_purchased
where item_id in (
select item_id from items_purchased
where purchaser_account_id = 12373
)
group by item_id