使用CodeIgniter活动记录类时限制SQL连接 [英] Limit SQL join when using CodeIgniter active record class

查看:114
本文介绍了使用CodeIgniter活动记录类时限制SQL连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个产品列表。每个产品可能有1个或多个图片,我只想返回第一张图片。

I'm getting a product listing. Each product may have 1 or more image, I only want to return the first image.

$this->db->select('p.product_id, p.product_name i.img_name, i.img_ext');    
$this->db->join('products_images i', 'i.product_id = p.product_id', 'left');
$query = $this->db->get('products p');

是否还有使用CI活动记录类将db-> join限制为1条记录? / p>

Is there anyway to limit the db->join to 1 record using the CI active record class?

推荐答案

添加 $ this-> db-> limit(1); 之前调用 $ this-> db-> get('products p'); 。请参阅 ellislab.com 上的文档:在网页中搜索限制

Add $this->db->limit(1); before calling $this->db->get('products p');. See the docs at ellislab.com: search the page for limit.

编辑:我误解了您尝试将LIMIT应用于内部JOIN语句的事实。

I misread the fact that you were trying to apply the LIMIT to the internal JOIN statement.

否。因为你不能对一个内部的JOIN语句在常规SQL中做一个LIMIT,你不能用Code Igniter的ActiveRecord类来做。

No. Since you can not do a LIMIT on an internal JOIN statement in regular SQL you can not do it with Code Igniter's ActiveRecord class.

这篇关于使用CodeIgniter活动记录类时限制SQL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆