Today I found a question in a newsgroup asking for help with
creating a join. This join should take a date out of table a and
find the record in table b which has a date less than that of
table a, but still closest to that date. The tables should be
joined through the id column.
Here's my example data (I've changed it a bit from the original
request):
mysql> SELECT * FROM a;[Read more]
+-----+----+------------+
| row | id | d |
+-----+----+------------+
| 1 | 15 | 2005-12-25 |
| 2 | 20 | 2005-12-29 |
| 3 | 25 | 2006-01-12 |
| 4 | 30 | 2006-01-15 |
+-----+----+------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM b;
+-----+----+------------+
| row | id | d |
+-----+----+------------+
| 1 | 15 | 2005-12-19 |
| 2 | 15 | 2005-12-21 |
| 3 | 15 | 2005-12-27 |
| 4 | 20 | 2005-12-11 |
| 5 | 20 | 2005-12-30 | …