@
@
@atambalasi2016-02-29 13:41:04
PHP
@atambalasi, 2016-02-29 13:41:04

How to get record with max id?

table posts

id  link    user_id
22 www   44
23  ddd    44
33  ddd    55

Table comments
id   user_id, some_id
2     44           66
3     55           66

Have a request
SELECT posts.id,  posts.link, posts.user_id FROM posts WHERE posts.user_id IN (SELECT comments.user_id FROM comments WHERE some.id=66)  GROUP BY posts.user_id

Result
post.id posts.link, posts.user_id
22 www.az.ru 44
50 www.me.ru 55 As a
result, I get what I need, but the user has a lot of posts, how to get exactly the last added post.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2016-02-29
@karminski

SELECT posts.id,  posts.link, posts.user_id FROM posts WHERE posts.user_id IN (SELECT comments.user_id FROM comments WHERE some.id=66)  WHERE MAX(post.id)

In general, if you need to get exactly the "last added post", then it's better to add a timestamp column and choose MAX(timestamp). After all, the "last" criterion implies a check by the time of adding IMHO.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question