如何在单个函数中执行两个表列数据

时间:2013-09-14 15:22:49

标签: php mysql sql codeigniter

我必须通过使用Users_height& amp;来计算用户的BMI。 users_weight来自不同的两个表。

以下是两个表格(http://i.stack.imgur.com/eOQs4.gif)& (http://i.stack.imgur.com/liqhH.gif

渴望输出

  weight_pounds    height_cm 
     121.25           130
     132.28           160
     154.32           221
     176.37           434

user_weight表 enter image description here

user_height表 enter image description here

我试过这个功能,但显示错误:

 function get_all_user_bmi($uid) 
{ 
    $this->load->database(); 
    $this->db->select('w.weight_pounds','h.height_cm');
    $res = $this->db->order_by('w.uweight_id', 'ASC')->order_by('h.height_id', 'ASC')
            ->get_where('user_weight w',array('w.creater_id'=>$uid))
            ->get_where('user_height h',array('w.creater_id'=>$uid))
            ;
    $ret = array();

    foreach ($res->result_array() as $row) {
        $weight=$row['w.weight_pounds']* 4.88;
        $height=($row['h.height_cm']*0.032808)*($row['h.height_cm']*0.032808);  
        $bmi=$weight/$height;
        $ret[] = $bmi;  //final bmi formuala calculated
    }

    return $ret;
}  

错误是:

 A Database Error Occurred

Error Number: 1054

Unknown column 'h.height_id' in 'order clause'

SELECT `w`.`weight_pounds` FROM (`user_weight` w) WHERE `w`.`creater_id` = '3235' ORDER BY `w`.`uweight_id` ASC, `h`.`height_id` ASC

Filename: D:\xampp\htdocs\webapp\system\database\DB_driver.php

Line Number: 330

希望有人可以帮助我......

更新

在列中添加了额外的列same_id以满足要求并添加了$this->db->join('user_height h' ,'w.same_id = h.same_id');这段代码,现在工作正常

2 个答案:

答案 0 :(得分:1)

尝试使用join您尚未加入表格,如果您需要用户详细信息,如身高,体重等,那么在您的表格中,您应该保存用户信息以及两个表格中的用户ID是一对一关系,然后根据该用户ID加入您的表来计算用户的bmi

$this->db->select('w.weight_pounds, h.height_cm');
$res = $this->db->join('user_height h' ,'w.user_id= h.user_id')
         ->order_by('w.uweight_id', 'ASC')->order_by('h.height_id', 'ASC')
        ->get_where('user_weight w',array('w.creater_id'=>$uid));

OR

$this->db->select('w.weight_pounds, h.height_cm');
$this->db->from('user_weight w');
$this->db->join('user_height h' ,'w.user_id= h.user_id')
$this->db->where('w.creater_id',$uid); 
$this->db->where('h.creater_id',$uid); 
$this->db->order_by('w.uweight_id', 'ASC')->order_by('h.height_id', 'ASC')
$query = $this->db->get();

在循环中只使用列名

foreach ($res->result_array() as $row) {
        $weight=$row['weight_pounds']* 4.88;
        $height=($row['height_cm']*0.032808)*($row['height_cm']*0.032808);  
        $bmi=$weight/$height;
        $ret[] = $bmi;  //final bmi formuala calculated
    }

Reference Active record

答案 1 :(得分:0)

参考@dianuj答案,我的努力和搜索,我发现了一个解决方案:

为了区分两个表,我使用每个相同输入的额外列same_id。像

ON  w.same_id = h.same_id

最后我的代码如下所示,并解决了我的问题

$this->load->database(); 
$this->db->select('w.weight_pounds, h.height_cm'); 
$this->db->from('user_weight w','user_height h');
$this->db->join('user_height h' ,'w.same_id = h.same_id');
$this->db->where('w.creater_id',$uid); 
$this->db->where('h.creater_id',$uid); 
$this->db->group_by('w.weight_pounds');
$this->db->order_by('w.uweight_id', 'ASC')->order_by('h.height_id', 'ASC');  
$res = $this->db->get(); 
$ret = array(); 
foreach ($res->result_array() as $row) {
    $weight=$row['weight_pounds']* 4.88;
    $height=($row['height_cm']*0.032808)*($row['height_cm']*0.032808);  
    $bmi=$weight/$height;
    $ret[] = $bmi;  //final bmi formuala calculated

}