Jump to content

Recommended Posts

Hi all,

I am trying to do something with symfony, but first i should know how its done in mysql. I have 2 tables:

products and images. Each product can have multiple images. And joining these two tables is very easy. But that way i recieve the images as array which i have to order by the field place (as i dont know how to order them by a field during the join). and then show the 1st. Which is a lot of phpcode for nothing. Another way is that what i am using now. During the loop of products call a simple query for the first image. But that can produce 1000s of queries.

 

So my question is how to join only 1 image (the firs image not by PK but by another field place) to the product. Someone has proposed group by ... but i just dont have the knowledge or the idea on how to do it.

I am opened to suggestions. Thanks!

Link to comment
https://forums.phpfreaks.com/topic/79791-solved-join-only-1-element/
Share on other sites

Well yes, but even if not if they are ordered by place the first image will be the one with lowest place value. And if there are no images for the certain product i will just link to a local no-image.gif for example.

Thats all. Remember it has to be done in one query, here is where it gets tricky :(

 

SELECT p.prodname, i.imagename
FROM product p INNER JOIN image i ON p.prodcode = i.prodcode
INNER JOIN
(SELECT prodcode, MIN(place) as minplace FROM image
GROUP BY prodcode) as X ON i.prodcode=X.prodcode AND i.place = X.minplace

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.