如何在单个函数中执行两个表列数据 [英] how to execute two table column data in a single function
问题描述
我必须通过以下方式为用户计算BMI:Users_height& users_weight来自不同的两个表。
以下是两个表格(
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
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
}
这篇关于如何在单个函数中执行两个表列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!