Answer the question
In order to leave comments, you need to log in
Join tables, LIMIT 1
Hello. Can you tell me how to correctly format the SQL query to get the only and last value in the second table?
SELECT
t1.id,
t1.title,
t2.user AS `last_user`,
t2.datetime AS `last_date`
FROM
`topics` AS t1 INNER JOIN
`comments` AS t2 ON (t2.id_topic = t1.id)
GROUP BY
t1.id DESC
SELECT t2.datetime, t2.user FROM `comments` AS t2 WHERE t2.id_topic = t1.id ORDER BY t2.id DESC LIMIT 1
Answer the question
In order to leave comments, you need to log in
For example like this:
select t1.*, c.user as `last_user`, c.datetime as `last_date` from topics as t1 inner join (select id_topic, max(id) max_id from comments c1 group by id_topic) t2 on (t2.id_topic = t1.id) inner join comments c on (c.id = t2.max_id);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question