[英]codeigniter fetch multiple value from sub table in same query
我在嘗試從同一查詢的子表中獲取多個值時遇到了一些問題。 我有dealdetails
和dealImages
表,其中dealImages
可以包含來自dealdetails
表的多個值wrt deal-id
。 像這樣寫查詢
$dealDetailsArray = array();
$this->db->select('d.dealId,d.dealTitle,d.slug,d.dealDetails,d.extraDetails,d.aditionalDetails,d.status,d.dateAdded,d.categoryId,d.dealSubCategory,d.siteId,d.dealBrandId,d.isPinned,d.priceId,d.price,d.startDate,d.startTime,d.endDate,d.endTime,d.addedTime,d.dealUrl,d.adminAffiliatePrice,d.cashbackType,d.cashbackAmountType,d.cashbackAmount,d.shippingType,d.NumberOfClicked,d.priceType,d.discountPrice,d.discountPercentage,d.deal_location,d.howtousethisoffer,d.cancellationpolicy,d.deal_submittedby,d.dealType,d.totalavailabledeals,d.numberofdealused,d.showinhomescreen,d.showinmenu,d.isHomeScreenBigDeal,di.imageId,di.imageUrl,di.thumbImage,di.imageOrder,di.status,di.dealId,di.addedOn,di.imageobjId,di.normalimageurl,di.imgobjext,di.imgobject,di.imgisdefault');
$this->db->from('dealdetails as d');
$this->db->join('dealImages di', 'di.dealId = d.dealId','left');
$this->db->where('d.endTime >= ',date('Y-m-d H:i:s'));
$this->db->group_by('d.dealId');
$this->db->order_by("d.dealId", "desc");
$query = $this->db->get();
if($query->num_rows()>0) {
$dealDetailsArray = $query->result_array();
$query->free_result();
}
return $dealDetailsArray;
這總是從dealImages
返回單個值,例如
[deal_submittedby] => 0
[dealType] => 1
[totalavailabledeals] => 0
[numberofdealused] => 0
[showinhomescreen] => 1
[showinmenu] => 0
[isHomeScreenBigDeal] => 1
[imageId] => 22
[imageUrl] => http://localhost/codeIgniter/uploads/cover_image/storeimages/general/general_1493562480.momo.jpg
[thumbImage] => http://localhost/codeIgniter/uploads/cover_image/storeimages/thumb/thumb_1493562480.momo.jpg
[imageOrder] => 1
[addedOn] => 2017-04-30 14:28:43
[imageobjId] => 1493562480
[normalimageurl] => http://localhost/codeIgniter/uploads/cover_image/storeimages/normal/normal_1493562480.momo.jpg
[imgobjext] => momo.jpg
[imgobject] => 1493562480.momo.jpg
[imgisdefault] => 1
然后在選擇查詢上添加GROUP_CONCAT,它以逗號(,)分隔返回值
像這樣的代碼:
$dealDetailsArray = array();
$this->db->select('d.dealId,d.dealTitle,d.slug,d.dealDetails,d.extraDetails,d.aditionalDetails,d.status,d.dateAdded,d.categoryId,d.dealSubCategory,d.siteId,d.dealBrandId,d.isPinned,d.priceId,d.price,d.startDate,d.startTime,d.endDate,d.endTime,d.addedTime,d.dealUrl,d.adminAffiliatePrice,d.cashbackType,d.cashbackAmountType,d.cashbackAmount,d.shippingType,d.NumberOfClicked,d.priceType,d.discountPrice,d.discountPercentage,d.deal_location,d.howtousethisoffer,d.cancellationpolicy,d.deal_submittedby,d.dealType,d.totalavailabledeals,d.numberofdealused,d.showinhomescreen,d.showinmenu,d.isHomeScreenBigDeal,di.imageId,di.GROUP_CONCAT(di.imageUrl) AS imageUrl,di.thumbImage,di.imageOrder,di.status,di.dealId,di.addedOn,di.imageobjId,di.normalimageurl,di.imgobjext,di.imgobject,di.imgisdefault');
$this->db->from('dealdetails as d');
$this->db->join('dealImages di', 'di.dealId = d.dealId','left');
$this->db->where('d.endTime >= ',date('Y-m-d H:i:s'));
$this->db->group_by('d.dealId');
$this->db->order_by("d.dealId", "desc");
$query = $this->db->get();
if($query->num_rows()>0) {
$dealDetailsArray = $query->result_array();
$query->free_result();
}
return $dealDetailsArray;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.