3

I have three tables

1.article (id,article_title)

2.article_status(id,article_id,stage_id)

3.article_stages(id,stage_name)

Each article have multiple insertion in table 2. I want to fetch the last value which indicate the current stage using join query

article

id article_title

1   article1
2   article2 

article_status

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      

I need the result like

id  article_title  stage_name
1   article1       Stage3
2   article2       Stage1

Here is my query

"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`"

I tried this

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();
}
2
  • I would suggest you to first group your article status table and then join to article and article_stages. Grouping table first reduces joining time. Commented Oct 3, 2017 at 10:36
  • Hi! You have now given us lots of details of what you're trying to do, and what you've tried, but I still don't see a description of what problem you are encountering - does your current code give an error? Or does it give a different result than you wanted? Be as specific as you can. Commented Oct 3, 2017 at 10:48

4 Answers 4

1

Query Explanation:

  1. Find article_id and current_stage_id ( see subquery groupedTable)
  2. join with article and article_stages table in order to get
    description.

SQL:

select article_id, article_title, stage_name  
from ( select article_id , max(stage_id) as current_Stage_ID
       from article_status
       group by article_id ) groupedTable 
join article on article.id = groupedTable.article_id
join article_stages on article_stages.id = groupedTable.current_Stage_ID

PHP:

function function_name() { 
       $sql = "select article_id, article_title, stage_name  
       from ( select article_id , max(stage_id) as current_Stage_ID
       from article_status
       group by article_id ) groupedTable join article on article.id = groupedTable.article_id
       join article_stages on article_stages.id = groupedTable.current_Stage_ID";
       $query = $this -> db -> query($sql);
       $result = $query -> result_array();
       return $result;
    }
Sign up to request clarification or add additional context in comments.

5 Comments

@Ann Have you tried above query. is it helpful or not ?
i need to include where condition also
Could you please explain about your condition as well ?
there are more colums in article table, like issue_no , journal_code,accode etc i need to fetch the data with condition like $condition = array("issue_no " => NULL,"journal_code"=>$jnl); I need to send condition as argument from controller
You just have to make condition and put it in proper place. You can also accept the answer if it solves your problem.
0

Use this query

SELECT    a.article_title,s.stage_name
FROM      article a
JOIN      (
              SELECT    MAX(stage_id) max_id, article_id 
              FROM      article_status 
              GROUP BY  article_id
          ) b ON (b.article_id = a.article_id)
JOIN      article_stages s ON (s.id = b.max_id)

Comments

0

try this query

$this->db->select('a.id, a.article_title, c.stage_name');
$this->db->from('article a');
$this->db->join('article_status b','b.article_id=a.id','left');
$this->db->join('article_stage c','c.id=b.article_status', 'left');
$this->db->group_by('a.article_name');
$this->db->get()->result_array();

Comments

0

You can use this query

SELECT    a.id,a.artcle_title,s.stage_name
FROM      article a
JOIN      (
              SELECT    MAX(stage_id) max_id, article_id 
              FROM      article_status 
              GROUP BY  article_id
          ) b ON (b.article_id = a.id)
JOIN      article_stages s ON (s.id = b.max_id)

With ID Column...

modified answer of - @Roshan Dandgavhal

1 Comment

@RoshanDandgavhal - there is some error in your ans, and some more result column added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.