1、需求分析:
现在有一张学科表(subject_test),字段有 id、name(学科名称)、up (父级id)
目前有三个级别的学科,一级学科、二级学科、三级学科
一级学科的up值为1,二级学科的up值为对应的一级学科的id,三级学科的up值为对应的二级学科的id。
需求:将一级学科名称二级学科名称三级学科名称查询出来,对照关系正确
2、实现:
/*查询学科对应关系及一级学科名称、二级学科名称、三级学科名称*/
select one.name as name1 ,two.name as name2 , three.name as name3from subject_test as one inner join subject_test as two on two.up = one.id inner join subject_test as three on three.up = two.id
where one.up = 1
order by one.id,two.id //排序
3、结果: