简体   繁体   English

试图从另一个表中获取价值-SQL

[英]trying to get value from another table - sql

I have two tables: 我有两个表:

Food and Aisle. 食物和过道。

Food contains the fields foodID, Name, Cost, and Sale Price.

Aisle contains foodaisleID, Aisle Name, Aisle Location AND foodID

What I am doing is displaying all of the foodaisleID's on my page along with the Aisle Name and the Aisles location (doing this from the Aisle table). 我正在做的是在我的页面上显示所有foodaisleID以及过道名称和过道位置(从过道表中执行此操作)。

What I am trying to do is along with displaying all the foodaisleIDs, Aisle Name and Aisle Location, I'm trying to display the foodID and the food name too, but as that's in a different table I have no idea how to display the corresponding one. 我想做的就是显示所有foodaisleID,过道名称和过道位置,我也试图显示foodID和食品名称,但是由于在另一个表中,我不知道如何显示相应的一。 (If I choose a specific Aisle, I want to display only the foods in that aisle). (如果我选择一个特定的过道,我只想显示该过道中的食物)。

My current SQL statement is: 我当前的SQL语句是:

"SELECT * FROM aisle WHERE foodaisleID = $foodaisleID"

and then I am echo'ing out the aisle name etc, any idea how I could incorporate the other table into the statement so I can echo the food name and cost too? 然后我回显了过道名称等信息,您知道如何将另一个表合并到语句中,这样我也可以回显食品名称和成本吗?

I have looked at "joins" but not sure how to actually implement them in 我已经看过“联接”,但是不确定如何在

Might be worth making foodAisleID a foreign key inside the Food table (at this point I assume that it is a primary key inside the Aisle table, so correct me if I'm wrong) so that you display the foodID and food name that correspond to the correct foodAisleID. 可能值得将foodAisleID设置为Food表中的外键(此时,我认为它是Aisle表中的主键,如果我错了,请更正我),以便显示与之对应的foodID和food名称正确的foodAisleID。 Then you could use a join to retrieve the correct data from both tables. 然后,您可以使用联接从两个表中检索正确的数据。

 SELECT * FROM aisle JOIN food ON Food.FoodId = Aisle.FoodId WHERE foodaisleID = $foodaisleID

应该做的把戏

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

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