php - 如何使用codeigniter获取表中的单个记录

标签 php mysql codeigniter

嗨, friend 们,我有两张 table

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();

这里我得到了所有重复的用户数据,我只想要一次进入购物车的用户数据

请帮帮我

最佳答案

尝试分组,例如

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

关于php - 如何使用codeigniter获取表中的单个记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20068754/

相关文章:

php - 函数在不同环境下表现不同

php - 如何在php中执行多个计数查询

php - Codeigniter无法从mysql表获取数据

javascript - jsPDF 将 PDF 附加到邮件

javascript - 如何在 CodeIgniter 中使用输出 JSON 将添加的数据保存到数据库中

php - 一对多然后使用 Laravel Eloquent ORM 急切加载数组

php - 使用 Artisan::call() 运行包迁移

mysql - 无法在 centos7 上的 mariadb 10 中增加打开文件限制

php - 如何在 codeigniter 的产品页面中添加动态产品标题

mysql - MySQL中的嵌套 View 有什么问题吗