



Needless to say again but before using any of the above, they should be tested in your environment and cross tested against all the many other versions/rewritings of the query. The useful index is on posts (author_id, date, id) for MySQL and or on posts (author_id, date DESC) for Postgres. If you want to be precise about which one (of the tied) will be returned, the ORDER BY in the subquery can be modified (to ORDER BY pi.date DESC, pi.id DESC or ORDER BY pi.date DESC, a.name for example). This solution to the greatest-n-per-group problem matches also your request about ties, as it returns always one result per group. It's also best if there is an index or a table to find all the distinct author_id values and an additional index on the posts table for the group by. The assumptions are that the number of authors (the attribute we group by on) is small, compared to the number of posts (the table where we apply the group by).

It uses a LATERAL join in Postgres, which is available in 9.3+ versions (in SQL Server lingo CROSS/OUTER APPLY) and a simulation of this join in MySQL. This specific kind of query is often called greatest-n-per-group (there is even a tag for it!) and under certain assumptions, one of the many ways to write them, is often quite efficient in both MySQL and PostgreSQL. You should always test various different ways of writing the queries, on your tables, with the sizes and distribution you expect to have on production, with your hardware and configuration settings, to decide which rewritings of the queries should be kept. Not always at least.Įfficiency depends on many different things, like the specific DBMS, the specific version (different versions have different improvements on the optimizer and the available syntax), the type of columns, the indexes available, the size of the tables and distribution of values, the hardware the server is running, the configuration settings etc. If you aim is to have queries with maximum efficiency, none of the above queries is really the best. ) p1 INNER JOIN posts p2 USING (author_id, date) ON p1.author_id = p2.author_id AND p1.date = p2.max_date LEFT JOIN posts p2 ON p1.author_id = p2.author_id AND p1.date < p2.date How should I modify these queries to return exactly one post per author? SELECT p1.* Then the returned result set contains all such posts. Thanks, both links provide answers to my question with some exception.Īll the following queries give the same result (which one is the most efficient btw?) The issue is when there is more than one post from the same author with the same date. I need to select one most recent post for each author. I use recent versions of PostgreSQL and MySQL. ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 Insert into `tableA`(`id`,`fullname`,`social_num`,`email`) values (1,'David TABLE `tableB` ( ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 LEFT JOIN tableB ON tableA.social_num = tableB.social_numīut how do i show all of other user from tableA togather as well, like Morris Q
MYSQL JOIN LATEST RECORD HOW TO
How to join them together, so that, i get the latest record on tableA and latest record on tableB link by social_num? I manage to get the latest record by auto increment id by using the following sql for tableA and tableB: SELECT * I had 2 table, tableA and tableB looking like this: This question has been asked at here as well.
