简体   繁体   中英

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

$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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM