Skip to content

Instantly share code, notes, and snippets.

@betweenbrain
Created August 14, 2014 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betweenbrain/7a2c2abfecd7fee3d963 to your computer and use it in GitHub Desktop.
Save betweenbrain/7a2c2abfecd7fee3d963 to your computer and use it in GitHub Desktop.
MySQL Join with Subquery Selecting Joined Data from Last Month
SELECT
table1.id,
table1.firstName,
table1.lastName,
a.sales
FROM `table1`
LEFT JOIN
( SELECT
firstName,
sales,
date
FROM `table2` WHERE MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
) as a
ON table1.firstName = a.firstName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment