Jump to content

SELECT Query on Two Tables


koopkoop

Recommended Posts

How do I select a single row from two tables with a common field - based on a unique sharked key.

 

For example, I've got two tables "products" and "prices". I want to select all the fields from each based on the shared "itemnumber" key.

 

This returns zero results:

SELECT * FROM prices,products WHERE products.itemnumber="11004NA" AND prices.itemnumber="11004NA"

 

 

This returns 1 result - which is exactly what it should be doing:

SELECT * FROM prices,products WHERE products.itemnumber LIKE"%11004NA%" AND prices.itemnumberLIKE "%11004NA%"

 

Why doesn't the first one work? What's the proper way of writing this?

 

 

Link to comment
Share on other sites

To further clarify, the number of fields between the two are completely different along with the names:

 

eg.

 

PRODUCTS

itemnumber,name,description

 

PRICES

itemnumber,prices,caseweight,caseqty

 

I want to search the two for a unique product and get a result like

itemnumber,name,description,prices,caseweight,caseqty

 

Thanks.

 

 

 

 

Link to comment
Share on other sites

You will need to use join statements.

 

SELECT D.name, D.description, P.prices, P.caseweight, P.caseqty FROM Products D INNER JOIN Prices P ON D.itemnumber = P.itemnumber WHERE P.itemnumber = "11004NA" AND D.itemnumber = "11004NA"

 

Something like that should work, or get you started anyway.

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.