[英]How to get the single record in table using codeigniter
Hi Friends I have two tables 嗨朋友我有两张桌子
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
i want to get the cart data having user details with out duplicate(only just we get who having at least one cart item user) 我想获得具有重复用户详细信息的购物车数据(只是我们得到至少有一个购物车用户的用户)
Note: 注意:
$this->db->select('*');
$this->db->from('user');
$this->db->distinct();
$this->db->join('cart', 'cart.id = users.id');
$query = $this->db->get()->result();
here i am getting all duplicate user data i want only once user data who are having in to the cart 在这里我得到所有重复的用户数据我只想要一次进入购物车的用户数据
please help me 请帮我
Try group by
like 尝试group by
$this->db->group_by('cart.id');
So it would be 所以它会
$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();
refer the solution below 参考下面的解决方案
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.