我必须通过从不同的两个表中获取 Users_height 和 users_weight 来计算用户的 BMI。
这是两张表(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 表
user_height 表
我试过这个功能,但它显示错误:
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');
这段代码,现在它工作正常