woocha Posted August 14, 2008 Share Posted August 14, 2008 Hey Guys... I am trying to do a simple SQL query on 1 Database but from 2 seperate tables. I have one table 'products' with an int id auto increment and I have another table 'items' with an int product_id . What I want to do is, make a one line SQL query that will grab any results where product_id {from table 1} = id{from table 2} Anyone have any experience with this? Thanks guys Quote Link to comment Share on other sites More sharing options...
unkwntech Posted August 14, 2008 Share Posted August 14, 2008 SELECT * FROM table1, table2 WHERE table1.product_id = table2.id AND OTHER CONDITIONS HERE Quote Link to comment Share on other sites More sharing options...
woocha Posted August 14, 2008 Author Share Posted August 14, 2008 Hey Thank you for the quick reply... The code I was trying is: $query = "SELECT instock FROM inventory where instock < 1 and products.item_number = inventory.id"; That is very similar to what you suggested, but mine did not work. Can you see anything I might try differently? Quote Link to comment Share on other sites More sharing options...
unkwntech Posted August 14, 2008 Share Posted August 14, 2008 You did not seelct and data from the second table, look at how my query is written. SELECT * FROM table1, table2 WHERE table1.product_id = table2.id AND table1.instock < 1 from BOTH tables --^----^ ^ then you have to reference each column with the table name --^ and specify the table1.product_id = table2.id before your other conditions Quote Link to comment Share on other sites More sharing options...
adam84 Posted August 14, 2008 Share Posted August 14, 2008 SELECT i.instock FROM inventory i, products p WHERE i.instock < 1 AND p.item_number = i.id Quote Link to comment Share on other sites More sharing options...
woocha Posted August 14, 2008 Author Share Posted August 14, 2008 You did not seelct and data from the second table, look at how my query is written. SELECT * FROM table1, table2 WHERE table1.product_id = table2.id AND table1.instock < 1 from BOTH tables --^----^ ^ then you have to reference each column with the table name --^ and specify the table1.product_id = table2.id before your other conditions SWEET !! That worked like a charm...I thank you for the tip. Your post squared it away for me. Thank you 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.