Mysql必读常用的SQL例句 数据库开发所需知识

Mysql必读常用的SQL例句 数据库开发所需知识,第1张

概述介绍《Mysql必读常用的SQL例句 数据库开发所需知识》开发教程,希望对您有用。

《MysqL必读常用的sql例句 数据库开发所需知识》要点:
本文介绍了MysqL必读常用的sql例句 数据库开发所需知识,希望对您有用。如果有疑问,可以联系我们。

--查看学生表的全部数据
select * from studio
--插入一个新的学生信息
insert into studio(st_name,st_sex,st_age,st_add,st_tel) values("黄兰淇",36,'南充','13943943334')
--查看class全部数据
select * from class
--向class表增加两条条数据
insert into class(cl_class,cl_Coding,cl_o_time,cl_remark) values('新电实训班','GXA-ncs-001','2008-03-11','都是很优秀的朋友')
insert into class(cl_class,cl_o_time) values('阿坝师专实训班','GXA-ABSZ-001','2008-03-11')
--更新一条的数据 条件的重要性
update class set cl_remark='真的是不错' where cl_ID=5
--删除一条数据 条件的重要性
delete from class where cl_ID=7
--修改列标题
select cl_ID as '班级主键',cl_class as '班级名称' from class
select 名字=st_name from studio
--使用文字串
select '名字是:',st_name from studio
--=============条件稍微复杂点的查增删改==============
--主要涉及到 or and not between in like > < = !> !< != <> () <= >= is null is not null
--查询cl_ID 大于 1 的所有信息
select * from class where cl_ID>1
--使用 or
select * from class where cl_ID<>10 or cl_class='百杰一班'
--使用and
select * from class where cl_ID<>10 and cl_class='百杰一班'
--使用like 和 %
select * from class where cl_class like '百杰%'
select * from class where cl_remark like '%上午%'
--使用 between
select * from class where cl_ID between 3 and 5
--使用 between 配合上 not
select * from class where cl_ID not between 3 and 5
--使用 is not null
select * from class where cl_remark is not null
--使用 in
select * from class where cl_class in('千星一班','百杰二班')
--=================使用数学运算符======================
--主要涉及到 + = * \
--查询Java相关课程分别要上多少周 按照每周5天,每天6节课来计算
select '结果'=co_num/5/6 from course where co_name in ('Java基础','Java项目入门')
--==================使用汇总函数 ========================
--涉及到COUNT SUM AVG MAX MIN
--查询课时数小于50的课程一共有多少门
select count(*) from course where co_num<50
--查询所有课程一共多少课时
select sum(co_num) from course
--计算全部课时费,假设每节课50块钱
select sum(co_num)*50 from course
--查询课时最少的课程
select min(co_num) from course
--查询课时最多的课程
select max(co_num) from course
--查询平均每门课多少课时
select avg(co_num) from course
--=================使用数学函数=============================
--包括求绝对值函数ABS函数、求圆周率函数PI()、求正玄值SIN()函数、求指数函数EXP()等.
--查询每门课的正弦值
select sin(co_num) from course
--查询每门课的绝对值
select abs(co_num) from course
--查询每门课课时数 乘以 圆周率,具体有什么用我也不知道,反正这好像绝对是8.5杆子都打不到的
select pi()*co_num from course
--查询每门课的指数
select exp(co_num) from course
--随机返回5个随机生成的数(返回的是0~1之间的随机float值)
declare @i tinyint
set @i=1
while @i<=5
begin
select rand(@i) as '随机生成的数',@i as '当前值'
set @i=@i+1
end
--返回数字表达式并四舍五入为指定的长度或精度 - ROUND
select round(345.456,-1) as '参数为-1'
,round(345.456,-2,1) as '参数为-2'
,0) as '参数为0'
,1) as '参数为1'
,2) as '参数为2'
--================使用日期函数======================
--DAY()、MONTH()、YEAR()――返回指定日期的天数、月数、年数;
select day(cl_s_time) as '日' from class --返回天
select '月'=month(cl_s_time) from class --返回月
select '年'=year(cl_s_time) from class --返回年
--DATEADD(datepart,number,date)――在日期上增加给定日期类型的数量;
select dateadd(yyyy,4,cl_s_time) as '增加4年后' from class --datepart - 年份
yy、yyyy
select dateadd(q,2,cl_s_time) as '增加2季度后' from class
--datepart - 季度
qq、q
select dateadd(mm,3,cl_s_time) as '增加3月度后' from class
--datepart - 月份
mm、m
--datepart - 每年的某一日
dy、y
--datepart - 日期
dd、d
--datepart - 星期
wk、ww
--datepart - 小时
hh
--datepart - 分钟
mi、n
--datepart - 秒
ss、s
--datepart - 毫秒
ms
--DATEDIFF(datepart,date1,date2)――获取两个日期之间给定的日期类型的数量差(整个函数结果是date2-date1);
select datediff(mm,cl_s_time,cl_o_time) as '共持续月' from class
--datepart(datepart,date)――在给定日期基础上返回指定日期类型的值(整数);
--其实这个等同于DAY、MONTH、和 YEAR 函数
select datepart(dd,cl_s_time) as '日期' from class
--GETDATE()――返回当前日期和时间.我们在设计数据库的时候,通常也可能把他作为默认值
update class set cl_s_time=getdate() where cl_ID=6
select * from class
Select CONVERT(varchar(100),GETDATE(),0): 05 16 2006 10:57AM
Select CONVERT(varchar(100),1): 05/16/06
Select CONVERT(varchar(100),2): 06.05.16
Select CONVERT(varchar(100),3): 16/05/06
Select CONVERT(varchar(100),4): 16.05.06
Select CONVERT(varchar(100),5): 16-05-06
Select CONVERT(varchar(100),6): 16 05 06
Select CONVERT(varchar(100),7): 05 16,06
Select CONVERT(varchar(100),8): 10:57:46
Select CONVERT(varchar(100),9): 05 16 2006 10:57:46:827AM
Select CONVERT(varchar(100),10): 05-16-06
Select CONVERT(varchar(100),11): 06/05/16
Select CONVERT(varchar(100),12): 060516
Select CONVERT(varchar(100),13): 16 05 2006 10:57:46:937
Select CONVERT(varchar(100),14): 10:57:46:967
Select CONVERT(varchar(100),20): 2006-05-16 10:57:47
Select CONVERT(varchar(100),21): 2006-05-16 10:57:47.157
Select CONVERT(varchar(100),22): 05/16/06 10:57:47 AM
Select CONVERT(varchar(100),23): 2006-05-16
Select CONVERT(varchar(100),24): 10:57:47
Select CONVERT(varchar(100),25): 2006-05-16 10:57:47.250
Select CONVERT(varchar(100),100): 05 16 2006 10:57AM
Select CONVERT(varchar(100),101): 05/16/2006
Select CONVERT(varchar(100),102): 2006.05.16
Select CONVERT(varchar(100),103): 16/05/2006
Select CONVERT(varchar(100),104): 16.05.2006
Select CONVERT(varchar(100),105): 16-05-2006
Select CONVERT(varchar(100),106): 16 05 2006
Select CONVERT(varchar(100),107): 05 16,2006
Select CONVERT(varchar(100),108): 10:57:49
Select CONVERT(varchar(100),109): 05 16 2006 10:57:49:437AM
Select CONVERT(varchar(100),110): 05-16-2006
Select CONVERT(varchar(100),111): 2006/05/16
Select CONVERT(varchar(100),112): 20060516
Select CONVERT(varchar(100),113): 16 05 2006 10:57:49:513
Select CONVERT(varchar(100),114): 10:57:49:547
Select CONVERT(varchar(100),120): 2006-05-16 10:57:49
Select CONVERT(varchar(100),121): 2006-05-16 10:57:49.700
Select CONVERT(varchar(100),126): 2006-05-16T10:57:49.827
Select CONVERT(varchar(100),130): 18 ???? ?????? 1427 10:57:49:907AM
Select CONVERT(varchar(100),131): 18/04/1427 10:57:49:920AM
--=============使用字符串函数=====================
--字符串链接运算符
select '结果显示' = '班级名称是:' + cl_class + ',班级编号是:' + cl_Coding from class
--使用SUBSTRING函数截取字符串
select substring(cl_class,1,4) from class
--从字符串的左边开始返回3个字符
select left(cl_class,3) from class
--同理,返回右边的
select right(cl_class,3) from class
--返回值的字符数
select len(cl_class) from class
--替换
select replace(cl_class,'实训','强化') from class
--==============使用系统函数====================
select host_ID()
--返回工作站标识号
select host_name()
--返回工作站所运行的计算机名称
select db_ID()
select db_name()
select object_ID('Stu_course_ADD')
--通过名称得到这个服务器对象的服务器ID
select object_name(151671588)
--同上相反
--=======学 云 网-天轰穿-[url]www.ixueyun.co m[/url]===使用其他子句====学 云网- 天轰 穿-[url]www.ixueyun.com[/url]=========
--首先是 order by 功能 - 排序
select * from studio order by st_name
--多排序条件
select * from studio order by st_name DESC,st_age DESC,st_sex DESC
--有条件,主要是看下条件和子句的位置
select * from studio where cl_ID=1 order by st_name
--GROUP BY 子句 功能 - 分组统计
select cl_ID as '班级编号',count(*) as '人数' from studio group by cl_ID
--按宿舍统计年龄平均值
select ho_ID as '宿舍编号',avg(st_age) as '平均年龄' from studio group by ho_ID
--多分组
select ho_ID as '宿舍编号',cl_ID as '班级编号',avg(st_age) as '平均年龄' from studio group by ho_ID,cl_ID
--有条件,主要是看下条件和子句的位置
select ho_ID as '宿舍编号',avg(st_age) as '平均年龄' from studio where cl_ID=1 group by ho_ID
--使用 having 子句 功能 - 指定组或者聚合的搜索条件,通常与group by 子句一起使用,完成分组查询后再进步筛选
select ho_ID as '宿舍编号',avg(st_age) as '平均年龄' from studio group by ho_ID having avg(st_age)>35
--多条件
select ho_ID as '宿舍编号',avg(st_age) as '平均年龄' from studio group by ho_ID having avg(st_age)>35 and ho_ID>2
--===========联合查询=======学 云 网-天轰穿-[url]www.ixueyun.com[/url]======
--使用union子句的查询称为联合查询,功能:将两个以上的查询结果集组合为一个单个结果集,该集中包括所有集中的全部行数据
--下面我们尝试将多个查询联合起来
select * from studio where cl_ID=1
union
select * from studio where ho_ID=1
union
select * from studio where st_age>=30
--下面我们继续利用上面的例题,增加上 All 看下效果
select * from studio where cl_ID=1
union all
select * from studio where ho_ID=1
union all
select * from studio where st_age>=30
--再继续利用,给他加上排序
select * from studio where cl_ID=1
union all
select * from studio where ho_ID=1
union all
select * from studio where st_age>=30
order by st_ID
--===========连接查询==================
--连接查询,功能 - 将多个表中的数据查询出来放在一起
--内连接:使用比较运算符=><....等进行表间某些数据库的比较 *** 作,并列出这些表中与连接条件相匹配的数据行
--等值连接,当然就是用等号了,毛病,这也要问
select * from studio inner join class on studio.cl_ID = class.cl_ID
--指明要查询的列(江湖上又称自然连接),并排序
select st_ID as '编号',st_name as '学生姓名',cl_class as '班级名称' from studio inner join class on studio.cl_ID = class.cl_ID order by st_ID
--使用表别名
select st.st_name as '学生姓名',st.cl_ID as '班级编号',cl.cl_class as '班级名称' from studio as st inner join class as cl on st.cl_ID = cl.cl_ID
--不等连接,这个问题很好笑,既然使用等号的是等值连接,那么不等值你说是不是应该是非等于以外的呢?
--下面我们再连接第三个表,看下是怎么搞滴
select st.st_name as '学生姓名',cl.cl_class as '班级名称',ho.ho_Coding as '所在宿舍编号'
from studio as st inner join class as cl
on st.cl_ID = cl.cl_ID
inner join hostel as ho
on st.ho_ID=ho.ho_ID
--我们再给他加个条件看下
--where st.cl_ID>2
--再给他个排序
--order by st.st_ID
--外连接:
--与内连接不同的是,内连接至少要有一个同属于两个表的行符合连接条件时才会返回行,外连接会返回符合任意条件的行
--他的表有主从之分,他用主表中的每行去匹配从表中的,与内连不同的是,他不会丢弃没有匹配的行,而是填充null给从结果集
--左外连接
select st.st_ID as '学生编号',st.st_name as '学生姓名',cl.cl_ID as '班级编号',cl_class as '班级名称'
from studio as st left 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_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 left outer join class as cl
on tka.cl_ID=cl.cl_ID
left outer join
course as co
on tka.co_ID=co.co_ID
left outer join
teacher as te
on tka.te_ID=te.te_ID
--====================右外连结 =======================
select st.st_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 '课程安排编号'
,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 '学生编号',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 '课程安排编号'
,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('苏州',z_ID) values('无锡',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,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
--====学 云网-天轰穿-[url]www.ixueyun.com[/url]==子查询============
--在一个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,该关键字引入一个子查询的时候基本上是对数据进行一次是否存在的测试
--我们查询那些人所在的班级是编号为 1 的
select * from studio where exists(select cl_ID from class where studio.cl_ID=class.cl_ID and class.cl_ID=1)
--使用 not exists
select * from studio where not exists(select * from class where studio.cl_ID=class.cl_ID and class.cl_ID=1) order by st_ID
--基于查询生成新的表
select st_name into class_3 from studio where cl_ID=3
--将数据批量插入一个表中
insert into class_3 select st_name from studio where cl_ID=4
-----------------------sql 编程--------------
declare @max int;
--申明一个变量@max
set @max=1;
--为变量@max赋值
while @max<10
--如果@max小于10就进入循环
begin
set @max=@max+1;--每次循环就给@max加1
print @max;
--打印当前@max的值
end
print '终于循环完了'; 总结

以上是内存溢出为你收集整理的Mysql必读常用的SQL例句 数据库开发所需知识全部内容,希望文章能够帮你解决Mysql必读常用的SQL例句 数据库开发所需知识所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1162355.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存