首页 > 解决方案 > MYSQL:我有两张表。我想在表 1 中使用 FK 显示表 1 和表 2 中的数据

问题描述

我正在尝试从两个表中获取数据。表 1 称为位置,表 2 称为餐厅。Location 有一个名为RestaurantID (FK) 的字段,它是Restaurant中的主键。我想显示表Location中“Location”列的值和Table Restaurant中的“RestaurantID”(Restaurant 中的 PK 和 Location 中的 FK)以及“ Name ”值。我在下面附上了 MySQL 表。

表:位置

表:餐厅

标签: mysql

解决方案


加入应该可以解决问题:

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

推荐阅读