gpt4 book ai didi

php - 连接具有第一个表中的重复项的表 Code Igniter Active Records

转载 作者:行者123 更新时间:2023-11-30 00:24:47 25 4
gpt4 key购买 nike

在我的 PHP 代码点火器项目上工作,我正在实现一个在线购物车,我想加入 PRODUCT 和 PRODUCT_IMAGE 两个表。产品包含除“image”之外的产品属性,并且 PRODUCT_IMAGE 包含每个“product_id”的图像我在 PRODUCT_IMAGE 中为每个“product_id”存储了多个图像,该“product_id”是表 PRODUCT_IMAGE 中的外键

我想使用 ACTIVE RECORDS 编写这样一个查询,以便它为 PRODUCT 中的每个产品仅提供一个来自 PRODUCT_IMAGE 的图像。

到目前为止我已经尝试过:

//$cat_num is category_number of the products against which we want to get Products  
$this->db->select('distinct(product_image.product_id),product_image.image,product.name');
$this->db->from('product');
$this->db->where('product.category',$cat_num);
$this->db->join('product_image', 'product.id = product_image.product_id','left');

$query = $this->db->get();

最佳答案

试试这个代码:

$this->db->select('product_image.product_id, product_image.image, product.name')
->from('product')
->join('product_image', 'product.id = product_image.product_id')
->where("product.category", $cat_num);
$query = $this->db->get();

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

关于php - 连接具有第一个表中的重复项的表 Code Igniter Active Records,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22973492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com