views:

66

answers:

5

I have 2 tables, one called "products" and one "images". The table "images" hold the images of each products, so I can have 5 image per product.

I want to make a select that retrive only 1 image for each product. I'm new to joins so i dont know how to solve this.

I'm trying with:

    SELECT * 
      FROM products
INNER JOIN images ON products.id=images.prod_id 
     WHERE products.cat='shoes'

I need to add a Limit 0,1 on images table. How I can do it?

Thanks in advance.

+2  A: 

Maybe a subselect is a better solution here.

Something like this:

SELECT
productId,
productName,
(SELECT imageData FROM Image i WHERE i.productId = productId LIMIT 1) AS imageData
FROM Products
Sjoerd
A: 

Take a look at DISTINCT

erenon
DISTINCT won't work because each image will have a unique value
OMG Ponies
A: 
SELECT * FROM products 
LEFT JOIN images ON products.id=images.prod_id
WHERE products.id='1' LIMIT 1

This will return the first image found for your product and all the product details.

If you want to retieve multiple products then I would suggest doing 2 queries.

SELECT product data

Loop through product data {
    SELECT image data LIMIT 1
}

Doing complex single queries can quite often end up being more expensive than a couple/few smaller queries.

Lizard
This query selects only one product, not one image per product.
erenon
This will only return a single product as the `limit` is being applied to the outermost query.
Donnie
A: 

The key here is correlated subqueries.

select
  *
from
  products p,
  (
  select
    *
  from
    images i
  where
    i.prod_id = p.id
  limit 1
  ) as correlated
where
  p.cat = 'shoes'
Donnie
I don't believe a derived table/inline view can utilize the correlation - I would expect this query to return an error, or only the same image record for every product. And the correlation is only for the image returned - there's no actual join criteria so the result will be a cartesian product...
OMG Ponies
+1: The world would be such a dark and unforgiving place without correlated subqueries.
manneorama
@OMG Ponies - you may be right, I'm not sure, don't have a DB in front of me. In that case, move the correlation into the `select` clause to get the same thing.
Donnie
@OMG - oh, and about the cartesian product. The correlated query is only returning 1 row, so, that's really ok.
Donnie
+1  A: 

It's best to avoid subqueries because they are slow in mysql.

If you want to get any image associated to product, you can do it in fast but not very nice way:

SELECT * 
FROM products
INNER JOIN images ON products.id=images.prod_id 
WHERE products.cat='shoes'
GROUP BY products.id

If you want to get a first image( by any criteria ), apply groupwise max techniques

Naktibalda
thanks! it seems to work correctly... anyway i dont understand how to use groupwise. I'll try to explain better what have to do my query:get all the products from category "shoes", and get the fist image of each product of category "shoes".
Luciano