How to filter one database query by another

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dennis_doug
Forum Newbie
Posts: 5
Joined: Fri Mar 04, 2005 10:56 pm

How to filter one database query by another

Post 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)
User avatar
smpdawg
Forum Contributor
Posts: 292
Joined: Thu Jan 27, 2005 3:10 pm
Location: Houston, TX
Contact:

Post 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)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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>'
Post Reply