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
Related
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
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`)
I have a table posts with columns (id, user_name, thread_id).
A user can submit multiple posts for a thread. thread to post is one to many.
I need to find out who submitted max posts per thread. So the result would be
Max(Count), user_name, thread_id WHERE there will be only one row per thread_id.
The table is too huge so I wanted to get the query optimized as much as I could.
You can try with the group by and having clauses:
select t.user_name, t.thread_id , count(*) as max_count
from tbl t
group by t.user_name, t.thread_id
having count(*) = ( select count(*) as ttl
from tbl
where thread_id = t.thread_id
group by user_name
order by ttl desc
limit 1 )
select user_name, thread_id, count(*) as max
from tbl t
group by user_name, thread_id
having count(*) = (
select count(*) as cnt /* most posts per user per thread */
from tbl
group by user_name, thread_id
order by cnt desc
limit 1
)
Easy workaround for system that don't have limit is:
select user_name, thread_id, count(*) as max
from tbl t
group by user_name, thread_id
having count(*) = (
select max(cnt) from (
select count(*) as cnt /* most posts per user per thread */
from tbl
group by user_name, thread_id
) m
)
Suppose you have a table posts with fields id, user_name & thread_id.
If you want to query which user has posted the most posts on a specific thread and the total number of his posts from a table, you can achieve that with this MySQL query:
SELECT user_name, thread_id, count(thread_id)
FROM posts WHERE thread_id=1 GROUP BY user_name
ORDER BY count(thread_id) DESC LIMIT 1;
It will return only one row...
i have next query
SELECT *
FROM(
SELECT
ID,
SUM(points) AS SUMMARY
FROM my_table
GROUP BY ID
ORDER BY SUMMARY DESC
) t1
WHERE SUMMARY>=(SELECT
SUMMARY
FROM (
SELECT
ID,
SUM(points) AS SUMMARY
FROM my_table
GROUP BY ID
ORDER BY SUMMARY DESC
) t2
WHERE ID=1234)
How can I remove duplicate query or reuse selection results?
Maybe my request is completely incorrect?
I'm pretty sure your query is identical to:
SELECT ID, SUM(points) AS SUMMARY
FROM my_table
GROUP BY ID
HAVING SUMMARY >= (SELECT SUM(points) FROM my_table WHERE ID=1234)
ORDER BY SUMMARY DESC
SQL Fiddle demonstration
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