【问题标题】:How to update data with a JOINed tables using Codeigniter's Active Record CI2?如何使用 Codeigniter Active Record CI2 使用 JOINed 表更新数据?
【发布时间】:2016-03-28 03:23:14
【问题描述】:

是否可以使用 Codeigniter 的 Active Record CI2 使用 JOINed 表更新数据?我有正在运行的代码,我想转换为 Codeigniter 的 Active Record。

public function update_table($job_id, $om_id, $recipient_id)
{
    $query = "UPDATE table1 AS t1
                       INNER JOIN table2 AS t2
                               ON t1.om_id = t2.id
                SET    t2.read_date = NOW()
                WHERE  t2.ref_table_id = $job_id
                AND    t1.om_id = $om_id
                AND    t2.recipient_id = $recipient_id
                AND    t2.read_date = '0000-00-00 00:00:00'";

    $result = $this->db->query($query);
    return $result;
}

我尝试这样但不起作用。

public function update_table($job_id, $om_id, $recipient_id)
{
    $this->db->set('t2.read_date', NOW());

    $this->db->where('t2.ref_table_id', $job_id);
    $this->db->where('t1.om_id', $om_id);
    $this->db->where('t2.recipient_id', $recipient_id);
    $this->db->where('t2.read_date', '0000-00-00 00:00:00');
    $this->db->where('t2.om_id = t1.id');
    $this->db->update('table1 AS t1, table AS t2');

}

任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: php mysql join activerecord codeigniter-2


    【解决方案1】:

    你可以试试这个吗..

    将 $condition 作为数组提供...与您要更新的数据相同。

    function update_data($table, $data, $condition)
    {
        $this->db->where($condition);
        $this->db->update($table, $data); 
    }
    

    【讨论】:

    • 我猜这只是CI上的简单方法,我应该把join子句放在哪里?
    • @VonnGarcia 嗯,据我所知,您在选择数据时只能使用join...您只是在更新t2.read_date...所以基本上您只需要做一个where clause on t1.om_id 因为om_id 已经是table2 why not do a where 子句直接在t2.om_id 上的外键......你让自己很难......
    猜你喜欢
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多