我的 mysql 数据库中有 2 个表: - 餐厅 - 餐厅类型
餐厅有 3 列:id
、idtype
、name
Example :
- 1, 1-2, Restaurant name
- 2, 2-3-5, Restaurant name
restaurant_type 有 2 列:idtype
, typename
Example :
- Salad
- Burger
- Steak house
- Japanese
- French fries
在那一刻,我的 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,但事实并非如此。所以你需要 3 个而不是 2 个表:
- 餐厅(
id
,名称
) - 类型(
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/