简体   繁体   中英

CI: (Count Rows) with same ID

foodorders

foodorders_id    foodorders_price    foodorders_status
1                   100                     1
2                   20                      1
3                   30                      1


foodorders_detail

foodorders_detail_id(AI,PK)      foodorders_id    foodorders_foodname    foodorders_lines
1                                       1               burgers                 1
2                                       1               fries                   2
3                                       1               chips                   3
4                                       2               chips                   1
5                                       3               drinks                  1
5                                       3               bars                    2

What i want: check how many rows with same foodorders_id and returns to controller

What i tried:

$this->db->select('foodorders.foodorders_price,foodorders.status,COUNT(foodorders_detail.foodorders_lines');
$this->db->from('foodorders');
$this->db->join('foodorders_detail', 'foodorders_detail.foodorders_id = sales.foodorders_id', 'left');

add foodorders.foodorders_id to your select columns and add group by to your query and then get result as object or array:
hint: use alias to shorten your code.

    $this->db->select('foodorders.foodorders_id,foodorders.foodorders_price,foodorders.status,COUNT(foodorders_detail.foodorders_lines)');
    $this->db->from('foodorders');
    $this->db->join('foodorders_detail', 'foodorders_detail.foodorders_id = foodorders.foodorders_id', 'left');
    $this->db->group_by('foodorders.foodorders_id');
    $query = $this->db->get();
    return $query->result();

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