如何使用MySQL Codeigniter在两个表中使用选择查询

时间:2019-06-19 10:33:55

标签: php codeigniter

我在mysql中有两个表usersemployers,我只想同时从两个表中获取所有记录。

如何使用codeigniter做到这一点?

2 个答案:

答案 0 :(得分:0)

根据您的评论,两个表之间没有关系,因此您可以像这种交叉连接那样获取数据:

$this->db->select('users.*,employers.*'); 
$this->db->from('users,employers');
$query = $this->db->get();
return $query->result();

如果您想使用LIKE子句,则只需添加:

$this->db->like('users.entity', $search_keyword_here, "both"); // here both means '%your keyword%'
$this->db->or_like('users.skills', $search_keyword_here, "both"); // here both means '%your keyword%'

您也可以对雇主表使用or_like。

请注意,当我们使用交叉连接时,它将产生类似(users no of rows *employers no of rows)

的结果

CI Query Builder

CI Database Reference

答案 1 :(得分:0)

  

尝试此sql查询

SELECT table1.column1, table2.column2....(put all tables and columns name that you want to join)
FROM table1
FULL JOIN table2
ON table1.common_field = table2.common_field;
  

[或转到此站点]

     

https://www.tutorialspoint.com/sql/sql-full-joins.htm