繁体   English   中英

Codeigniter-从辅助表获取价值

[英]Codeigniter - Getting Value from a Secondary Table

我的数据库中有3个表:“锻炼”,“锻炼”和“ exercise_list”。

锻炼: id | 日期时间| misc1 | misc2 |

练习: id | ex_id | wo_id | 重量 代表 wo_order |

运动清单: id | 标题|

到目前为止,我已经生成了一个视图,其中包含特定锻炼的详细信息( myurl.com/workouts/view/<datetime>

我建立了一个查询,可以从“锻炼”中获取字段,还可以获取与该锻炼相对应的任何“锻炼”条目(通过使用wo_id的get_where)。

我建立了一个视图,其中列出了该锻炼的练习,但是我只能深入了解该练习的“ id”。 我需要以某种方式进一步查询,以获取与该锻炼“ id”相关联的每个练习的“标题”。

所以我目前有一张桌子(html):

| 锻炼| 重量 代表

| 1 | 50 | 8 | ...

我需要'1'成为'exercise_list'中练习的标题,并且'id'为'1'。


我的解决方案

可能不是完美的,但它可以工作:

public function get_exercises($wo_id)
  {
    $this->db->select('exercises.wo_id,
                       exercises.weight,
                       exercises.reps,
                       exercise_list.title');
    $this->db->from('exercises');
    $this->db->join('exercise_list','exercises.ex_id= exercise_list.id');
    $this->db->where('exercises.wo_id',$wo_id);
    $q = $this->db->get();
    $query = $q->result_array();
    return $query;
  }

不确定执行最后几行的最佳方法。 这是在我的模型中,因此我需要返回数组。 我要说有一种方法可以比最后三行更好。

您可以使用联接并从exercise_list表中选择标题

$this->db->select('w.*,el.title')
    ->from('workouts w')
    ->join('exercises e','w.id = e.wo_id')
    ->join('exercise_list el','el.id = e.ex_id')
    ->where('e.wo_id',$yourid)
    ->get()
    ->result();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM