How to count who has most hits in MySQL - php

I have a table with a column called bid and I would like to list how many records there exists with each bid value.
Kind of making a TOP TEN list.
Example:
bid with value 1 has 5 entries/rows.
bid with value 2 has 3 entries/rows.
bid with value 3 has 8 entries/rows.
etc
How do I make a query that counts and sums up each of the bid's and sort the in a DESCending order?
Thankful for any kind of help!

This should work in MySQL
select u.firstname, t.bid, count(*) as counts
from your_table t
join users u on u.bid = t.bid
where t.confirmed <> '0000-00-00 00:00:00'
group by t.bid
order by counts desc
Generally you can do
select u.firstname, t.bid, t.counts
from
(
select bid, count(*) as counts
from your_table
where confirmed <> '0000-00-00 00:00:00'
group by bid
) t
join users u on u.bid = t.bid
order by t.counts desc

How about this?
SELECT bid, count(*) as TotalHits
FROM tableName
GROUP BY bid
if you want result as per hits sorted, use
SELECT bid, count(*) as TotalHits
FROM tableName
GROUP BY bid
ORDER BY TotalHits DESC

Related

Join mysql query. Limit 1 on second table only with desc order

SELECT *
FROM student as std LEFT JOIN
(SELECT * FROM billing WHERE `paid` < '4' ) AS bill
ON bill.reg_id = std.reg_id
GROUP BY bill.reg_id
ORDER BY bill.id
Here student table is main table and its primary key is foreign key in billing table.
There are multiple records in billing table with student reg_id .
I want latest billing record with limit 1
and no limit on student table.
Try This Query, I think it should work
SELECT *
FROM student as std LEFT JOIN
(SELECT * FROM billing WHERE `paid` < '4' group by reg_id order by id desc)
AS bill
ON bill.reg_id = std.reg_id
I'm assuming there's a column called billing_date, you'll have to update query with the correct timestamp. This is giving each billing record a rank based on its recency then pulling back only the most recent. It's assuming mysql 8.0, you can mimic this in earlier version.
select
*
from student std
left join (select
*, row_number() over (partition by reg_id order by billing_date desc) rn
from billing) bill on bill.reg_id = std.reg_id and rn=1

show users the position after sorting based on score

I have a table which has the id column and score column, I want to sort the table based on score column and then find the specific user who is loading the page and show him his/her position. for example, tell him "your position is 40th".
Well I know how to sort a query:
SELECT id,score FROM `table` ORDER BY `score` DESC
But after the sort how can I find an specific id's position?
You don't need an order by for this. Instead:
select 1 + count(*)
from table t
where t.score > (select t2.score from table t2 where id = $id);
Try it:
SELECT #rownum:=#rownum+1 ‘rank’, id, score FROM table t, (SELECT #rownum:=0) r ORDER BY score DESC;
This will create a column and increase 1 in each record.

How break ties after getting rank

I have a query that gets me a users rank in a table of scores.
SELECT
*
FROM
(SELECT
*, #rank:=#rank + 1 rank
FROM
(SELECT
user_id, SUM(round_total) TotalPoints
FROM
sx14sp_mem_picks
GROUP BY user_id) s, (SELECT #rank:=0) init
ORDER BY TotalPoints DESC) r
WHERE
user_id = 22234
There is a problem with ties. I have a table field "pick_date" that i would like to use to break ties with. The user who made his picks first beats the tie.
Any ideas?
If sx14sp_mem_picks.pickdate is the field to break ties then in the order by sx14sp_mem_picks subquery, add
min( pickdate) asc
This will put the earliest pickdate first - you have to use MIN() bc you need to use an aggregate function given the use of "group by".
You need to order by the pick date in addition to the total points. However, you are talking about multiple rows per user. So, let's take the last pick date:
SELECT *
FROM (SELECT *, (#rank:=#rank + 1) as rank
FROM (SELECT user_id, SUM(round_total) as TotalPoints, max(pick_date) as max_pick_date
FROM sx14sp_mem_picks
GROUP BY user_id
) s CROSS JOIN
(SELECT #rank := 0) init
ORDER BY TotalPoints DESC, max_pick_date asc
) r
WHERE user_id = 22234;

Combine two SELECT queries from different tables

Issue
I have one table (posts) with articles and article meta.
Another table (post_reviews) contains user-submitted ratings (a value out of 5) for each article, referencing posts by the id of the post in question.
I am trying to find the top three posts, by review, of the last 3 days. Therefore I need to:
find all the posts in that time period (3 days)
find the average rating for each post
sort them by average rating (desc)
Code
For the first part, I can successfully use the query:
SELECT * FROM `posts` WHERE `hub_id`=:hub_id AND `date`>=:start_date AND `date`<=:end_date)
To find each individual post's average rating, I use this query:
SELECT SUM(`review`) AS `total` FROM `post_reviews` WHERE `id`=:id
then get the number of rows from this to work out the average:
SELECT * FROM `post_reviews` WHERE `post_id`=:id
How can I combine these three, or process this data so I can order the posts in a time period by the average rating?
ANSWER
The end result looks like this:
SELECT `posts`.`id`, avg(`post_reviews`.`review`) as `average`
FROM `posts`
JOIN `post_reviews` ON (`posts`.`id`=`post_reviews`.`post_id`)
WHERE `hub_id`=:hub_id
AND `posts`.`date`>=:start_date
AND `posts`.`date`<=:end_date
GROUP BY `post_id`
ORDER BY avg(`review`) desc
Not sure what your hub_id represents, but I assume it's necessary; also assume the key field in Posts is posts.post_id and not posts.id:
SELECT `p`.`id`, avg(`pr`.`review`) AS `average`
FROM `posts` AS `p`
JOIN `post_reviews` AS `pr` ON (`p`.`id`=`pr`.`post_id`)
WHERE `hub_id` =:hub_id
AND `p`.`date` BETWEEN CURRENT_DATE-3 AND CURRENT_DATE
GROUP BY `p`.`id`
ORDER BY avg(`review`) DESC;
See Example: sqlfiddle
Not sure about the syntax for MySQL, but the would need a join and a group by.
Something like....
SELECT post_id. avg(review)
FROM Posts P Inner Join Post_reviews PR on (p.post_id = pr.Post_id)
WHERE `hub_id`=:hub_id AND `date`>=:start_date AND `date`<=:end_date)
Group by Post_id
order by 2 desc
Here is a query that works with sqlfiddle to prove it.
SELECT
p.hub_id
,p.post_id
,p.article
,p.articleMeta
,p.date
,IFNULL(AVG(r.ratings), 0) averageRating
FROM posts p
LEFT JOIN post_reviews r ON
r.post_id = p.post_id
WHERE
hub_id = 1
AND date >= CURDATE() - 3
AND date <= CURDATE()
GROUP BY
p.hub_id
,p.post_id
,p.article
,p.articleMeta
,p.date
ORDER BY
p.date DESC
,averageRating DESC
http://sqlfiddle.com/#!2/39315/1

Order by distinct number of rows for 3 tables?

I have 3 tables - News, Content and Edits.
Every row in each table contains a user_id field.
What I want to do is make a list of the top contributors to these 3 tables.
So I need to count how many times each different user_id appears in each table and order from the highest count to the lowest.
Like this:
SELECT
user_id,
COUNT(*) AS all_actions
FROM (
SELECT user_id FROM News
UNION ALL
SELECT user_id FROM Content
UNION ALL
SELECT user_id FROM Edits
) tmp_table
GROUP BY user_id
ORDER BY all_actions DESC
This should do it.
SELECT all_user_ids.user_id as `user_id`, COUNT(all_user_ids.user_id) as `count`
FROM
(SELECT user_id FROM news
UNION ALL
SELECT user_id FROM content
UNION ALL
SELECT user_id FROM edits) AS all_user_ids
GROUP BY `user_id`
ORDER BY `count` DESC
This would be a per table instance
SELECT DISTINCT(COUNT(user_id)) as num FROM news GROUP BY user_id ORDER BY num DESC LIMIT 0,3

Categories