-- 更新左右值
--------------
-- 这边我们关注影响的行数,明细比之前全表更新的少。
--------------
UPDATE area
SET left_num = left_num + 2
WHERE left_num > 2879
AND top_layer_id = 2147;
Query OK, 13 rows affected (0.03 sec)
Rows matched: 13 Changed: 13 Warnings: 0
UPDATE area
SET right_num = right_num + 2
WHERE right_num > 2879
AND top_layer_id = 2147;
Query OK, 15 rows affected (0.01 sec)
Rows matched: 15 Changed: 15 Warnings: 0
-- 插入 '北沙区' 信息
INSERT INTO area
SELECT NULL, '北沙区', '440116', 2148, left_num + 1, left_num + 2, 2147
FROM area WHERE area_id = 2148;
--查看是否满足要求
SELECT c.*
FROM area AS p, area AS c
WHERE c.left_num BETWEEN p.left_num AND p.right_num
AND p.area_id = 2148;
+---------+-----------+-----------+------+----------+-----------+--------------+
| area_id | name | area_code | pid | left_num | right_num | top_layer_id |
+---------+-----------+-----------+------+----------+-----------+--------------+
| 2148 | 广州市 | 440100 | 2147 | 2879 | 2908 | 2147 |
| 3613 | 北沙区 | 440116 | 2148 | 2880 | 2881 | 2147 |
| 3612 | 南沙区 | 440115 | 2148 | 2882 | 2883 | 2147 |
| 2161 | 从化市 | 440184 | 2148 | 2884 | 2885 | 2147 |
| 2160 | 增城市 | 440183 | 2148 | 2886 | 2887 | 2147 |
| 2159 | 花都区 | 440114 | 2148 | 2888 | 2889 | 2147 |
| 2158 | 番禺区 | 440113 | 2148 | 2890 | 2891 | 2147 |
| 2157 | 黄埔区 | 440112 | 2148 | 2892 | 2893 | 2147 |
| 2156 | 白云区 | 440111 | 2148 | 2894 | 2895 | 2147 |
| 2154 | 天河区 | 440106 | 2148 | 2896 | 2897 | 2147 |
| 2153 | 海珠区 | 440105 | 2148 | 2898 | 2899 | 2147 |
| 2152 | 越秀区 | 440104 | 2148 | 2900 | 2901 | 2147 |
| 2151 | 荔湾区 | 440103 | 2148 | 2902 | 2903 | 2147 |
| 2150 | 东山区 | 230406 | 2148 | 2904 | 2905 | 2147 |
| 2149 | 其它区 | 440189 | 2148 | 2906 | 2907 | 2147 |
| 1997 | 湖南省 | 430000 | 0 | 2908 | 3209 | 1997 |
+---------+-----------+-----------+------+----------+-----------+--------------+