问题
I have three database table
1st
vedd_plan_task ::
task_id | task name
2nd
plan_task_mapping ::
task_mapp_id | plan_id | task_id
3rd
tbl_plan ::
plan_id | plan_title | plan_price | plan_desc
In this table i mapped the 2nd table with 1st & 3rd. (2 table's value come from 1st & 3rd)
for PLAN i call the data from PLAN TASK tbl. and submit the data in two different table. now i have to show the database as table but i can't find the way to get a task_name form mapping table.
I want a table with this field
Plan_id | plan_title | plan_price | plan_desc | task_name (which i inserted before with particular plan id)..
回答1:
$query = $this->db->query('SELECT tbl_plan.plan_id ,plan_title ,plan_price ,plan_desc ,task_name,plan_task_mapping.task_id FROM tbl_plan LEFT JOIN plan_task_mapping ON plan_task_mapping.plan_id = tbl_plan.plan_id LEFT JOIN vedd_plan_task ON vedd_plan_task.task_id = plan_task_mapping.task_id');
foreach ($query->result() as $row) {
echo $row->plan_id;
echo $row->plan_title;
echo $row->plan_price;
echo $row->plan_desc;
echo $row->task_name;
echo $row->task_id;
} ?>
回答2:
$this->db->join('vedd_plan_task','vedd_plan_task.task_id=plan_task_mapping.task_id','inner');
$this->db->join('plan_task_mapping ','plan_task_mapping.plan_id=tbl_plan.plan_id ','inner');
$this->db->get('tbl_plan');
回答3:
$this->db->select('tbl_plan.plan_id,tbl_plan.plan_title,tbl_plan.plan_price,tbl_plan.plan_price,tbl_plan.plan_desc,vedd_plan_task.task_name');
$this->db->from('plan_task_mapping,tbl_plan,vedd_plan_task');
$this->db->where('plan_task_mapping.plan_id = tbl_plan.plan_id and plan_task_mapping.task_id=vedd_plan_task.task_id');
$query = $this->db->get();
return $query;
来源:https://stackoverflow.com/questions/31427631/how-to-query-fetch-a-multiple-table-of-mysql-database-as-a-single-table-in-cod