我的mysql数据库中有两个表:
-餐厅
-餐厅类型
餐厅有三栏:id
,idtype
,name
例子:
1,1-2,餐厅名称
2,2-3-5,餐厅名称
餐厅类型有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");
但我不知道如何添加explode函数来搜索不同的
idtype
。想帮我吗?
谢谢您!
最佳答案
你的餐桌安排错了。你把一个N:M关系(餐馆类型)当作1:N,而不是1:N。因此,您需要3个表而不是2个表:
餐厅(id
,name
)
类型(id
,name
)
餐厅类型(id_restaurant
,id_type
)
示例数据:
Restaurant
==========
1, restaurant name 1
2, restaurant name 2
Type
====
1, Salad
2, Burguer
3, Steak House
4, Japanese
5, French Fries
Restaurant type
===============
1, 1
1, 2
2, 2
2, 3
2, 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.name
关于php - SQL + PHP加入并爆炸,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33075607/