I'm writing this tutorial because it took me awhile to figure out that MySQL's SUBSTR() function is not identical to PHP's substr(). SUBSTR(field, startChar, length) is the syntax, but instead of being zero based it starts at 1. Here's a real world example: imagine a db table with a field or column named dte, it is a datestamp such as YYYY-MM-DD and there are a lot of them... you only need to know the YYYY-MM pattern. Instead of using PHP, you can solve the puzzle closer to the core, by utilizing one of MySQL's built in functions.
Remember, MySQL's SUBSTR() function is not zero based, it starts at 1.
SELECT SUBSTR(dte, 1, 7) as ym FROM table ORDER BY dte DESC GROUP BY ym;
This returns a MySQL result with a record of each unique YYYY-MM pattern which was found.
Google mysql string functions for some more great info.