我在餐厅搜索中使用 php 完成了三个表。搜索可以选择按复选框状态按多种类型进行过滤。一些餐厅可能属于多种类型,并且在 type_stack 表中会有多个条目。
Table1 - **restaurant**
------+----------+----------
id + name + place
------+----------+----------
1 rest1 ny
2 rest2 la
3 rest3 ph
4 rest4 mlp
Table2 - **r_type**
------+----------+----------
id + name + code
------+----------+----------
1 type1 0
2 type2 1
3 type3 2
4 type4 3
Table3 - **type_stack**
------+----------+----------
id + rest_id + type
------+----------+----------
1 2 2
2 4 1
3 1 2
我想获取符合用户所选类型的所有餐厅。但问题是我多次去同一家餐厅。我只希望一行显示一次。
这是我的查询
SELECT restaurant.name, restaurant.place FROM restaurant, type_stack WHERE restaurant.id = type_stack.rest_id AND type_stack.type = '0' AND type_stack.type = '1' AND type_stack.type = '2' LIMIT 0 , 30
查询是根据复选框状态进行的!在本例中,选择了类型 0、1 和 2。
最佳答案
将您的语句更改为(您需要 DISTINCT):
SELECT DISTINCT restaurant.name, restaurant.place FROM restaurant, type_stack WHERE restaurant.id = type_stack.rest_id AND type_stack.type = '0' AND type_stack.type = '1' AND type_stack.type = '2' LIMIT 0 , 30
关于php - 防止重复的行在mysql中返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3090048/