Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从表中提取数据_Mysql_Codeigniter_Sum - Fatal编程技术网

Mysql 从表中提取数据

Mysql 从表中提取数据,mysql,codeigniter,sum,Mysql,Codeigniter,Sum,这里有两张桌子,分别是rent和student_hotel id date stud_id paid balance 18 10-2016 94 15000 15000 19 10-2016 94 10000 5000 20 10-2016 96 25000 5000 21 10-2016 96 5000 0 我的学生宿舍桌子是这样的

这里有两张桌子,分别是rent和student_hotel

    id   date      stud_id  paid    balance

    18  10-2016       94    15000   15000
    19  10-2016       94    10000   5000
    20  10-2016       96    25000   5000
    21  10-2016       96    5000    0
我的学生宿舍桌子是这样的

     id   first_name  last_name    stud_id     admit_date   hostel   class  room bed status

     94     ss        ff          PHBH00094     01-10-2016   12        16   115  501A    P
     96    maltu      uv          PHBH00096     01-10-2016   12        16   115  501C    p
为了得到最后一个插入的螺柱id的平衡,我使用了这样的代码

public function rent_outstanding($hos,$dt)
{
    $sql = "select s.stud_id ,s.admit_date ,s.class,first_name,sum(paid) as rt_paid,balance,rt.stud_id 
            from student_hostel s, rent rt  where s.id=rt.stud_id and hostel=? and rt.date=? 
            and rt.id = (select max(id) from rent r where r.stud_id = rt.stud_id and r.date='$dt') 
            and status!= 'G' and status!= 'R'  GROUP BY rt.stud_id";
    $query=$this->db->query($sql, array($hos, $dt));
    return $query;
}
我在这里面临的问题是,我无法对同一个stud_id的paid列下的值求和。 我得到的输出是这样的

SI.No   STUDENT ID   NAME    RENT    PAID    BALANCE
 1      PHBH00094     Ss    30000    10000    5000  
 2      PHBH00096    Maltu  30000     5000      0
SI.No   STUDENT ID   NAME    RENT    PAID    BALANCE
 1      PHBH00094     Ss    30000   25000     5000  
 2      PHBH00096    Maltu  30000   30000       0   
我需要得到的期望输出如下

SI.No   STUDENT ID   NAME    RENT    PAID    BALANCE
 1      PHBH00094     Ss    30000    10000    5000  
 2      PHBH00096    Maltu  30000     5000      0
SI.No   STUDENT ID   NAME    RENT    PAID    BALANCE
 1      PHBH00094     Ss    30000   25000     5000  
 2      PHBH00096    Maltu  30000   30000       0   

您最终解决的查询是:

select s.stud_id ,s.admit_date ,s.class,first_name,sum(paid) as rt_paid, 
(select r.balance from rent r where r.stud_id = rt.stud_id and r.date='$dt' order by r.id desc limit 1) as balance ,rt.stud_id 
from student_hostel s join rent rt  on s.id=rt.stud_id where hostel=? and rt.date=? 
and status!= 'G' and status!= 'R'  GROUP BY s.stud_id
我已经在现有查询中删除了这一行,rt.id=select maxid from rent r,其中r.stud_id=rt.stud_id,r.date='$dt'

您从租金r中选择maxid,其中r.stud_id=rt.stud_id,r.date='$dt'意味着始终只能获得最后一行rt.id,如PHBH00094为19,PHBH00096为21。
这就是为什么你每次都要做最后一次计算。

有没有人知道我在s.stud_id和rt.piad的最后一组中给出了这样的结果,当时它得到了,但只有一行得到了。你能用数据共享你的2 sql表吗?是的,我知道。我想在您的2表中使用.sql文件。有可能吗?我怎么能在这里分享呢?不知道?让我们分享吧。