MySQL销售日报

时间:2016-06-26 19:46:03

标签: php mysql codeigniter

大家好我正在创建一份日报。我是MySQL的初学者。
我想生成这种数据。
我的phpmyadmin中有一个带有这种结构的表

<table>
    <tr> 
        <th>ID</th>
        <th>Item_desc<th>
        <th>Total_price<th>
        <th>Quantity</th>
        <th>Date</th>
        <th>Branch</th>
    </tr>

    <tr> 
        <td>1</td>
        <td>Bottle</td>
        <td>100 </td>
        <td>10 </td>
        <td>June 26,2016 </td>
        <td>Rizal</td>
    </tr>

    <tr> 
        <td>2</td>
        <td>Bottle</td>
        <td>120 </td>
        <td>12 </td>
        <td>June 26,2016 </td>
        <td>Rizal</td>
    </tr>

    <tr> 
        <td>3</td>
        <td>Bottle</td>
        <td>100 </td>
        <td>10 </td>
        <td>June 26,2016 </td>
        <td>Rizal</td>
    </tr>
</table>

但我只获得了1项纪录。我想用total_price和quantity的总和得到所有记录。这是我目前的查询。谢谢。

$query= $this->db->select('*,SUM(total_price),SUM(quantity)')->from('sales')->where('branch',$_SESSION['branch'])->where('date',$from)->get();

1 个答案:

答案 0 :(得分:0)

试试这个

$this->load->library('session');
$branch = $this->session->userdata('branch');

$query = $this->db->query("SELECT *,SUM(total_price) AS price,SUM(quantity) As quantity
                            FROM sales 
                            WHERE branch = '$branch' AND WHERE date = '$from' ");
$result = $query->result_array();
return $result;