Jump to content

[SOLVED] MySQL Query from 2 tables id = product_id


woocha

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.