首页 > 代码库 > sql开发技巧总结-1
sql开发技巧总结-1
1.数据库分类
关系型 非关系型
2.sql语句分类
sql: ddl数据库定义语言 tpl事物处理语言 dcl数据控制语言 dml数据操作语言(insert delete update select)
增加数据库处理效率 减少相应时间
减少数据库服务器负载 增加服务器稳定性
减少服务器间通讯网络流量
3.join语句
inner(内连接)
--inner join
内连接inner join基于连接谓词将两张表(如A和B)的列组合在一起,产生新的结果表
select <select_list> from tableA A inner join tableB B on A.key=B.key;
(select a.`user_name`,a.`over`,b.`over` from user1 a inner join user2 b on a.`user_name`=b.`user_name`;)
full outer(全外连接)
--full join
(如何在mysql中使用full join??)
join操作类型-full join
select a.`user_name`,a.`over`,b.`over`
from user1 a
left join user2 b on a.`user_name`=b.`user_name`
union
select b.`user_name`,b.`over`,a.`over`
from user1 a
right join user2 b on a.`user_name`=b.`user_name`
left outer(左外连接)
--left join
select a.`user_name`,a.`over`,b.`over` from user1 a left join user2 b on a.`user_name`=b.`user_name` where b.user_name is null;
right outer(右外连接)
--right join(同left join)
cross(交叉连接)
--cross join
笛卡尔查询
4.join语句
更新使用过滤条件中包括自身的表
联合更新
update user1 a join(
select b.`user_name` from user1 a inner join user2 b on
a.`user_name`=b.`user_name`
) b on a.`user_name`=b.`username`
set a.over = ‘qitiandasheng‘;
5.使用join优化子查询
6.使用join优化聚合子查询
查询四人组中打怪最多的日期(避免子查询)
select a.`username`,b.`timstr`,b.kills from user1 a join user_kills b on a.id = b.user_id where
b.kills = (select max(c.kills) from user_kills c where c.user_id=b.user_id);
select a.user_name,b.timestr,b.kills from user1 a
join user_kills b on a.id = b.user_id
join user_kills c on c.user_id = b.user_id
group by a.user_name,b.timestr,b.kills
having b.kills = MAX(c.kills);
sql开发技巧总结-1