![](/img/trans.png)
[英]How to get values from another table in codeigniter using the Active Record?
[英]How to get the single record in table using codeigniter
嗨朋友我有两张桌子
1)user
id name etc..
1 xyz
2 pqrs
3 lmn
2)cart
cart_id id etc....
1 1
2 2
3 1
4 1
我想获得具有重复用户详细信息的购物车数据(只是我们得到至少有一个购物车用户的用户)
注意:
$this->db->select('*');
$this->db->from('user');
$this->db->distinct();
$this->db->join('cart', 'cart.id = users.id');
$query = $this->db->get()->result();
在这里我得到所有重复的用户数据我只想要一次进入购物车的用户数据
请帮我
尝试group by
$this->db->group_by('cart.id');
所以它会
$this->db->select('*');
$this->db->from('user');
$this->db->distinct();
$this->db->join('cart', 'cart.id = users.id');
$this->db->group_by('cart.id');
$query = $this->db->get()->result();
参考下面的解决方案
function users_having_sinle_item(){
$this->db->select('u.id, count(c.id) cart_count, u.username, c.cart_id, c.created_at');
$this->db->from('users u');
$this->db->join('cart c', 'c.id = u.id');
$this->db->group_by('u.id');
$this->db->having('cart_count = 1');
$query = $this->db->get()->result();
print_r($query);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.