簡體   English   中英

在CODEIGNITER中加入三個表格

[英]JOIN THREE TABLES IN CODEIGNITER

我有三張桌子

1.article(id,article_title)

2.article_status(ID,article_id的,stage_id)

3.article_stages(ID,stage_name)

每篇文章都在表2中有多個插入。我想使用連接查詢獲取指示當前階段的最后一個值

文章

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      

我需要結果如

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();
   }

查詢說明:

  1. 查找article_id和current_stage_id(請參閱子查詢分組表)
  2. 加入article和article_stages表以獲取
    描述。

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;
    }

使用此查詢

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)

您可以使用此查詢

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)

使用ID列...

修改后的答案 - @Roshan Dandgavhal

試試這個查詢

$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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM