1.查询某个表的某个字段批量新增到另外一个表:
INSERT INTO insertTable (var1,var2,var3,create_time,var4)select field1,field2,constant1,1,now(),NOW(),constant2 from selectTalble;
2.删除名字重复的,如果重复保留id最小的
delete from table where id not in (select minid from (select min(id) as minid from table group by name) b);
3.查看某个数据库的所有数据量
SELECT TABLE_NAME, TABLE_ROWS FROM `information_schema`.`tables` WHERE `table_schema` = 'databaseName'
4.判断某个列是否重复
select col,count(*) as ct from tableGROUP BY col HAVING ct>1;
5.字符串拼接用concat
concat('code201803190000',id)
6.需经常查询的字段或关联性比较多的,或区分比较大的字段,经常用的字段推荐建索引.如果ABC经常一起使用,特别是A是必须有的情况下可以建组合索引
7.sql语句关联进来走有索引的字关联,对于数据量大的尽量缩小查询范围,where后面直接跟范围筛选最多,有索引的
8.mybaits中,resultMap一般对应自定义实体类,
resultType对应常用引用类或包装类,如:Integer,Double,String,HashMap,
jdbcType对应的类型是sql的类型,如VARCHAR,TIMESTAMP,BIT,INTEGER都是大写,(而不是实体类里的Date,String)
9.常用的mysql关键字:curdate(),date_add,now(),ifnull(),concat,sum,count,round,max,min
10.批量新增:
<foreach collection="list" item="asd" index="index" separator=",">
()
</foreach>
11.批量更新:
<foreach collection="list" item="item" separator=";" index="index" open="" close="">
update XXX set yy=#{item.yy} where id=#{item.id}
</foreach>
12.对于经常用的且不易改变的,比如字典,可以放入redis缓存
13:多个选择:choose-when-otherwise用于查询条件选择,case when-then-else-end用于字段参数选择
14.删除主从表,特别唯一关联的是主表某个唯一字段,
DELETE s.*, main.* FROM sub s
INNER JOIN main m ON s.m_id= m.id
WHERE code=#{code};
DELETE from main WHERE code=#{code};
15.两表更新:update main m,main_copy mc set m.code=mc.code where m.id =mc.id