I was just wondering if anybody would be able to help me with a SQL join as I’m fairly new to MySQL. I’m looking to join two tables, here my tables and what I’m looking for the end result to look like, thanks in advance!
TABLE ARTICLES
article_id title body date category author
1 Lorem Lorem ipsum.. 1/1/2013 1 Mitchell
TABLE CATEGORIES
category_id category
1 Web Design
SQL STATEMENT
select categories.category_id, categories.category as category
from categories left outer join articles on categories.category = articles.category
SQL RESULT I’M LOOKING FOR
article_id title body date category author
1 Lorem Lorem ipsum.. 1/1/2013 Web Design Mitchell
Your joining
ONcondition should be betweencategories.category_idandarticles.category. Otherwise, you just need to add more columns to yourSELECTlist, as you’re nearly there.Here it is in action: http://sqlfiddle.com/#!2/0e0c1/1
I note that you have listed your date in the format
m/d/yyyyhere instead ofYYYY-MM-DDwhich is MySQL’s default output. If that is indeed how you have stored it in your table (as aVARCHAR()rather than a properDATEtype), I would recommend changing that strategy before you get too far into the project to change it easily.