我有两个表restaurants
和meals
。餐厅有
`id`
`name`
`menu`
`image`
`text`
`address`
餐有
`meal_id`
`meal_name`
`meal_image`
`meal_weight`
`meal_price`
`meal_menu`
我认为如何连接它们
meal.restaurant.id
。这是一种好方法还是更好地制作另一个(第三个)表并将它们连接在那里。您能否提供一个查询,询问如何选择ID = 1的餐厅仅显示该餐厅的餐点。现在,我认为要在
meal_menu
行中保留餐厅ID。 最佳答案
创建数据透视表,例如
CREATE TABLE meal_restaurant
(
meal_id int not null,
restaurant_id int not null,
primary key (meal_id, restaurant_id),
foreign key (meal_id) references meals (meal_id),
foreign key (restaurant_id) references restaurants (id)
);
现在要选择特定餐厅的所有餐点
SELECT m.meal_id, meal_name, meal_image, meal_weight, meal_price
FROM meals m JOIN meal_restaurant mr
ON m.meal_id = mr.meal_id
WHERE restaurant_id = 1
这是一个SQLFiddle演示