【问题标题】:how to execute two table column data in a single function如何在单个函数中执行两个表列数据
【发布时间】:2013-09-14 15:22:48
【问题描述】:

我必须通过从不同的两个表中获取 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');这段代码,现在可以正常工作了

【问题讨论】:

    标签: php mysql sql codeigniter


    【解决方案1】:

    试试这个join你还没有加入你的表,如果你需要用户的详细信息,如身高、体重等,那么你应该在你的表中保存用户信息和用户ID,因为有一个表- to- 关系,然后根据该用户 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));
    

    $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

    【讨论】:

    • 对不起,答案不完整,creater_id是用户creater_id的id,所以所有这些数据都是由同一个用户创建的,因此The join column is full of duplicates.。这是两个表(i.stack.imgur.com/eOQs4.gif) & (i.stack.imgur.com/liqhH.gif)
    • @user2732367 那么这些表格中的用户 ID 在哪里,您如何将这些表格中单个用户的身高和体重联系起来??
    • user_id是以上2个表的外键,userdetails表的主键
    • 现在我解决了我的问题。我在两个表same_id 中添加了一个新字段,并用$this->db->join('user_height h' ,'w.same_id = h.same_id'); 这个代码替换了这行($this->db->join('user_height h' ,'w.creater_id = h.creater_id'))你的代码。现在它工作正常。我要求您在答案中指定这些内容,并在您的答案中进行一些更改,以便我可以接受它作为正确答案。
    • 因为你已经发布了查询所以我猜creater_id 存在于两个表中所以我假设它的用户 ID 所以我现在提供了连接示例如果你添加了 same_id 来关联然后很好:)
    【解决方案2】:

    参考@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
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-04
      • 2020-10-13
      • 2020-10-04
      • 2020-08-13
      • 2016-02-02
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多