Php 需要获得将多个表合并为一行的结果
我想得到将多个表连接为一行的结果,并从t_Council表中获取多个Council_名称,然后使用php(CODEIGNITER)和连接在t_search.cuisineId列中获取Council_id 使用搜索表获取这样的菜系ID,以便我可以通过菜系ID获取可用的菜系名称Php 需要获得将多个表合并为一行的结果,php,mysql,codeigniter,Php,Mysql,Codeigniter,我想得到将多个表连接为一行的结果,并从t_Council表中获取多个Council_名称,然后使用php(CODEIGNITER)和连接在t_search.cuisineId列中获取Council_id 使用搜索表获取这样的菜系ID,以便我可以通过菜系ID获取可用的菜系名称 t_search table searchID|restaurant_name|cuisineId 1 | XYZ | 1,4,5 2 | KIH
t_search table
searchID|restaurant_name|cuisineId
1 | XYZ | 1,4,5
2 | KIH | 2
3 | GHY | 4,5
4 | UIO | 1,2,3
5 | RTY | 3,5
t_cuisine table
cuisineId|cuisine_name
1 | ABC
2 | CDE
3 | EFG
4 | GHZ
5 | HJL
在我的模型中,我使用了
$this->db->select('*');
$this->db->from('t_search');
$this->db->join('t_cuisine','t_cuisine.cuisineId = t_search.cuisineId');
它仅基于t_搜索中cuisineId中的单个值获取数据
$this->db->select('*');
$this->db->from('t_search');
$this->db->join('t_cuisine','t_cuisine.cuisineId IN(t_search.cuisineId)');
$this->db->where('t_cuisine.cuisineId', X);
将X更改为您正在寻找的菜肴的ID
您以前(当您的查询基于cuisineId
中的单个值时)所拥有的是一对多关系。这样的加入效果很好,因为每个搜索都有一种美食
这是一个多对多关系,并且是这个表结构。您需要另一个表来表示搜索和烹饪之间的关系,而不是在t\u search
表的cuisineId
列中存储分隔列表,如下所示:
t_search_cuisine table
searchID|cuisineId
1 | 1
1 | 4
1 | 5
2 | 2
3 | 4
3 | 5
4 | 1
4 | 2
4 | 3
5 | 3
5 | 5
然后,您应该能够通过一个附加连接获取所有数据
$this->db->select('*');
$this->db->from('t_search');
$this->db->join('t_search_cuisine','t_search.searchID = t_search_cuisine.searchID');
$this->db->join('t_cuisine','t_search_cuisine.cuisineId = t_cuisine.cuisineId');
根据表的结构,连接这些表并不容易。也许你可以用两个查询来代替
$this->db->select("cuisineId");
$this->where("searchID", $searchID);
$qry1 = $this->db->get("t_search");
$res1 = $qry1->row_array();
$qry1->free_result();
if ($res1) {
$this->db->select("*");
$this->db->where_in('cuisineId', $res1);
$qry2 = $this->db->get("t_cuisine");
$res2 = $qry2->result();
return ($res2) ? $res2 : false;
}
这是不在一列中存储多个值的一个很好的理由。