gpt4 book ai didi

mysql - 如何使用一列中的 SUM 从数据库中检索数据值

转载 作者:行者123 更新时间:2023-11-29 16:56:48 26 4
gpt4 key购买 nike

我有一个从数据库中检索 json 值的函数。我需要从数据库中获取一些数据以及某些列的 SUM 值。当我没有从这些值中检索 SUM 时,我会获取所有必需的数据,但当我还包含 SUM 函数时为了从特定列获取总值,我只得到单个值对象

型号

    public function search_tickets($data){
$ticketDate = $data['ticket_date'];
$ticketBus = $data['ticket_bus'];
$ticketAgentId = $data['agent_id'];
$user = $this->db->select("
CONCAT_WS(' ', tp.firstname, tp.lastname) AS passenger_name,
tb.seat_numbers AS seat_number,
tb.id_no AS ticket_number,
fr.reg_no AS bus_number,
DATE_FORMAT(ta.start_date, '%d/%m/%Y') as ticket_date,
tb.price AS fare_paid,
tb.pickup_trip_location AS boarding,
tb.drop_trip_location AS dropping,
SUM(tb.price) AS total_fare
")
->from('tkt_booking AS tb')
->join('tkt_passenger AS tp', 'tb.tkt_passenger_id_no = tp.id_no' ,'full')
->join('trip_assign AS ta', 'ta.id = tb.trip_id_no' ,'full')
->join('trip_route AS tr', 'tr.id = tb.trip_route_id','full')
->join('agent AS a', 'a.agent_id = tb.agent_id','full')
->join('fleet_registration AS fr', 'fr.id = ta.fleet_registration_id','full')
->where('ta.start_date', $ticketDate )
->where('fr.reg_no', $ticketBus )
->where('tb.agent_id', $ticketAgentId )
->get()
->result();
return $user;
}

Controller

    public function searchTickets(){ 
$data = $_POST;
$ticket=$this->booking_model->search_tickets($data);
if($ticket){
$result = array('tickets'=>$ticket);
} else {
$result = NULL;
}
print json_encode($result);
}

最佳答案

您应该GROUP BY乘客 ID 来获取每位乘客的总金额:

 $this->db->group_by('tb.tkt_passenger_id_no');

关于mysql - 如何使用一列中的 SUM 从数据库中检索数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52515499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com