0

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.

2
  • Do you have another table not database, is it ? Commented Jun 10, 2012 at 9:51
  • 1
    you have examples in mysql documentation: dev.mysql.com/doc/refman/5.0/en/join.html Commented Jun 10, 2012 at 9:53

2 Answers 2

1
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
Sign up to request clarification or add additional context in comments.

2 Comments

It should work, you are probably doing something wrong, but query is correct. Are you trying this in your php application or mysql console?
Then you have mistake while outputting related rows, can't tell more without your code.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.