这里只是写了我面试的时候让我写的一些sql以及自己对个别的总结,没有其他的!
1.sql 行转列,列转行
首先:行转列:
源数据:
转换后:
列转行
转换后:
三、删除user_name重复的数据,只保留其中一条(实例是在postgresql中操作的,mysql删除的时候,要有临时表)。
1.查询出重复的数据,并且只保留重复中id最小的一条数据
select id from t_two where user_name in (
SELECT user_name FROM t_two group by user_name having count(user_name)>1)
and id not in (SELECT min(id) FROM t_two group by user_name having count(user_name)>1)
2.删除筛选后的重复数据
delete from t_two where id in (
select id from t_two where user_name in (
SELECT user_name FROM t_two group by user_name having count(user_name)>1)
and id not in (SELECT min(id) FROM t_two group by user_name having count(user_name)>1)
)
数据量大的话,可以考虑存储过程
其他:
1.drop,delete与truncate的区别
drop > truncate > delete
2.索引建立的条件
3. UNION和UNION ALL 区别:UNION两个表中不重复的值得集合,如果有重复,只取其中一条,而后者都会取
4.mysql主从复制过程
5.sql 创建用户和赋值权限:
-- 赋予部分权限,其中的shopping.*表示对以shopping所有文件操作。
grant
select
,
delete
,
update
,
insert
on
simpleshop.*
to
superboy@
'localhost'
identified
by
'superboy'
;
flush
privileges
;
-- 赋予所有权限
grant
all
privileges
on
simpleshop.*
to
superboy@localhost identified
by
'iamsuperboy'
;
flush
privileges
;
6.sql查询某个字段长度最长的那条记录
四、sql执行顺序
五、sql 连接效率
六、MySQL count(1) count(*) 比较 详解
额外说一句:mybatis中:
<if test="version != null">
and version = #{version,jdbcType=INTEGER}
</if>
注意:version 为int 或者 integer类型的字段时,在判断 != '' 时,等效于 !=0
mybaits支持批量返回主键:官网上面写的很清楚:
http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#insert_update_and_delete