我的mysql数据库中有两个表:-餐馆- restaurant_type
餐厅有三栏:id,idtype,name
例子:
restaurant_type有2列:idtype,typename
例子:
现在,我的sql请求是:
mysql_query("select distinct
restaurant.idtype,
restaurant_type.idtype,
restaurant_type.typename
from restaurant,restaurant_type
where restaurant.idtype=restaurant_type.idtype
order by restaurant_type.typename");但我不知道如何添加But函数来搜索不同的idtype。
帮我的主意?
谢谢!
发布于 2015-10-12 08:05:05
你的桌子计划错了。您将N:M关系(餐馆<->类型)视为1:N,而不是。因此,您需要的不是两个表,而是三个表:
id,name)id,name)id_restaurant,id_type)示例数据:
餐厅========== 1,餐厅名称12,餐厅名称2 ==== 1,沙拉2,汉堡3,牛排餐厅4,日式5,=============== 1,1,2,2,2,3,5型炸薯条餐厅
然后是你的问题:
SELECT DISTINCT restaurant.name, type.name
FROM restaurant,restaurant_type
WHERE restaurant.id = restaurant_type.id_restaurant and restaurant_type.id_type = type.id
ORDER BY type.namehttps://stackoverflow.com/questions/33075607
复制相似问题