问题
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

user_height table

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
回答1:
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
回答2:
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
}
来源:https://stackoverflow.com/questions/18803289/how-to-execute-two-table-column-data-in-a-single-function