Java web —— 日常踩坑之 MySql
1、left Join
在我的想象中 表A left Join B on a.id == b.id 是以表A为主表 B中满足条件的生成中间表 B中不存在则相应字段为null;这是对的。
但是如果这时候问题来了 A是订单,B是订单的操作日志的时候 也就是 A.id 对应多条B.id时 查询结果会产生多条记录。
2、for update
在Hibernate中 hsql对 该语法做了封装 可以直接
update userInfo a set a.statusCode = 402,a.step=8 where a.userId= 43 AND a.statusCode = 400 for update
然而原生sql的话 不支持 for update直接用 需要在 begin 与 commit 之间
正确的原生写法是这样的
start transaction ;
select * from table_name where id =1 for update ;
update table_name set count = count - 1 where id= 1;
commit;
此时如果另一个事务也想执行类似的操作。
start transaction ;
select * from table_name where id =1 for update ;
//下面的这行sql会等待,直到上面的事务回滚或者commit才得到执行。
update table_name set count = count - 1 where id= 1;