It is necessary to deduce articles from different authors from the database, and so that the authors do not repeat and the last article from this author is displayed. I make a request

SELECT write_id, author_id ,name_author, write_date, write, mini_img FROM writes WHERE author_id = '$UserId' GROUP BY author_id 

he prints everything without repetition, but the author’s record displays the very first one, how to display the last one?

  • It is displayed without repetitions, because the author is defined by ID, if all authors with their last record are needed, a SELECT DISTINCT query must be started. - Gorets

3 answers 3

If the date you have with the time is stored, then you can:

 SELECT wr.author_id ,wr.name_author, wr.write_date, wr.write, wr.mini_img FROM writes wr JOIN (SELECT author_id, max(write_date) AS write_date FROM writes GROUP BY author_id) w ON wr.author_id = w.author_id AND wr.write_date = w.wdate; 

If only the date and there will be several entries for one number, then extra ones will be displayed.

  • thanks, works - BraGB
 SELECT write_id, author_id ,name_author, write_date, write, mini_img FROM writes WHERE author_id = '$UserId' AND MAX(write_date) GROUP BY author_id 

probably so

  • It does not work, I tried to start with SELECT DISTINCT, the same thing - BraGB
  • is write_date the last publication date? - Gorets
  • this is just the date of publication of the article, well, respectively, if there are two articles, the first is valid from January 1, the second from January 3, you need to print the second, and the first is BraGB
  • SELECT query write_id, author_id, name_author, MAX (write_date), write, mini_img FROM writes WHERE author_id = '$ UserId'GROUP BY author_id - Gorets
  • Something I have already forgotten sql-ku =), it seems, GROUP BY - in the query is superfluous, because there will be only 1 author in the select. can such a suitable SELECT DISTINCT write_id, author_id, name_author, MAX (write_date), write, mini_img FROM writes WHERE author_id = '$ UserId' - Gorets

Why not try using ORDER BY desc write_date ?

  • All entries will be displayed, only sorted by date, including repetitions, if you share GROUP BY author_ID ORDER BY write_date DESC, the result will be the same as without ORDER BY - BraGB
  • SELECT query write_id, author_id, name_author, write_date, write, mini_img, max (write_date) as "wdate" FROM which writes WHERE author_id = '$ UserId' GROUP BY author_id having write_date = wdate - Vfvtnjd