簡體   English   中英

如何使用codeigniter獲取表中的單個記錄

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM