0

你好 stackoverflow 人 :) 我想从 2 个表中获取数据。我不想要任何特定的“位置”选项,所以这是我的模型代码:

$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');
4

1 回答 1

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');
于 2013-07-30T22:43:25.383 回答