【问题标题】:Using OUTER APPLY with Codeigniter active record将 OUTER APPLY 与 Codeigniter 活动记录一起使用
【发布时间】:2023-04-03 14:24:01
【问题描述】:

是否可以将 SQL Server 的 OUTER APPLY 与 Codeignter 的 Active Record 一起使用?

【问题讨论】:

  • 不确定 APPLY 但你可以做 OUTER join.. 比如:$this->db->join('table', 'table.id = table2.id', 'outer');
  • 是的,我已经多次使用 join,但在这种情况下,我需要使用 APPLY。可能需要发挥创意...

标签: sql-server codeigniter activerecord outer-apply


【解决方案1】:

不可能。要使用 Cross Apply 或 Outer Apply,您需要在文件 system\database\DB_query_builder.php 中添加特定函数

话虽如此,您可以将此功能(从join one修改)添加到所述文件中:

public function apply($table, $type = '', $escape = NULL){
    if ($type !== '')
    {
        $type = strtoupper(trim($type));

        if ( ! in_array($type, array('CROSS', 'OUTER'), TRUE))
        {
            $type = 'CROSS';
        }
    }
    else{
        $type = 'CROSS';
    }

    // Extract any aliases that might exist. We use this information
    // in the protect_identifiers to know whether to add a table prefix
    $this->_track_aliases($table);

    is_bool($escape) OR $escape = $this->_protect_identifiers;

    // Do we want to escape the table name?
    if ($escape === TRUE)
    {
        $table = $this->protect_identifiers($table, TRUE, NULL, FALSE);
    }

    // Assemble the APPLY statement
    $this->qb_join[] = $join = $type.' APPLY '.$table;

    if ($this->qb_caching === TRUE)
    {
        $this->qb_cache_join[] = $join;
        $this->qb_cache_exists[] = 'join';
    }

    return $this;
}

然后,你可以像这样使用它

$this->db->apply('(select * from table_a where table_a.id=table_z.fk_id) as tmp_table');

或者更完整的东西,比如

$this->db->apply('(select * from table_a where table_a.id=table_z.fk_id) as tmp_table','outer',false);

【讨论】:

    【解决方案2】:

    你可以这样做

    $this->db->join('comments', 'comments.id = blogs.id', 'outer');
    

    或者

    $this->db->query("SELECT column_name(s)
        FROM table1
        FULL OUTER JOIN table2
        ON table1.column_name=table2.column_name;");
    

    Codeigniter Query BuilderOUTER APPLY 示例


    可能加入 Codeigniter
    (与此 $this->db->join('comments', 'comments.id = blogs.id', 'outer'); 关联)

    1. 外层
    2. 内部
    3. 左外
    4. 右外

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      相关资源
      最近更新 更多