MySQL - Latest row by set of users

Pal Bergstrom

I have a MySQL table where I would like to get the latest posts by "post_date" from a set of users in a particular group (they can belong to more than one group). I first get all users in the group. Then I try:

SELECT *, max(post_date) FROM posts 
         WHERE user_id IN ($matches) 
         GROUP BY user_id 
         ORDER BY post_date DESC

But this doesn't work. How do I solve it?

M Khalid Junaid

To get the latest posts from group of users, you can do so by using a self join

SELECT p.*
FROM posts p 
JOIN(
SELECT user_id , max(post_date) post_date
FROM posts 
GROUP BY user_id  
) pp
USING(user_id,post_date)
WHERE pp.user_id IN ($matches) /* i assume here you have user ids like IN (1,2,3)*/
ORDER BY p.post_date DESC

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mysql select distinct but latest row

From Dev

Mysql get latest row for status

From Dev

MySQL - join users latest level, not any level?

From Dev

Find the latest location of each users in a MySQL table

From Dev

MySQL join get latest valid row

From Dev

select latest row in mysql but primary is varchar

From Dev

mySQL - Selecting the latest row from a table

From Dev

Deleting Row from users table PHP MYSQL

From Dev

After inserting a row in mysql, the latest row climbs up based on date

From Dev

Set ID for each row in MySQL

From Dev

How to get Latest value of row in Cursor result set in SQL

From Dev

Select latest row in LEFT JOIN PHP MySQL CodeIgniter

From Dev

How to select a latest value row in mysql table for 2 days

From Dev

MySQL : How to get latest row on optional where clause using group by?

From Dev

MySQL updating same row by multiple users like counter

From Dev

MySQL updating same row by multiple users like counter

From Dev

MysQL : How to set default value for empty row?

From Dev

Set the value of second row to NULL MySQL

From Dev

MySQL : Get latest SCORE for each users where quiz_id = something

From Dev

For a specific user_id get the latest rows (time-wise) that this user is in with all other users, in MySQL

From Dev

Mysql set different UUID for different row in multi row insert

From Dev

Set password to postfix email users stored in mysql by php

From Dev

Display the latest row only

From Dev

Select row by latest datetime

From Dev

Remove duplicate rows based on composite key and keep the latest row entry mysql php

From Dev

MySQL grab row only if it's the latest ID that also matches a string (location_is)

From Dev

set file as latest file

From Dev

set Latest Event Info

From Dev

Mysql - Set key of row from recurring field in a 3 rows

Related Related

  1. 1

    mysql select distinct but latest row

  2. 2

    Mysql get latest row for status

  3. 3

    MySQL - join users latest level, not any level?

  4. 4

    Find the latest location of each users in a MySQL table

  5. 5

    MySQL join get latest valid row

  6. 6

    select latest row in mysql but primary is varchar

  7. 7

    mySQL - Selecting the latest row from a table

  8. 8

    Deleting Row from users table PHP MYSQL

  9. 9

    After inserting a row in mysql, the latest row climbs up based on date

  10. 10

    Set ID for each row in MySQL

  11. 11

    How to get Latest value of row in Cursor result set in SQL

  12. 12

    Select latest row in LEFT JOIN PHP MySQL CodeIgniter

  13. 13

    How to select a latest value row in mysql table for 2 days

  14. 14

    MySQL : How to get latest row on optional where clause using group by?

  15. 15

    MySQL updating same row by multiple users like counter

  16. 16

    MySQL updating same row by multiple users like counter

  17. 17

    MysQL : How to set default value for empty row?

  18. 18

    Set the value of second row to NULL MySQL

  19. 19

    MySQL : Get latest SCORE for each users where quiz_id = something

  20. 20

    For a specific user_id get the latest rows (time-wise) that this user is in with all other users, in MySQL

  21. 21

    Mysql set different UUID for different row in multi row insert

  22. 22

    Set password to postfix email users stored in mysql by php

  23. 23

    Display the latest row only

  24. 24

    Select row by latest datetime

  25. 25

    Remove duplicate rows based on composite key and keep the latest row entry mysql php

  26. 26

    MySQL grab row only if it's the latest ID that also matches a string (location_is)

  27. 27

    set file as latest file

  28. 28

    set Latest Event Info

  29. 29

    Mysql - Set key of row from recurring field in a 3 rows

HotTag

Archive