会员登录 | 会员注册 | 意见建议 | 网站地图

站长资源综合门户

当前位置:首页 > 站长学院 > 数据库 > MYSQL操作常用命令大全

MYSQL操作常用命令大全

时间:2012-08-16 20:39:15   作者:   来源:   点击:

--====================右外连结 ======================

select st.st_id as '学生编号', st.st_name as '学生姓名',cl.cl_id as '班级编号',cl_class as '班级名称'

from studio as st right outer join class as cl

on st.cl_id=cl.cl_id

where cl.cl_id>2

--多表

select tka.te_co_id as '课程安排编号'

,cl.cl_id as '班级编号',cl.cl_class as '班级名称'

,co.co_id as '课程ID',co.co_name as '课程名称',co.co_num as '课时数'

,te.te_name as '老师姓名'

from te_kc_ap as tka

right outer join class as cl

on

tka.cl_id=cl.cl_id

right outer join teacher te

on

tka.te_id=te.te_id

right outer join course co

on

tka.co_id=co.co_id

--=======完全连接=============

select st.st_id as '学生编号', st.st_name as '学生姓名',cl.cl_id as '班级编号',cl_class as '班级名称'

from studio as st full outer join class as cl

on st.cl_id=cl.cl_id

order by st.st_id

--多表

select tka.te_co_id as '课程安排编号'

,cl.cl_id as '班级编号',cl.cl_class as '班级名称'

,co.co_id as '课程ID',co.co_name as '课程名称',co.co_num as '课时数'

,te.te_name as '老师姓名'

from te_kc_ap as tka

full outer join class as cl

on

tka.cl_id=cl.cl_id

full outer join teacher te

on

tka.te_id=te.te_id

full outer join course co

on

tka.co_id=co.co_id

--==========交叉连接==============

--该方式在不带where子句时,返回的是两个表中所有数据行的笛卡尔积(第一个表中的行乘以第二个表中的行)

--用学生和班级表做交叉查询

select st_name,cl_class from studio cross join class

select st_name,cl_class from studio,class

select st_name,cl_class from studio cross join class

--=========自连接===

-----------------先临时创建一个表-------------

create table zone(

id int primary key identity(1,1) not null,

z_zone varchar(30),

z_id int references zone(id))

--大家试下,这里是否可以给个默认值

select * from zone

insert into zone(z_zone) values('北京')

insert into zone(z_zone,z_id) values('北京',4)

insert into zone(z_zone) values('四川')

insert into zone(z_zone,z_id) values('成都',6)

insert into zone(z_zone,z_id) values('绵阳',6)

insert into zone(z_zone) values('江苏')

insert into zone(z_zone,z_id) values('南京',10)

insert into zone(z_zone,z_id) values('苏州',10)

insert into zone(z_zone,z_id) values('无锡',10)

insert into zone(z_zone,z_id) values('常州',10)

----------------------------------------------

--看下自连接的一般用处

select a.z_zone,b.z_zone from zone as a inner join zone as b on a.z_id=b.id

--扩展应用下

select b.z_zone,count(a.z_zone) as '辖区数' from zone as a inner join zone as b on a.z_id=b.id group by b.z_zone

--简单说就是自己连接自己,换言之对同一个表进行连接操作

select a.st_name,a.st_add,b.st_name,b.st_add from studio as a inner join studio as b on a.st_add=b.st_add

--我们发现有人等于自己,那么增加一个条件

select a.st_name,a.st_add,b.st_name,b.st_add from studio as a inner join studio as b on a.st_add=b.st_add and a.st_name!=b.st_name

--======子查询=====爱微网-天轰穿-======

--在一个SQL语句中镶入另一个SQL语句教镶套查询,而被镶入的这个SQL语句就被江湖人称子查询。是处理多表操作的附加方法

--子查询也称内部查询,而包含子查询的Select语句被诚为外部查询,子查询自身可以包括一个或者多个子查询,也可以镶套任意数量的子查询

--使用in的子查询

select * from studio where cl_id in (select cl_id from class where cl_id>2)

--使用 not in

select * from studio where cl_id not in (select cl_id from class where cl_id>2)

--使用比较运算符的子查询 -- any 表示子查询中任意的值 all 表示子查询中的每个值

--使用any

select * from class where cl_id>any(select cl_id from studio where st_age>30)

--使用all

select * from class where cl_id>all(select cl_id from studio where st_age>30)

--===========一个分页的SQL语句=======

select top 3 * from studio

where st_id>all(select top 3 st_id from studio order by st_id)

order by st_id

--使用 exists ,该关键字引入一个子查询的时候基本上是对数据进行一次是否存在的测试

分享到:

网友评论

推荐数据库

热门数据库