我有一张这样的table:
我的表名是: user_viewed_offer
```
id user_id coupon_id
1 1 65
2 1 58
3 1 65
4 1 65
5 1 34
6 1 46
7 1 24
我要检索这些ID:
4-5-6-7
我曾经 `group by` 但它会返回这些ID:
"[{"id":"7"},{"id":"6"},{"id":"5"},{"id":"2"}]"
我的职能:
$this->db->select('id');
$this->db->from('user_viewed_offer');
$this->db->where('user_id',$user_id);
$this->db->group_by('coupon_id');
$this->db->order_by('id','desc');
$this->db->limit('4');
$data['coupons'] = $this->db->get()->result();
var_dump(json_encode($data['coupons']));
exit();
我想我用 `distinct` 但我不知道该怎么用。
2条答案
按热度按时间tpxzln5u1#
将您的表联接到查找最近
id
每个用户、每个优惠券的值。lskq00tm2#
似乎要从优惠券id中的最大值开始检索id