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

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

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)

Mysql Recursive Query Not Returning Parent

I've created a nested commenting system like so:
id
user_id
comment
comment_date
parent_comment_id
And I am trying to get a thread of comments by there parent ids. My query is as such:
with recursive cte (id, user_id, comment, comment_date, parent_comment_id) as (
select id,
user_id,
comment,
comment_id,
parent_comment_id
from comments
where parent_comment_id = 'MES-738fc5be20b24b57978b3e873237ef12'
union all
select c.id,
c.user_id,
c.comment,
c.comment_date,
c.comment_parent_id
from comments c
inner join cte
on c.parent_comment_id = cte.id
)
select * from cte
The works except it does not return the first parent comment, which has a parent_comment_id of null. What could I be doing wrong?
I am guessing that you simply want a different anchor:
select id, user_id, comment, comment_id, parent_comment_id
from comments
where id = 'MES-738fc5be20b24b57978b3e873237ef12'

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`)

MySQL Group by UserID only show last post?

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