简体   繁体   English

MYSQL:我有两个表。 我想在表 1 中使用 FK 显示表 1 和表 2 中的数据

[英]MYSQL: I have two tables. I want to show data from table 1 and table 2 using FK in table 1

I am trying to fetch data from two tables.我正在尝试从两个表中获取数据。 Table 1 is called Location and Table 2 is called Restaurant.表 1 称为位置,表 2 称为餐厅。 Location has a field called RestaurantID (FK) which is the primary key in Restaurant . Location 有一个名为RestaurantID (FK) 的字段,它是Restaurant 中的主键。 I want to show the value of column "Location" from table Location and the "RestaurantID" (PK in Restaurant and FK in Location) as well as " Name " value from Table Restaurant .我想显示表Location 中“Location”列的值和“RestaurantID” (餐厅中的PK和位置中的FK)以及表餐厅中的名称”值。 I have attached MySQL table below.我在下面附上了 MySQL 表。

表:位置

表:餐厅

A join should do the trick:连接应该可以解决问题:

SELECT l.location, l.restaurantid, r.name
FROM   location l
JOIN   restaurant r ON l.restaurantid = r.restaurantid

暂无
暂无

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

相关问题 我有三张桌子。 我想要一张桌子。 重新设计我的桌子 - I have three tables. I want to have one table. ReDesign my table 从两个表中选择数据。 一张桌子是父母/孩子,我需要让父母没有关系 - Select data from two tables. one table is parent/child and I need to get the parents without a relation 我有 3 个有关系的表。 第一个表具有另一个两个表的外部 id。 我想知道获取数据的确切查询 - I have 3 tables with relation. The first table have foreign id of another both tables. I want to know the exact query of fetching the data 我有两个表A和B。我想使用内部联接将数据从表B插入到A - I have two tables both A and B. I want to insert data from table B to A using an inner join 复制相关的 MySQL 表。 我有一张桌子,有相关的桌子,我想要一个 function 来复制很多 - Copying related MySQL Tables. I have a table, with related tables, and I'd like to have a function to copy the lot 1张桌子,但两张桌子。 类似于mysql表的磁盘分区 - 1 table, but two tables. Something like disk partitioning for mysql tables 我有一个包含一组数据的表,我想使用Mysql显示来自其中的特定记录: - I have a table with a set of data and I would want to display specific records from it using Mysql: MySQL查询根据来自两个不同表的值选择一个表。 - MySQL query to select one table based on values from two different tables. 如何使用MySQL PHP将两个表中的数据插入到一个表中 - How do I Insert data from two tables into one table using MySQL PHP 我有两个表,我无法按名称分组并在 mysql 中显示与 header 的所有数据交易 - i have two table and i have problem to group by name and show all data transaction with header in mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM