OldWest Posted November 10, 2010 Share Posted November 10, 2010 I've got two tables. I am trying to JOIN both tables by product_id (both tables contain this field), and WHERE status equals A in table1. Here is what I have so far with absolutely no luck! $query = "SELECT product_id FROM products, product_descriptions WHERE products.status='A'"; From what I understand, I am SELECTing product_id from both tables (I also confirmed this much of the statement is working). FROM 2 tables as noted: products, product_descriptions. WHERE products status field equals value A... Obviously my query is screwed. I've spent some hours on dev.mysql.com and have not been able to piece this together properly.. Thanks for any advice. Does anyone know why this is not working as expected? Quote Link to comment https://forums.phpfreaks.com/topic/218323-join-query-for-2-tables-cannot-get-a-valid-result-wout-error/ Share on other sites More sharing options...
DavidAM Posted November 10, 2010 Share Posted November 10, 2010 You have to JOIN the two tables on a common field. Otherwise, you will get a Cartesian product (every one in each table matched with every row in the other table(s)) $query = "SELECT product_id FROM products JOIN product_descriptions ON products.product_id = product_descriptions.product_id WHERE products.status='A'"; Quote Link to comment https://forums.phpfreaks.com/topic/218323-join-query-for-2-tables-cannot-get-a-valid-result-wout-error/#findComment-1132779 Share on other sites More sharing options...
OldWest Posted November 10, 2010 Author Share Posted November 10, 2010 Thank you for that tip.. I was so darn close! The query you sent would not work on my version of mysql .. SELECT product_id was required to be SELECT product_descriptions.product_id But it appears to be working now with that small tweak. Thank you for your advice. Quote Link to comment https://forums.phpfreaks.com/topic/218323-join-query-for-2-tables-cannot-get-a-valid-result-wout-error/#findComment-1132787 Share on other sites More sharing options...
DavidAM Posted November 11, 2010 Share Posted November 11, 2010 Oops. Sorry about that. Since the product_id column exists in more than on table, it has to be qualified by the table name. Quote Link to comment https://forums.phpfreaks.com/topic/218323-join-query-for-2-tables-cannot-get-a-valid-result-wout-error/#findComment-1133076 Share on other sites More sharing options...
OldWest Posted November 11, 2010 Author Share Posted November 11, 2010 Mental fragmentation! I think it's part and parcel with programming.. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/218323-join-query-for-2-tables-cannot-get-a-valid-result-wout-error/#findComment-1133140 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.