数据库的外键引入导致表格排序不按照主键排序,怎么解决?

烨炎花 2019-03-22 05:28:09
插入时,链表排序不正常
deptName作为外部键
情况:假如deptName部门信息不变时,都是 测试部,链表deptId为2,正常排序,如1001,1002
但是假如改了部门信息deptName为 开发部 时,顺序跑到1001、1002上面,此时如果不改部门信息还是开发部,继续添加,又正常按照顺序排列


emp_id是链表主键,deptName是外表的数据,作为外部键引入,deptId为外表主键



mapper文件里我也看了
  <!--     List<Employee> selectByExampleWithDept(EmployeeExample example);
Employee selectByPrimaryKeyWithDept(Integer empId); -->
<select id="selectByExampleWithDept" resultMap="WithDeptResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="WithDept_Column_List" />
FROM tbl_emp e
LEFT JOIN tbl_dept d ON e.`d_id`=d.`dept_id`
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKeyWithDept" resultMap="WithDeptResultMap">
select
<include refid="WithDept_Column_List" />
FROM tbl_emp e
LEFT JOIN tbl_dept d ON e.`d_id`=d.`dept_id`
where emp_id = #{empId,jdbcType=INTEGER}
</select>


改的话,尝试在 order by ${orderByClause} 改为 order by emp_id或者order by e.emp_id

但没有作用。

请教大佬们,到底怎么做才能按照emp_id排序,而不是以外键d_id排序为主,emp_id为次的排序?

在下刚学习SSM,以上代码来自网络教程

如有能指点迷津,在下不胜感激!
...全文
207 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

5,658

社区成员

发帖
与我相关
我的任务
社区描述
Web开发应用服务器相关讨论专区
社区管理员
  • 应用服务器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧