how to execute two table column data in a single f

2019-09-06 01:09发布

i have to calcuate BMI for a user by taking Users_height & users_weight from the different two tables.

Here is the two tables(http://i.stack.imgur.com/eOQs4.gif) & (http://i.stack.imgur.com/liqhH.gif)

DESIRED OUTPUT

  weight_pounds    height_cm 
     121.25           130
     132.28           160
     154.32           221
     176.37           434

user_weight Table enter image description here

user_height table enter image description here

I tried this function, but its showing error:

 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;
}  

Error is:

 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

Hope someone can help me here...

UPDATE

Added extra column same_id in both the column to meet the requirements and added $this->db->join('user_height h' ,'w.same_id = h.same_id'); this piece of code, now its working fine

2条回答
我只想做你的唯一
2楼-- · 2019-09-06 01:52

With reference to @dianuj Answer, and my little effort and searched , i found out a solution :

To distinguish between two table i use an extra column same_id of every same input . like

ON  w.same_id = h.same_id

Finally my code looks like below, and solved my problem

$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

}
查看更多
家丑人穷心不美
3楼-- · 2019-09-06 01:56

Try this with join you haven't joined your tables, if you need the user details like height , weight etc then in your tables you should save the user information along with the user id in both tables as there is one - to- relation , then join your tables on the basis of that user id to calculate the bmi for user

$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();

And in loop just use the column name

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

查看更多
登录 后发表回答