Share a case when then when then end sql statement writing usage

writing background

Usually I case when then else enddon’t use it much, I used it to calculate the select result, for example:

select job,ename,(case job when 'MANAGER' then sal*1.1 when 'SALESMAN' then sal*1.5 else sal*1.8 end) as "newSal" from emp;

The function of the above sql statement is to judge the value of the job field, and then calculate the salary according to different values

Today I was looking at the Ruoyi project, and I saw the move operation of the department structure tree. Although it is actually an operation to modify the organization of the department, what is hidden behind it is the move operation of the structure in the tree. Because Ruoyi is defining the department table At that time, a field was added ancestors, which is actually the path field, which stores the combination of path id strings from the highest level to the current level (separated by English commas in the middle), so when we change the parent level of a certain department, we must not only Update ancestorsthe fields of the department, and also update all ancestorsthe fields of all sub-departments of the department. Let's take a look at the page, as follows:

insert image description here
According to the above figure, we can see that we want to move 研发中心the following to the bottom, but there are two sub-level departments under the Changsha branch. According to the above statement, we not only need to modify the field (type path, full path id string combination) , also update all fields in all subdepartments of the department长沙分公司若依科技长沙分公司ancestorsancestors

I'm in the same line of thinking as Zoe's previous part

First find the 长沙分公司previous ancestorsfield value according to the department id, and then get 长沙分公司the latest ancestorsfield value (first get the latest parent ancestorsfield value, plus the parent id, this is 长沙分公司the latest ancestorsfield value)

Find all the sub-level data according to the id of the Changsha branch, and then replace the 长沙分公司old and new field values ​​obtained aboveancestors

Now we have obtained the latest ancestorsfield values ​​of all sub-level departments. The code logic is as follows:

List<SysDept> children = deptMapper.selectChildrenDeptById(deptId);
for (SysDept child : children)
{
    
    
    child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
}

My follow-up solution ideas (not recommended)

If it were me, I would probably update them one by one according to the department id. This method will interact with the database multiple times, so it is not recommended to do so

If you follow the follow-up solution ideas (suggestion)

Directly use an update statement to update all the data. Let's take a look at the code first, as follows:

java code:

List<SysDept> children = deptMapper.selectChildrenDeptById(deptId);
for (SysDept child : children)
{
    
    
    child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
}
if (children.size() > 0)
{
    
    
    deptMapper.updateDeptChildren(children);
}

sql file.xml:

<update id="updateDeptChildren" parameterType="java.util.List">
	update sys_dept set ancestors =
	<foreach collection="depts" item="item" index="index"
		separator=" " open="case dept_id" close="end">
		when #{
    
    item.deptId} then #{
    
    item.ancestors}
	</foreach>
	where dept_id in
	<foreach collection="depts" item="item" index="index"
		separator="," open="(" close=")">
		#{
    
    item.deptId}
	</foreach>
</update>

Let's take a look at what the specific sql looks like, as follows:

update sys_dept set ancestors = case dept_id when ? then ? when ? then ? end where dept_id in ( ? , ? )

As can be seen from the above sql, first find all the data that needs to be updated through the where condition, and then determine the value of ancestors by judging the value of dept_id when updating, so a single sql statement can solve all sub-category update problems, Is it nice?

At present, if we use the persistence layer framework mybatis, if we use it mybatis-plus, we can use baseMapper.updateBatchById()the method to perform batch update operations

According to the example of follow-up solutions

For the above situation, let's give a demo, assuming that the original data looks like this:

insert image description here
We want to update all sexthe values ​​​​that are male to 20, and the values ​​​​that are equal to female to 18. The sql statement is as follows:agesexage

update t_user 
set age =
	case sex 
		when "男" THEN 20 
		when "女" THEN 18 
	end

The final result is as follows, wish fulfilled

insert image description here

Guess you like

Origin blog.csdn.net/qq_42449963/article/details/130977846