【问题标题】:multiple group_by with join and order by多个 group_by 与 join 和 order by
【发布时间】:2017-02-02 06:17:19
【问题描述】:

我有三张桌子

买家

b_id 和 b_name

产品

p_id、p_status、p_cat

出价

bid_id、bid_amount、fkb_id、fkp_id(买方和产品表的外键)

买家可以对相同的产品进行多次出价
我想获取在 desc 中具有唯一 bid_amount 的每个产品的所有记录

我试试这个

     $this->db->distinct();

     $this->db->from('bid'); 

     $this->db->join('product','product.p_id=bid.fkp_id');

     $this->db->join('buyer','buyer.b_id=bid.fkb_id');

     $this->db->where('product.p_status','close');

     $this->db->where('product.p_cat','diamond');

     $res = $this->db->get();

     $res_data = $res->result();

     return $res_data;

这将返回所有产品,但不是唯一的 fkb_id(在这种情况下为买方,即投标人 ID)

【问题讨论】:

    标签: mysql codeigniter


    【解决方案1】:

    在查询末尾使用 Group by...

     $this->db->group_by('column_name');
    

    【讨论】:

      【解决方案2】:

      试试这样 $this->db->max('bid_amount'); $this->db->group_by('fkb_id');

      【讨论】:

        猜你喜欢
        • 2014-08-31
        • 2012-12-15
        • 1970-01-01
        • 2011-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多