[英]MySQL Select from multiple tables
I'm new to MySQL. 我是MySQL的新手。 I am creating a checkout page in PHP.
我正在用PHP创建一个结帐页面。 When the users select the items they want to buy and click "Add to Cart", a temporary table gets created which has the following fields (table name is temp):
当用户选择要购买的商品并单击“添加到购物车”时,将创建一个临时表,该表具有以下字段(表名称为temp):
+--------------+-----------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------+------+-----+-------------------+----------------+
| Cart_Item_ID | int(11) | NO | PRI | NULL | auto_increment |
| Item_ID | int(11) | NO | | | |
| Added_On | timestamp | YES | | CURRENT_TIMESTAMP | |
+--------------+-----------+------+-----+-------------------+----------------+
I'm only inserting to the Item_ID field which contains the ID of each item they bought (I'm populating the forms with item IDs). 我只插入包含他们购买的每个商品的ID的Item_ID字段(我将使用商品ID填充表单)。 What I want to do is look up the item's name and price that's stored in the Inventory table.
我想做的是查找“库存”表中存储的商品名称和价格。 Here's how that looks:
看起来是这样的:
+--------------+----------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------------+------+-----+-------------------+----------------+
| Inventory_ID | int(11) | NO | PRI | NULL | auto_increment |
| Item_Name | varchar(40) | NO | | | |
| Item_Price | float unsigned | NO | | 0 | |
| Added_On | timestamp | YES | | CURRENT_TIMESTAMP | |
+--------------+----------------+------+-----+-------------------+----------------+
So how would I pull out the Item_name and Item_Price fields from the Inventory table based on the Item_ID field from the temp table so I can display it on the page? 那么,如何根据临时表的Item_ID字段从库存表中提取Item_name和Item_Price字段,以便在页面上显示它们呢? I just don't understand how to formulate the query.
我只是不明白如何制定查询。 I'd appreciate any help.
我将不胜感激。 Thank you.
谢谢。
what i understand is that the Item_ID in temp table is referencing to the Inventory_ID in inventory table. 我了解的是,临时表中的Item_ID引用了库存表中的Inventory_ID。 based on this assumption you can use the following query.
基于此假设,您可以使用以下查询。
Select Item_Name, Item_Price from Inventory, Temp where Temp.Item_ID == Inventory.Inventory_ID
i guess this is what you want to do. 我想这就是你想要做的。
Thanks 谢谢
As it stands, you can't (unless Inventory_ID = Item_ID) 就目前而言,您不能这样做(除非Inventory_ID = Item_ID)
What you need is a way of JOINing the two tables together. 您需要的是将两个表联接在一起的方法。 In this instance, if Inventory_ID = Item_ID then the following is possible:
在这种情况下,如果Inventory_ID = Item_ID,则可以进行以下操作:
SELECT Item_Name,
Item_Price
FROM InventoryTable
INNER JOIN TempItemTable ON (InventoryTable.Inventory_ID = ItemTable.Item_ID)
If you want to filter for a particular item you can add the constraint: 如果要过滤特定项目,可以添加约束:
WHERE ItemTable.Item_ID = 27 --for example
That will join all the rows in your inventory table with matching rows in the Item table. 这会将库存表中的所有行与项目表中的匹配行连接在一起。
Jeff Atwood has a great (IMO) visual explanation of how JOINs work. Jeff Atwood对联接的工作原理有一个很好的 (IMO)视觉解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.