【问题标题】:How to Join two tables in codeigniter in a single column如何在单列中加入codeigniter中的两个表
【发布时间】:2019-04-19 06:20:14
【问题描述】:

我想在一个列中加入 CodeIgniter 中的两个表。例如,有一个如图所示的表A

表 B

连接表 A 和表 B 后的期望结果

【问题讨论】:

  • 这两个表之间有什么关系吗?
  • 我不想比较这些表格
  • 我的方式是分别获取记录然后合并结果

标签: mysql sql codeigniter


【解决方案1】:

使用union

select name from tableA
union 
select name from tableB

【讨论】:

    【解决方案2】:

    试试这个,

    $tableA = $this->db->select('name')->get_compiled_select('tableA');
    echo $tableA;
    $tableB = $this->db->select('name')->get_compiled_select('tableB');
    echo $tableB;
    $tableAB = $this->get_compiled_select($tableA.' UNION '.$tableB);
    echo $tableAB;
    $query = $this->db->query($tableAB);
    

    输出:

    SELECT name FROM tableA
    SELECT name FROM tableB
    SELECT name FROM tableA UNION SELECT name FROM tableB
    

    或者:

    $query = $this->db->query("SELECT name FROM tableA UNION SELECT name FROM tableB");
    

    【讨论】:

      【解决方案3】:

      试试这个

      $result1 = $this->db->get('TableA');
      
      $result2 = $this->db->get('TableB');
      

      如果您只想合并特定列,请使用select()

      $this->db->select('name');
      $result1 = $this->db->get('TableA');
      
      $this->db->select('name');
      $result2 = $this->db->get('TableB');
      

      现在合并这两条记录

      $combine = array_merge($result1, $result2);
      

      阅读更多关于array_merge()

      【讨论】:

        【解决方案4】:

        select Name from TableA Union select Name from TableB

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-16
          • 2013-11-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-10
          • 2020-03-05
          相关资源
          最近更新 更多