У меня есть три таблицы
1.article (id,article_title)
2.article_status(id,article_id,stage_id)
3.article_stages(id,stage_name)
Каждая статья имеет несколько вставок в таблицу 2. Я хочу получить последнее значение, указывающее текущий этап, с помощью запроса соединения
статья
id статьи_title
1 article1
2 article2
статус статьи
id article_id stage_id
1 1 1
2 1 2
3 1 3
4 2 1
article_stages
id stage_name
1 Stage1
2 Stage2
3 Stage3
Мне нужен результат типа
id article_title stage_name
1 article1 Stage3
2 article2 Stage1
Вот мой вопрос
"SELECT `article`.`article_title` as id,`article`.`article_title`,`article_stages`.`stage_name`, MAX(`article_status`.`stage_id`) AS stage_id FROM (`article`)
JOIN `article_status` ON `article_status`.`article_id`=`article`.`id`
JOIN `article_stages` ON `article_stages`.`id` = (SELECT MAX(`stage_id`) FROM `article_status` )
GROUP BY `article_status`.`article_id`"
Я попробовал это
public function getAllArticleforIssue($condition = array())
{
$table1 = 'article';
$table2 = 'article_status';
$table3 ='article_stages';
$this->db->select($table1 . '.id as id,' . $table1 . '.article_title, ' . $table3 . '.stage_name');
$this->db->select_max($table2 . '.stage_id');
$this->db->from('rsgp_article');
$this->db->join($table2, $table2 . '.article_id=' . $table1 . '.id');
$this->db->join($table3, $table3 . '.id = ' . $table2 . '.stage_id');
if (count($condition) > 0)
$this->db->where($condition);
$this->db->group_by($table2 . ".article_id");
$query = $this->db->get();
return $query->result_array();
}
Подробнее здесь: https://stackoverflow.com/questions/465 ... odeigniter