Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help selecting multiple items with complex query

Status
Not open for further replies.

mes123

Programmer
Jul 29, 2005
62
GB
I've got a db with 3 tables - products, products_description and accessories.

The accessories table contains 2 columns:
main_product_id
acc_product_id

If a product has accessories these can be found by performing a right join on the accessories table:

Code:
select 

p.products_id, 
pd.products_name, 
pd.products_description, 
p.products_model, 
p.products_quantity, 
p.products_image, 
pd.products_url, 
p.products_price, 
p.products_tax_class_id, 
p.products_date_added, 
p.products_date_available, 
p.manufacturers_id 

from 

TABLE_PRODUCTS p,  
TABLE_PRODUCTS_DESCRIPTION pd 


right join TABLE_ACCESSORIES pa 
on (
	p.products_id = pa.sub_product_id
and
	pa.main_products_id = $product_id

) where 

p.products_status = '1' and 
pd.products_id = p.products_id and 
pd.language_id = '$languages_id'

This works fine for locating the accessories for a single product.

However, I now need to be able to find the accessories for a range of products and I don't know how to modify the query.

Can anyone help?

Thanks in advance.

 
You could simply replace:[tt]
pa.main_products_id = $product_id[/tt]
with:[tt]
pa.main_products_id BETWEEN $start_prod AND $end_prod[/tt]
(for a single range)
or:[tt]
pa.main_products_id IN ($prod1,$prod2,$prod3)[/tt]
(for a list of products)
 
Thanks Tony, works a treat (pa.main_products_id IN ($prod1,$prod2,$prod3))

it's the first time i've come across the IN keyword - still learning SQL...


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top