this site is like an ecommerce site and i have two tables that i’m stuck on.
tbl_products
---------------
phash
product
price
desc
maxcount
tbl_product_images
-------------------
phash
thumb
large
tbl_products stores the products description
tbl_product_images stores the path of the thumb and large image of eac product.
phash is an md5 of the products name its what i use rather than the id or product name when matching things together.
what i’m having throuble with is say tbl_products has one record for a product and tbl_product_images
in relation to that product has 5 rows for its images.
how would i run the query?
$sql = "select
tbl_products.phash
tbl_products.product
tbl_products.price
tbl_products.desc
tbl_products.maxcount
tbl_product_images.phash
tbl_product_images.thumb
tbl_product_images.large
from tbl_products
inner join tbl_product_images
on tbl_products.phash = tbl_products_images.phash";
this will display 5 rows of records since tbl_product_images has 5 records.
the way i output is my traditional way
$query = $db->query("$sql");
while($row = $db->fetch($query))
{
....
}
i am really unsure how i’m going to do this, if you need more explanation to what i’m trying to explain please let me know. thanks
The simplest way is to limit the number of results coming from the images table – though that’s only appropriate if you don’t care which image is returned for each product:
You can add an ORDER BY [col] DESC to that LIMITed query if there’s some other column in the images table (like a date) you could use to determine which image appears.
(Note: If there were some column in tbl_product_images that determined which image to return, you could either put that in a
WHEREclause on the query, or include it in the join criteria, like:ON tbl_products.phash = tbl_product_images.phash AND tbl_product_images.color = 'Blue')Further, if you are trying to get a product with all of its images, but only want one row in the resultset, it may be possible to “pivot” the set of images into columns. However, this is tricky if you can have an arbitrary (or simply large) number of images per product. You may be better off just returning multiple rows per product, and handling the results in your application code…