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
Related
Hi I am having a problem with following query.
SELECT id, user_id, cloth_id FROM `items` GROUP BY user_id ORDER BY id desc LIMIT 3
I want the latest records with group by but somehow its showing old records.
I have also gone through MySQL - Group by with Order by DESC but not working as expected.
Try this:
SELECT i.id, i.user_id, i.cloth_id FROM
(
SELECT max(id) as id, user_id FROM `items` GROUP BY user_id
) temp
LEFT JOIN `items` i on i.user_id = temp.user_id AND i.id = temp.id
in temp you get each user with the latest id.
in i you get the cloth_id for that combination
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.
well, i'm new to MySQL Database and i got a problem, i need to get the last two set of records from a table ( the records are automatically added to this table every week) i need to use to find the growth that an entity did during the last week.
can any one help plz.
here is what i wrote, i tested it on my local host and it worked :D, but when we installed it online, it crashed :(
select pages.*, new.* from (
select id, tableone.page_id, one, two,(two - one) as diff from
(SELECT id, page_id, likes as two FROM `page_records` WHERE id IN ( SELECT max( id ) FROM `page_records` GROUP BY page_id )) as tableone left join
(SELECT page_id , likes as one FROM `page_records` where id in ( SELECT max(id) FROM `page_records` where id not in (select max(id) from `page_records` group by page_id) group by page_id))
as tabletwo
on tableone.page_id = tabletwo.page_id
order by tableone.page_id asc) as new inner join pages on pages.id = new.page_id
Thanks in advance.
try:
SELECT id, page_id, likes FROM page_records order by id desc limit 0, 2
Try this:
SELECT * FROM table_name
ORDER BY id DESC
LIMIT 0, 2
Thanks!
I am having two tables table_purchase1 and table_purchase2. From these tables i want to list purchase history with order by date.
My tables
table_purchase1
purchase_id user_id item date
1 1 AA1 2011-06-10
2 1 AA2 2011-06-12
table_purchase2
purchase_id user_id item date
1 1 BB1 2011-06-11
2 1 BB2 2011-06-13
Expecting result
item
BB2,AA2,BB1,AA1
How can i do this
Thanks
You can use UNION:
(SELECT item, `date` FROM table_purchase1)
UNION
(SELECT item, `date` FROM table_purchase2)
ORDER BY `date`
If you need single row following might work:
SELECT GROUP_CONCAT(
( (SELECT item, `date` FROM table_purchase1)
UNION
(SELECT item, `date` FROM table_purchase2)
ORDER BY `date`
))
(SELECT
item
FROM
table_purchase1)
UNION
(SELECT
item
FROM
table_purchase2)
ORDER BY `date`
$sql = (SELECT item, date FROM table_purchase1) UNION (SELECT item, date FROM table_purchase2) ORDER BY date
I'm currently working on a news database website and I can't seem to create a query to select the 5 hottest news articles. The 2 tables of the database that are affected for this query are:
News - containing all news items (id, author, message, etc.)
Rates - containing all ratings on news items (id, news_id, rating, etc.)
Now my query should select 5 news_ids from the table Rates with the highest average rating and most votes ( so: ordered by AVG(Rating) and COUNT(*) I supposed ). I first tried to make my query as well get all info of these news_ids from the News table instantly ( using a WHERE id IN(--the query selecting the 5 hottest news_ids--) clause ) but that returned an error of my MySql Version not being cappable of using LIMIT inside of the WHERE IN clause sub-query.
Well, I hope you can help me out on the first query that has to select those 5 news_ids. The query I got as for now ( but not fully working ) is:
SELECT news_id FROM
(SELECT news_id, AVG(rating) AS average_r, COUNT(*) AS amt_r
FROM rates
GROUP BY news_id
ORDER BY average_r,amt_r
DESC LIMIT 5
) AS news_rates
or in content with the rest of my script:
$get_hot_news_ids = mysql_query("SELECT news_id FROM
(SELECT news_id, AVG(rating) AS average_r, COUNT(*) AS amt_r
FROM rates
GROUP BY news_id
ORDER BY average_r,amt_r DESC LIMIT 5) AS news_rates");
$first = 1;
while($news_id = mysql_fetch_assoc($get_hot_news_ids)) {
if(!$first) {
$hot_news_ids .= " ,";
}else{
$first = 0;
}
$hot_news_ids .= $news_id['news_id'];
}
//print_r($hot_news_ids);
$get_hot_news = mysql_query("SELECT * FROM news
WHERE id IN($hot_news_ids)
ORDER BY FIELD(id, $hot_news_ids)");
Are you sure both average_r and amt_r are both in descending order?
SELECT news_id FROM
(SELECT news_id, AVG(rating) AS average_r, COUNT(*) AS amt_r
FROM rates
GROUP BY news_id
ORDER BY average_r DESC, amt_r DESC
LIMIT 5
) AS news_rates
Try this:
SELECT TOP 5 N.id, N.author, N.message, AVG(R.rating) AS rate, COUNT(R.news_id) AS votes
FROM news N
INNER JOIN rates R ON N.id = R.news_id
GROUP BY N.id, N.author, N.message
ORDER BY rate, votes
You can use a join instead, which will allow the limit:
SELECT *
FROM news n JOIN (
SELECT news_id, AVG(rating) AS average_r, COUNT(*) AS amt_r
FROM rates
GROUP BY news_id
ORDER BY average_r,amt_r DESC
LIMIT 5
) top5 ON n.news_id = top5.news_id
ORDER BY top5.average_r,top5.amt_r
Note: You might want to change your query to a ORDER BY average_r DESC, amt_r DESC to get the highest rated items, instead of the lowest rated.