希望有人能帮我做一个MYSQL查询
我有 3 个数据库表:
- 兴趣点
- 兴趣点类型
- 兴趣点类型
points_of_interest
表包含有关某个区域的所有详细信息。
point_of_interest_type
表是一个类型列表,例如 Restaurant、Cafe、Museum 等。该表还有一个事件列(1 或 0)
poi_poi_types
是一个映射表 - 因此它包含兴趣点的 id 和兴趣点类型的 id
一个兴趣点可以有很多兴趣点类型,因此在 poi_poi_types 表中可能有多个条目
所以我想要的结果是返回一个 points_of_interest 列表,但只针对活跃的兴趣点类型
这是我目前的 codeignitor 查询,但它不正确 - 它返回 poi_poi_types 表中实际兴趣点的每个瞬间
$this->db->select('poi.*, poi_types.type as poi_type');
$this->db->from('poi');
$this->db->join('poi_poi_types', 'poi.id = poi_poi_types.poi_id');
$this->db->join('poi_types', 'poi_types.id = poi_poi_types.poi_type_id');
$this->db->where('poi_types.active', 1);
最佳答案
我相信这应该有效。好吧,将其设置为使 select 语句与众不同。我认为这基本上可以做到 SELECT DISTINCT
$this->db->distinct();
$this->db->select('poi.*, poi_types.type as poi_type');
$this->db->from('poi');
$this->db->join('poi_poi_types', 'poi.id = poi_poi_types.poi_id');
$this->db->join('poi_types', 'poi_types.id = poi_poi_types.poi_type_id');
$this->db->where('poi_types.active', 1);
关于php - 不同的 MySQL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49197245/