Jump to content
Larry Ullman's Book Forums

Displaying Results When Foreign Key Has Duplicate Values


Recommended Posts

In the site I am creating, I have an images table that allows for duplicate values (2 only) which are the foreign key article_id. You see each article_id can have up to two images displayed on it. So in my images table each image_id row will have 2 article_id's in it. 

 

The trouble is that when I display results on my website using this data, I get double results. The query makes 2 rows appear, when I only want 1 to show on my page. When going through my code the computer sees 2 article_id's in the images table and displays 2 rows when I only want one.

 

I tried using the DISTINCT keyword in my query but that didn't work. Is there a better workaround for this, I really want to keep it like this, 2 images per article. Is there a way to change my table and make it more proper or make php only display 1 result. Or should I just create a separate images table for the second images?

 

$p = "SELECT DISTINCT i.article_id, i.inv_title, i.date_submitted, i.category_id, c.category_id, i.approved, c.category, u.image_name FROM articles AS i INNER JOIN categories AS c USING (category_id) INNER JOIN images AS u USING (article_id) WHERE c.category_id = $cat AND i.approved = 'approved' ORDER BY date_submitted DESC LIMIT $start, $display";

Link to comment
Share on other sites

 Share

×
×
  • Create New...