我正在从事教育项目,遇到了简单的逻辑.我有两个表月份和semester_type.下面是它们的架构和数据;
月表
month_id month_name month_value lupdate
1 January 1
2 February 2
3 March 3
4 April 4
5 May 5
6 June 6
7 July 7
8 August 8
9 September 9
10 October 10
11 November 11
12 December 12
这是我的semester_type表;
semester_type_id semester_type_name start_month end_month
1 Fall 8 12
2 Summer 1 4
这是我想要的输出;
Semester Name Start Month End Month
Fall August December
Summer January April
我对内部连接两个表中具有start_month和end_month列的month_id感到困惑.有人可以帮助我与Codeigniter查询
解决方法:
与学期表一起两次加入月份表
select s.semester_type_name,
m.month_name start_month ,
m1.month_name end_month
from semester s
join month m on(m.month_id = s.start_month)
join month m1 on(m1.month_id = s.end_month)
使用codeigniter的活动记录库,您可以将其编写为
$this->db->select('s.semester_type_name,m.month_name start_month ,m1.month_name end_month')
->from('semester s')
->join('month m','m.month_id = s.start_month')
->join('month m1','m1.month_id = s.end_month')
->get()
->result();