php - 防止重复的行在mysql中返回

标签 php mysql duplicates

我在餐厅搜索中使用 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/

相关文章:

r - 展开给定条件的行

elasticsearch - Logstash 重复数据

php - 如何在 PHP 类型提示中捕获 "catchable fatal error"?

php - PHP/MySql 注销后如何更新表

mysql - MySQL是否支持用户定义的数据类型

php - 如何获取Fetch_array数据

java - 使用StAX读取同名子节点

php - 如何在页面加载后立即提交表单

php - 我可以在不转义的情况下安全地将哪些字符插入到 SQL 数据库中?

php - fgetcsv 无法读取以 mac 格式的 csv 文件结尾的行,有更好的解决方案吗?