php - sql + php 连接和爆炸

标签 php mysql sql

我的 mysql 数据库中有 2 个表: - 餐厅 - 餐厅类型

餐厅有 3 列:ididtypename

Example :

  • 1, 1-2, Restaurant name
  • 2, 2-3-5, Restaurant name

restaurant_type 有 2 列:idtype, typename

Example :

  1. Salad
  2. Burger
  3. Steak house
  4. Japanese
  5. 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/

相关文章:

php - 如何获取从开始日期到结束日期的数据?

php - 只允许创建条目的用户修改它

我无法在更新查询上复制的 MySql 错误

mysql - 无法创建表 'mydb.contact'(错误号 : 150)

javascript - 如何使用knex无条件删除表中的所有记录?

mysql - 如何从 LEFT JOIN mysql 表调用结果

c# - 如何使用逗号更改小数位的值,例如表中的 2,532.00 更改为 2532。

php - 如何在使用 Zend_Auth 进行身份验证后重定向到请求的 URI?

SQL 使用或选择值

php - 无法在服务器上保存文件