获取每个唯一付款 ID 的相同产品 ID 的最后一行,并使用 codeigniter 在不同的 table 上更新状态

Get the last row of the same product id for every unique payment id and update the status on a different table with codeigniter

我有两个table,第一个table叫payments见下文

我想获得每个 product_id 的最新 payment_id(意思是最新付款),请参阅突出显示的行,在这种情况下,我想获得 10 的 product_id, 9、8、3 和 6,但来自最新的 payment_id,即 69、68、59、58 和 55。

获得所有这些后,我想在另一个名为 list_data 的 table 上更新状态,见右:

此 table 也有 product_id 来自 payments table 的相同产品 ID。

我的总体目标是在付款后更新每个 product_id 的状态。
下面是我的控制器:

$data["exp_date"] = $this->Superadmin_model_CMS->get_all_detail_needed_again();

        $this->load->helper('date');

        foreach ($data["exp_date"] as $value) {

            $exp_date = $value->payment_endDate;
            $todays_date = date("Y-m-d");
            $today = strtotime($todays_date);
            $expiration_date = strtotime($exp_date);

            if ($expiration_date <= $today && $value->ad_status === 'active') {
                // if ($value->ad_status === 'active') {
                $update_status = 'inactive';
                $data = array('ad_status' => $update_status
                );
                $this->Listing_model->update_ads_status($value->list_data_id, $data);
                // }
            }
        }

下面是我的模型

public function get_all_detail_needed_again() {
    $this->db->select('py.payment_id,py.user_id,py.product_id,py.product_name,py.txn_id,py.payment_gross,py.payer_email,py.payment_status,py.payment_date,py.payment_endDate,ld.ad_status,ld.list_data_id');
    $this->db->from('payments py');
    $this->db->join('list_data ld','py.product_id = ld.list_data_id');

    $this->db->order_by('py.payment_id','desc');
    $this->db->limit(1); //If I don't put limit the code doesn't get the latest payment_id but if write limit 1, it only get 1 row of product_id and payment_id
    $query = $this->db->get();

    if($query->num_rows() > 0){
       return $query->result();
    } else{
       return array();
    }

}

public function update_ads_status($id, $data) {

    $this->db->where('list_data_id', $id);
    $this->db->update('list_data', $data);

    return true;

}

在我的模型中,如果我不写 limit 1,代码不会得到最新的 payment_id 但如果写 limit 1 它只会得到 1 行 product_idpayment_id

谁能帮帮我?谢谢

使用以下代码更新您的函数:

public function get_all_detail_needed_again() {
    $this->db->select('py.payment_id,py.user_id,py.product_id,py.product_name,py.txn_id,py.payment_gross,py.payer_email,py.payment_status,py.payment_date,py.payment_endDate,ld.ad_status,ld.list_data_id');
    $this->db->from('payments py');
    $this->db->join('list_data ld','py.product_id = ld.list_data_id');
    $this->db->group_by('py.product_id'); //added this line
    $this->db->order_by('py.payment_id','desc');
    $query = $this->db->get();

    if($query->num_rows() > 0){
       return $query->result();
    } else{
       return array();
    }

}

您可以先尝试使用子查询 select 每个 product_id 的最后一个 payment_id,尝试像这样修改模型:

public function get_all_detail_needed_again() {
    $this->db->select('py.payment_id,py.user_id,py.product_id,py.product_name,py.txn_id,py.payment_gross,py.payer_email,py.payment_status,py.payment_date,py.payment_endDate,ld.ad_status,ld.list_data_id');
    $this->db->from('payments py');
    $this->db->where('`payment_id` IN (SELECT MAX(`payment_id`) FROM `payments` GROUP BY `product_id`)');
    $this->db->join('list_data ld', 'py.product_id = ld.list_data_id');
    $query = $this->db->get();

    if($query->num_rows() > 0){
       return $query->result();
    } else{
       return array();
    }

}