数据重新排序问题
在一张表中如果删掉了几条数据ID就会断,看的很不爽,如果不是根据Id关联的数据就可以把Id重新排。
UPDATE tableName
SET ID = (
SELECT
@i := @i + 1
FROM
(SELECT @i := 0) i
)
如果想要按照某个顺序更新的话
支持rank语句的数据库可以这样
update (select ROW_NUMBER() over (order by `key`) as tid,id from tableName) t1,tableName t2 set t2.id = t1.tid where t2.id = t1.id
不支持的自己写rank
update (select(select @i := @i + 1 from (select @i := 0) as i )as tid,id from tableName) t1,tableName t2 set t2.id = t1.tid where t2.id = t1.id
但是这样过程冲会有id重复,所以可能会报错
关于Mybatis传入字符串类型问题
mybatis会尝试将map中的字符串类型转为数字类型比较 如test表中如果有如下数据 key为字符串类型
+----+------+
| id | key |
+----+------+
| 1 | 002 |
| 2 | 2 |
+----+------+
如果用如下查询查询
当key传 map 有 key : 002 时可以查出所有数据
<select id="selectByMap" parameterType="map" resultTpe="map">
select *
from test
<where>
<if test="key!=null">
key = #{key}
</if>
</where>
</select>
把中间变成时只能查处一条数据 应该时在 key!= ’ '的时候把key认成了字符串
<if test="key!=null and key!= ''">
key = #{key}
</if>
关于update更新问题
现在我们有一个表t1
+----+------+-----+
| id | key1 | key2 |
+----+------+------+
| 1 | a | |
+----+------+------+
如果我想有同时更新key1,key2
update t1 set key1 = 'b',key2 = key1
这样可以同时更新
但是如果声明另一个表,尽管不用
update t1,t2 set key1 = 'c',key2 = key1
key2和key1的值会不一样 key2的值会是key1修改之前的值,因为再关联更新时两个表都是映射表,在更新完之后才会同步到实际表