【问题标题】:Codeigniter using SELECT_SUM, only returning the first columnCodeigniter 使用 SELECT_SUM,只返回第一列
【发布时间】:2020-03-24 17:35:07
【问题描述】:

我试图通过按订单添加菜肴数量来获取登录用户的每个订单中有多少菜的价值。

假设登录用户的用户 ID 为 35,我运行的正常查询是成功的:

SELECT      o.orderid, location, status, pay, total, 
            SUM(quantity) AS dishnum 
FROM        orders o 
JOIN        ordersdish od 
            ON od.orderid = o.orderid 
WHERE       userid = 35 
GROUP BY    o.orderid;

结果:

我尝试运行的 Codeigniter 代码:

public function getorderself(){
    $this->db->select_sum('quantity');
    $this->db->select('orders.orderid','location','status','pay','total');
    $this->db->from ('orders');
    $this->db->join('ordersdish', 'ordersdish.orderid = orders.orderid');
    $this->db->where('userid', $_SESSION["userid"]);
    $this->db->GROUP_BY('orders.orderid');
    $query = $this->db->get ();
    return $query;
}

谢谢。 更新:我改变了一点我的代码,它可以返回数据但只有 orderid。

我在视图中使用的代码:

<?php
    if($orderdata->num_rows() > 0)
    {
        foreach($orderdata->result() as $row)
        {
?>
            <tr>
                <td><?php echo $row->orderid; ?></td>                        
                <td><?php echo $row->location; ?></td>
                <td><?php echo $row->status; ?></td>
                <td><?php echo $row->pay; ?></td>
                <td><?php echo $row->total; ?></td>
                <td><?php echo $row->quantity; ?><td>
                <td><button type="button" href="#" class="delete_data delButton" orderid="<?php echo $row->orderid; ?>">Delete</button></td>
            </tr>
<?php  

        }
    }
    else
    {
?>
        <tr>
            <td colspan="7" align="center">No Data Founds</td>
        </tr>
<?php   
    }
?>

我在maim.php中使用的代码

public function vieworderself()
{

    $this->load->view('header');
    $this->model->caltotal();
    $data["orderdata"]=$this->model->getorderself();  
    $this->load->view('/orders/vieworderself', $data);
    $this->load->view('footer');
}

【问题讨论】:

  • 错字 this-&gt;db-GROUP_BY('order.orderid');db附近缺少&gt;
  • 对不起,粗心的错误,但代码仍然无法正常工作,我想我的代码在“this->db->select_sum('quantity');”行有问题
  • @abcsgss123 表中有quantity 列吗?哪一栏显示点菜的数量?
  • 是的,我在“ordersdish”表中有“数量”列

标签: php codeigniter activerecord


【解决方案1】:

尝试在每列之前使用表名。例如。订单位置

public function getorderself(){
    $this->db->select_sum('quantity');
    $this->db->select('orders.orderid','orders.location','orders.status','orders.pay','orders.total');
    $this->db->from ('orders');
    $this->db->join('ordersdish', 'ordersdish.orderid = orders.orderid');
    $this->db->where('orders.userid', $_SESSION["userid"]);
    $this->db->GROUP_BY('orders.orderid');
    $query = $this->db->get ();
    return $query;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2014-12-14
    • 2011-05-15
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多