MySQL Group by UserID only show last post? - mysql

I'm trying to show a list with the last posts from each user. If I group by ID however I get the first post instead of the last. How can I group by UID and show only the row with the biggest Date?
This is what I'm trying now:
SELECT * FROM Posts GROUP BY `UID` ORDER BY `Date` DESC

Because you want the largest Date per user, you can use MySQL's MAX():
SELECT MAX(`Date`), * FROM Posts GROUP BY `UID`
You can also specify it in the HAVING clause too:
SELECT *
FROM Posts
GROUP BY `UID`
HAVING `Date` = MAX(`Date`)
ORDER BY `Date` DESC

You can do this using a join, to get the max date, and then only choosing those records:
select p.*
from posts p join
(select uid, max(date) as maxdate
from posts p
group by uid
) pmax
on p.uid = pmax.uid and
p.date = pmax.maxdate

Related

Not able to select the latest row with maximum date

DB table structure above
Result
See the images
SELECT max(comment_date), comment_content FROM wp_comments WHERE comment_post_id = 8687
It selects the field with latest date but wrong content.
I want to implement this in the below query which already has one GROUP BY statement and multiple JOINS, using limit alters the result.
Your query should fail, because it is an aggregation query and it has no group by but has unaggregated columns.
The simple way to do this uses order by and limit:
SELECT c.*
FROM wp_comments c
WHERE comment_post_id = 8687
ORDER BY comment_date DESC
LIMIT 1;
use a Join with the same table with the max date and searching for the coincidence
SELECT comment_date, comment_content
FROM wp_comments a
JOIN (SELECT comment_post_id, max(comment_date) as maxdate FROM wp_comments) b ON
b.comment_post_id = a.comment_post_id
and b.maxdate = a.comment_date
WHERE comment_post_id = 8687
Select max date in sub query and fetch required columns.
SELECT comment_date, comment_content
FROM wp_comments
WHERE comment_post_id = 8687
AND comment_date = (Select max(b.comment_date) from
wp_comment b)

How to get the current id from an outer query in a nested select

I'm trying to fetch the most common value from a column when I group entries.
I figured I have to use a GROUP BY in order to get that working. I have the following query:
SELECT post_id, (SELECT browser from visits WHERE
id = ? GROUP BY browser ORDER BY count(browser)
DESC limit 1) as common_browser, count(id) as visits FROM `visits` group by post_id
On the "?" I want to pass the ID of the group, it does what I'm trying to do if I pass a parameter to it, i.e: id = 1.
If there's any other way to accomplish this, please let me know!
Use a correlated subquery.
SELECT post_id, (
SELECT browser
FROM visits AS v1
WHERE v1.post_id = v.post_id
GROUP by browser
ORDER BY COUNT(*) DESC
LIMIT 1) AS common_browser, COUNT(*) AS visits,
FROM visits AS v
GROUP BY post_id

Select values from table A based on values from table B?

I'm creating a very basic post and reply system to get a better understanding of MySQL and PHP. I have two tables: posts and comments.
posts(post_id, post_user, timestamp, post_text)
comments(comment_id, post_id, timestamp, comment_text)
What I want to do is order the posts by the ones that have the most recent reply. So I would need to SELECT * from posts ordered by comments.timestamp desc since I want to order by most recent comments and not by the original post's timestamp. I can't figure out a proper query that works.
You may looking for this
SELECT p.*
FROM posts p
INNER JOIN comments c ON c.post_id= p.post_id
ORDER BY c.timestamp desc
SELECT post_id, post_user, timestamp, post_text,
most_recent_comment
FROM posts NATURAL JOIN
( SELECT post_id,
MAX( timestamp ) AS most_recent_comment
FROM comments
GROUP
BY post_id ) AS t
UNION
SELECT post_id, post_user, timestamp, post_text,
NULL AS most_recent_comment
FROM posts
WHERE post_id NOT IN ( SELECT post_id FROM comments );
SELECT A.Post_Id FROM
(SELECT P.Post_Id,C.TimeStamp,ROW_NUMBER() OVER( PARTITION BY S.Post_Id ORDER BY C.TimeStamp DESC) Rnk FROM POSTS p INNER JOIN COMMENTS C
ON P.Post_Id=C.Post_Id) A
WHERE A.Rnk=1
ORDER BY A.TimeStamp DESC
This is SQL SERVER version.
So hope you can find Mysql version for it

Select latest post from each categories

Am trying to get the latest post from each category of post ordered by the date, i tried this but its not giving the latest post from the table and when i use 'order by' before 'group by', the post are not ordered by date using mysql.
SELECT post_id, category, author_id, title, article, time FROM (SELECT * FROM blog_post GROUP BY category LIMIT 0,5 ) AS timePost ORDER BY time DESC
SELECT post_id, category, author_id, title, article, time FROM (SELECT * FROM blog_post ORDER BY date LIMIT 0,5 ) AS timePost GROUP BY category
You can do self join.
SELECT T1.`post_id`,
T1.`category`,
T1.`author_id`,
T1.`title`,
T1.`article`,
T1.`time`
FROM
blog_post T1
INNER JOIN
(SELECT MAX(`time`) AS `time`,`category` FROM blog_post GROUP BY category) T2
ON T1.`category` = T2.`category` AND T1.`time` = T2.`time`
ORDER BY T1.`time` DESC
Hope this helps.
IN MS SQL as your question is not clear as per my assumption
SELECT post_id, category, author_id, title, article, time
FROM blog_post t
WHERE
t.post_id=(SELECT TOP 1 post_id
FROM Table t2
WHERE
t.post_id=t2.post_id
ORDER BY time DESC)
This seem to have work... i dont know how efficient it is though
SELECT post_id, category, author_id, title, article, time
FROM (SELECT * FROM blog_post ORDER BY time DESC )
AS timePost GROUP BY category ORDER BY time DESC

Group by and also order by?

Two tables:
Topics
Comments (There is a Topic field, so I know in which topic the comment is)
And I want to get only the newest comment in each topic (highest value in Date field) and then order the topics in that way.
Query I've tried:
SELECT User, Topic, Date
FROM Comments
GROUP BY Topic
ORDER BY Date DESC
Return a row if no other row with same topic has a later date.
SELECT User, Topic, Date
FROM Comments c1
where not exists (select 1 from Comments c2
where c2.topic = c1.topic
and c2.date > c1.date)
order by date desc
Try this, improved answer:
SELECT `User`,
temp.`Topic`,
temp.`Date`
FROM (
SELECT `Topic`,
MAX(`Date`) `Date`
FROM `Comments`
GROUP BY `Topic`
ORDER BY MAX(`Date`) DESC
) temp
INNER JOIN `Comments`
USING (`Topic`, `Date`)