Page 1 of 1

How to filter one database query by another

Posted: Sat Mar 05, 2005 9:59 pm
by dennis_doug
What I am hoping to do is filter one database query by another.

I have two databases and they both have a field call vehicle_type. What I want to happen is that vehicle_type from the products database filters what is shown from the accessories database.

These are my SQL queries

SELECT * FROM products WHERE id = %s",

SELECT * FROM accessories WHERE vehicle_type=''";

I think the accessories query should look like
SELECT * FROM accessories WHERE vehicle_type=''$vehicle_type";

but I don't know how to give $vehicle_type a value from the products database.

Hope this makes sense, any help would be greatly appreciated.

Thanks Dennis :0)

Posted: Sat Mar 05, 2005 10:07 pm
by smpdawg
Perhaps this will help. Fix the $product_id so it matches whatever variable you were going to send to the query.

Code: Select all

SELECT a.* FROM products p, accessories a WHERE (p.vehicle_type = a.vehicle_type) AND (p.id = $product_id)

Posted: Sun Mar 06, 2005 7:11 am
by JAM
Just a different approach, using JOINS...

Code: Select all

select
 accessories.*
from 
 accessories
 inner join products on products.vehicle_type = accessories.vehicle_type
where
 products.vehicle_type = '<whatever>'