chrischen Posted February 2, 2008 Share Posted February 2, 2008 So I have a merchant table with merchant details. I want to be able to search for merchants by category. I have a category table that ties merchant IDs to category IDs so that merchants can have more than one category. But the problem I have is how do I make it search by a category if the category is in another table. [search Box] [Drop down list of categories] [go] Results page should display merchant details pulled from the merchant table based on the query but only on in a specific category. In the category table there is merchantId and categoryId. And specific merchants might have more than one entry in that database if it falls into another category. I have everything else working except this search by category. Thanks. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 3, 2008 Share Posted February 3, 2008 SELECT m.merchant_name FROM merchant m INNER JOIN category c ON c.merchantID = m.id WHERE c.categoryID = '$selectedCategory' Quote Link to comment Share on other sites More sharing options...
chrischen Posted February 3, 2008 Author Share Posted February 3, 2008 Great thanks for the fast reply! I have one more question and it is about the way I store category data. Is it more efficient to store categories in the merchants table by comma delimiting the category ids (for example under the category column: 12,25,1,7) or by doing it the way I am right now with a separate table that matches category ID to merchant ID? Thanks. Quote Link to comment Share on other sites More sharing options...
chrischen Posted February 3, 2008 Author Share Posted February 3, 2008 SELECT m.merchant_name FROM merchant m INNER JOIN category c ON c.merchantID = m.id WHERE c.categoryID = '$selectedCategory' What's the m in "merchant m" and c in "category c" In m.merchant_name m is the table name right? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 3, 2008 Share Posted February 3, 2008 That is called aliasing. It basically just shortens the table name for the query so you don't have to type it out all the time. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 3, 2008 Share Posted February 3, 2008 Great thanks for the fast reply! I have one more question and it is about the way I store category data. Is it more efficient to store categories in the merchants table by comma delimiting the category ids (for example under the category column: 12,25,1,7) or by doing it the way I am right now with a separate table that matches category ID to merchant ID? Thanks. As you have it now, normalised. Don't even consider 12,25,1,7 Quote Link to comment Share on other sites More sharing options...
Barand Posted February 3, 2008 Share Posted February 3, 2008 That is called aliasing. It basically just shortens the table name for the query so you don't have to type it out all the time. That's part of the story. Sometimes it's essential if you meed to reference the same table twice in a query eg http://www.phpfreaks.com/forums/index.php/topic,180282.msg804214.html#msg804214 Quote Link to comment Share on other sites More sharing options...
chrischen Posted February 4, 2008 Author Share Posted February 4, 2008 Ok great thanks! By the way it worked perfectly! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.