多列选择查询

时间:2016-03-19 18:02:53

标签: codeigniter

我的表格字段是

payid   payamount   paytype
01      5000        1
02      3000        1
03      2500        3

我希望结果为select cash =(select sum (payamount)where paytype=1) online=(select sum (payamount)where paytype=2) check=(select sum (payamount)where paytype=3)

我怎么能在codeigniter中做到这一点?

3 个答案:

答案 0 :(得分:0)

可能您正在寻找以下

$this->db->select("SUM(payment)")
        ->from("TABLE_NAME")
        ->where("paytype", $paytype)

放置TABLE_NAME并传递$ paytype并希望它能正常工作......

答案 1 :(得分:0)

听起来像GROUP BY。

$this->db->select_sum('paymount');
$this->db->select('paytype');
$this->db->group_by('paytype');
$query = $this->db->get('mytable');

在Codeigniter中,您可以使用

array1 = ["helloworld.html", "helloworld.html", "helloworld.html"]

loadData(array1).then(function() {
  $('pre').append("done");
});


function loadData(gim) {
    for (i = 0; i < gim.length; i++) {
      console.log(i);
      $.get(gim[i], function(data) {
        console.log("a = " + i);
        $('pre').append(data+" -- "+i+"<br>");
      });
    }
  }

答案 2 :(得分:0)

public function getincomebyyear($s,$e){
        $this->db->select('income_id, income_date,income_source,income_sourceid,income_refferanceid,income_description,income_amount,(select sum(income_amount) from tbl_incomes where income_description=1) as cash,'
                . '(select sum(income_amount) from tbl_incomes where income_description=2) as online,'
                . '(select sum(income_amount) from tbl_incomes where income_description=3) as che');
    $this->db->from('tbl_incomes');
        $this->db->where('income_date between'
                . ' "'. date('Y-m-d', strtotime($s)). '" and'
                . ' "'. date('Y-m-d', strtotime($e)).'"');
        $query_result= $this->db->get();
        $result=$query_result->result();
        return $result;
    }