Posted on: 06/18/10 01:41pm
By: ::Ben
Hello,
Here is my code
Text Formatted Code
// Get products from database
$sql = "SELECT p.id,p.name,p.short_description,p.price,p.download,i.pi_filename
FROM {$_TABLES['paypal_products']} AS p
LEFT JOIN {$_TABLES['paypal_images']} AS i
ON i.pi_pid = p.id AND i.pi_img_num = 1
";
I need the same request with i.pi_img_num >=1 but I would to limit the result to 1 raw.
If i.pi_img_num = 1, it's ok stop here, or if i.pi_img_num = 2 ok stop there...
I read that I must include a subselect but do not succedd yet. Any help would be appreciated.
Thanks,
::Ben
Re: Need some help on an sql and left join subselect
Posted on: 06/18/10 04:25pm
By: jmucchiello
Can you be more specific? Are you trying to get a random image from the set of images associated with each product? I'm not really understanding what you mean by "but I would to limit the result to 1 raw. " OR "If i.pi_img_num = 1, it's ok stop here, or if i.pi_img_num = 2 ok stop there".
Can you create a few "fake" table listings and explain what you want to output:
Text Formatted Code
products:
id name ...
1 abc
2 def
images:
pi_pid img_num filename
1 1 abc_1
1 2 abc_2
1 3 abc_3
2 1 def_1
2 2 def_2
What is the output you are expecting?
Re: Need some help on an sql and left join subselect
Posted on: 06/18/10 05:15pm
By: ::Ben
The actual code give me each product and the associated image when pi_img_num=1.
Like this I can print a list of products with only the first image for each product.
Text Formatted Code
products:
abc picture abc_1
def picture def_1
If I delete this image, then I would like my request to give me, if the image exist, the next available one for the product (pi_img_num=2). So, there is still one image per produt on the main list.
Text Formatted Code
products:
abc picture abc_1
def picture def_2
Thank you for your help;
::Ben
Re: Need some help on an sql and left join subselect
Posted on: 06/18/10 07:59pm
By: Laugh
$sql = "SELECT p.id,p.name,p.short_description,p.price,p.download,i.pi_filename
FROM {$_TABLES['paypal_products']} AS p
LEFT JOIN {$_TABLES['paypal_images']} AS i
ON i.pi_pid = p.id AND i.pi_img_num = 1
";
I would try something like this (I believe this will work it has been a while for me with sub queries).
SELECT p.id,p.name,p.short_description,p.price,p.download,(SELECT i.pi_filename FROM {$_TABLES['paypal_images']} AS i WHERE p.id = i.pi_id ORDER BY pi_img_num LIMIT 1) pi_filename
FROM {$_TABLES['paypal_products']} AS p
This should return all items which have at least 1 image. Sub queries can reference the outer query and be located either in the select or where clause.
Search for "subquery" on the MySQL site for more information (I would have included the url but the post was flagged as SPAM).
Re: Need some help on an sql and left join subselect
Posted on: 06/19/10 06:38am
By: ::Ben
I finally found the query I needed:
Text Formatted Code
$sql = "SELECT p.id,p.name,p.short_description,p.price,p.download,i.pi_filename
FROM {$_TABLES['paypal_products']} AS p
LEFT JOIN {$_TABLES['paypal_images']} AS i
ON i.pi_pid = p.id AND i.pi_img_num = (SELECT pi_img_num FROM {$_TABLES['paypal_images']} WHERE pi_img_num >= 1 AND pi_pid = p.id LIMIT 1)
";
maybe there is several approch but this one get products from database only once with no image if there is no image available or the first one if one or more are available.
Thanks.
::Ben
Re: Need some help on an sql and left join subselect
Posted on: 06/20/10 03:52pm
By: jmucchiello
Quote by: cordisteI finally found the query I needed:
Text Formatted Code
$sql = "SELECT p.id,p.name,p.short_description,p.price,p.download,i.pi_filename
FROM {$_TABLES['paypal_products']} AS p
LEFT JOIN {$_TABLES['paypal_images']} AS i
ON i.pi_pid = p.id AND i.pi_img_num = (SELECT pi_img_num FROM {$_TABLES['paypal_images']} WHERE pi_img_num >= 1 AND pi_pid = p.id LIMIT 1)
";
maybe there is several approch but this one get products from database only once with no image if there is no image available or the first one if one or more are available.
Thanks.
::Ben
If you care, that SQL will not work with MS SQL. Try:
Text Formatted Code
$sql = "SELECT p.id,p.name,p.short_description,p.price,p.download,i.pi_filename
FROM {$_TABLES['paypal_products']} AS p
LEFT JOIN {$_TABLES['paypal_images']} AS i
ON i.pi_pid = p.id AND i.pi_img_num IN (SELECT MIN(pi_img_num) FROM {$_TABLES['paypal_images']} WHERE pi_img_num >= 1 AND pi_pid = p.id)
";
Re: Need some help on an sql and left join subselect
Posted on: 06/21/10 02:56am
By: ::Ben
If you care, that SQL will not work with MS SQL
Thank you Joe for this. :chestslam:
::Ben