简体   繁体   English

如何使用 sql 或 codeigniter 3 querybuilder 连接 5 个表(4 个子表 + 1 个主表)?

[英]How to join 5 tables (4 subtable + 1 primary) with sql or codeigniter 3 querybuilder?

I have been building a hobby project where I want to write summaries about films, series, animes, tales etc. I want to join 4 tables to 1 main table, but I got a problem.我一直在建立一个爱好项目,我想在其中写关于电影、系列、动漫、故事等的摘要。我想将 4 个表加入 1 个主表,但我遇到了问题。 I'm getting null values.我得到 null 个值。 At my database the data are exist.在我的数据库中,数据存在。 But I can not figure out what's the problem with my query.但我无法弄清楚我的查询有什么问题。

The concept is as follows.概念如下。 There is an interface where I can record the data.有一个我可以记录数据的界面。 After submitting the form, the controller decides what has been recorded.提交表格后,controller 决定记录的内容。 It saves in the main table what has been saved (anime, movie, etc.) and increases the autoincrement value.它在主表中保存已保存的内容(动漫、电影等)并增加自动增量值。 It then stores the identifier of the main table and other data (description, title, image, etc.) in one of the subtables.然后它将主表的标识符和其他数据(描述、标题、图像等)存储在其中一个子表中。 This works nicely.这很好用。 Then I wanted to list the contents of these 4 sub-tables, and here I ran into a problem.然后我想列出这4个子表的内容,这里遇到了问题。

Here is my query: function getContents() {这是我的查询:function getContents() {

    $this->db->select('*');
    $this->db->from('contents'); 

    $this->db->join('films',  'films.content_id = contents.global_content_id', 'left');
    $this->db->join('series', 'series.content_id = contents.global_content_id', 'left');
    $this->db->join('tales',  'tales.content_id = contents.global_content_id', 'left');
    $this->db->join('animes', 'animes.content_id = contents.global_content_id', 'left');

    $query = $this->db->get();
    if ($query->num_rows()>0) return $query->result_array();
}

That's what I got back:这就是我得到的:

Array
(
    [0] => Array
        (
            [global_content_id] => 2
            [content_main_category] => film
            [id] => 
            [title] => 
            [description] => 
            [short_description] => 
            [category_id] => 
            [picture_path] => 
            [running_time] => 
            [director] => 
            [release_date] => 
            [language] => 
            [actors] => 
            [main_category_id] => 
            [uploader_id] => 
            [upload_time] => 
            [link] => 
            [status] => 
            [content_id] => 
            [subtitle_status] => 
            [subtitle_language] => 
        )

    [1] => Array
        (
            [global_content_id] => 3
            [content_main_category] => film
            [id] => 
            [title] => 
            [description] => 
            [short_description] => 
            [category_id] => 
            [picture_path] => 
            [running_time] => 
            [director] => 
            [release_date] => 
            [language] => 
            [actors] => 
            [main_category_id] => 
            [uploader_id] => 
            [upload_time] => 
            [link] => 
            [status] => 
            [content_id] => 
            [subtitle_status] => 
            [subtitle_language] => 
        )

Thank you guys for the help!谢谢你们的帮助!

That's the solution what I wanted:这就是我想要的解决方案:

function getContents()
{
  $this->db->select('*');
  $this->db->from('films');
  $query1 = $this->db->get_compiled_select();
        
  $this->db->select('*');
  $this->db->from('animes');
  $query2 = $this->db->get_compiled_select();

  $this->db->select('*');
  $this->db->from('series');
  $query3 = $this->db->get_compiled_select();
        
  $this->db->select('*');
  $this->db->from('tales');
  $query4 = $this->db->get_compiled_select();
        
  $query = $this->db->query('SELECT * FROM ('.$query1 . ' UNION ALL ' . $query2 . ' UNION ALL ' . $query3 . ' UNION ALL ' . $query4.') AS bigtable LEFT JOIN ohf_contents ON ohf_contents.global_content_id = bigtable.content_id');

  if ($query->num_rows()>0) return $query->result_array();  


  /*       
    Pure SQL
        
    SELECT * FROM
    (SELECT * FROM ohf_films f UNION ALL
    SELECT * FROM ohf_animes a UNION ALL
    SELECT * FROM ohf_series s UNION ALL
    SELECT * FROM ohf_tales t) AS bigtable LEFT JOIN ohf_contents ON ohf_contents.global_content_id = bigtable.content_id;
  */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM