clankill3r
clankill3r

Reputation: 9543

join mysql query

I have this query which is working fine:

$query = "SELECT adafruit_articles.id, adafruit_articles.title, adafruit_articles.timestamp  FROM adafruit_articles WHERE MONTH(timestamp)='$m' LIMIT 5";

I also have another database called adafruit_images that has the name of the image(s) for each article (some articles have no image, some have more).

The structure looks like this:

id  articleid   image

I want to combine the query from above that it also grabs the images. I'm only not that good in mysql so i was hoping someone could help.

Upvotes: 0

Views: 61

Answers (2)

roev
roev

Reputation: 1277

It should look something like this:

SELECT adafruit_articles.id, adafruit_articles.title, adafruit_articles.timestamp
FROM adafruit_articles 
JOIN adafruit_images
ON adafruit_images.articleid = adafruit_articles.id
WHERE MONTH(timestamp)='$m' 
LIMIT 5

Upvotes: 1

Choo
Choo

Reputation: 233

SELECT adafruit_articles.id, adafruit_articles.title, adafruit_articles.timestamp
FROM adafruit_articles
LEFT OUTER JOIN adafruit_images ON adafruit_images.articleid = adafruit_articles.id
WHERE MONTH(timestamp)='$m'
LIMIT 5

Upvotes: 1

Related Questions