简体   繁体   English

从多个表中获取数据

[英]Get data from multiple tables

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. SubForm列是“产品名称”和“数量”,它们需要列出与订单ID相关的产品。

The tables in question are: 有问题的表是:

PRODUCTS(productID,productName)

ORDER(orderID,prodID,quantity)

Where the prodID in ORDER refers to the PRODUCTS table. ORDER中的prodID指的是PRODUCTS表。

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 . 关于orderID

How can I use a SQL query to get this data? 如何使用SQL查询获取此数据? 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. 我希望这是有道理的。

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>
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. 您可能还想重命名表ORDER-使用保留字作为表名并不是最好的样式。

从product_table p,quantity_table q中选择p.productname,q.quantity,其中p.productId = q.productId;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM