Codeigniter - > from(多个表)

时间:2013-07-30 22:30:11

标签: php codeigniter

Hello stackoverflow人:) 我想从2个表中获取数据。我不想要任何特定的'where'选项,所以这是我的模型代码:

$this->db->from('table1, table2');      
$this->db->order_by('table1.date, table2.date', "desc");
$query = $this->db->get('', $num, $offset);

return $query->result_array();

这样我只从'table2'获取信息,其中只包含1篇文章,而本文在页面上多次回显。这两个表具有相同的结构,我希望用这种方式写'order_by'会有所帮助,但是没有。如果我做任何其他操作,我只能从一个表中获取信息。按日期排序很重要。 希望任何人都会发现这个问题很有意思,也会有所帮助。提前谢谢!

编辑部分: 功能:

$this->db->get("table1");

$comm_numbers = $this->db->count_all_results("table1");

$config['base_url'] = base_url();
$config['total_rows'] = $comm_numbers;
$config['per_page'] = '5';
$config['uri_segment'] = 1;
$this->pagination->initialize($config);

$this->load->model("model_get_all");
$data["results"] = $this->model_get_all->getData1($config['per_page'], $this->uri->segment(1));
$this->load->helper('url');

1 个答案:

答案 0 :(得分:1)

试试这个:

$this->db->query('SELECT id, title, text, views, date, image FROM table1 UNION SELECT id, title, text, views, date, image FROM tablee2');

<强>更新 使用CodeIgniter分页查询:

$this->db->query("SELECT id, title, text, views, date, image FROM table1 UNION SELECT id, title, text, views, date, image FROM tablee2 LIMIT $offset, $num");

更新2:好的,我只在$offset转到null时更改地点。

$this->db->get("table1");

$comm_numbers = $this->db->count_all_results("table1");

$config['base_url'] = base_url();
$config['total_rows'] = $comm_numbers;
$config['per_page'] = '5';
$config['uri_segment'] = 1;
$this->pagination->initialize($config);

$page = ($this->uri->segment(1)) ? $this->uri->segment(1) : 0;

$this->load->model("model_get_all");
$data["results"] = $this->model_get_all->getData1($config['per_page'], $page);
$this->load->helper('url');