0

I need to get data from multiple tables and put it into a subform.

The SubForm columns are 'product name' and 'quantity' and they need to list out the products relating to the order ID.

The tables in question are:

PRODUCTS(productID,productName)

ORDER(orderID,prodID,quantity)

Where the prodID in ORDER refers to the PRODUCTS table.

As you can see, the problem is that the name of the product is in a different table to the order. So, the data I need to get is:

Products(productName)

Order(quantity)

In relation to the orderID.

How can I use a SQL query to get this data? I am aware of joins and so on, but I just can't see how to apply it.

Thank you. I hope this makes sense.

3
  • Design seems a bit odd to me I would expect another table: orderediTems, so that way you are not repeating order information (date of order order by ship to etc) for each productID on the order. Or does order have an "Order_header" or something? Commented Jan 4, 2012 at 11:33
  • @xQbert I do have a ORDRERITEMS table and an ORDERS table, yes. Sorry, I should have made that clearer. Commented Jan 5, 2012 at 11:34
  • possible duplicate of Simple SQL Select from 2 Tables (What is a Join?) Commented Jun 17, 2012 at 18:29

3 Answers 3

3

This is a simple inner join between the two tables to return the rows you want:

SELECT P.PRODUCTNAME, O.QUANTITY
FROM PRODUCTS P INNER JOIN ORDER O ON P.PRODUCTID = O.PRODID
WHERE O.ORDERID = <order id>
Sign up to request clarification or add additional context in comments.

Comments

3
SELECT
  PRODUCTS.productName AS productName,
  `ORDER`.quantity AS quantity
FROM
  `ORDER`
  INNER JOIN PRODUCTS on `ORDER`.prodID=PRODUCTS.productID
WHERE
 ..

You migh also want to rename the table ORDER - using reserved words as table names is not the best of styles.

3 Comments

+1 for noting that the ORDER table will need to be quoted, although only MySQL (as far as I know) uses backquotes.
Truly sorry, having SQL and MySQL being synonymous for too long I suspect.
I notice from the OP's profile that his other tags include Apache, PHP and Python - chances are he is using MySQL.
0

Select p.productname, q.quantity from product_table p, quantity_table q where p.productId = q.productId;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.