1、sql是一种数据库语言,而mysql是DBMS数据库管理系统;
2、SQL用于访问,更新和操作数据库中的数据,而mysql允许保持数据库中存在的数据;
3、sql的语言是固定的,而mysql可以获得频繁的更新。
select 字段名 from 表名 where 字段名 in(具体内容)
select 字段名 from 表名 where 字段名 between 数值1 and 数值2Eg:查询当字段Salary范围在3000~5000时,字段Name和Salary的内容。select Name,Salary from t_emp where Salary between 3000 and 5000
select 字段名 from 表名 where 字段名 like '模糊条件'模糊条件中”%”与”_”区别:“%a”:无论字符a前面有多少字符 ;”_a”:字符a前面只有一个字符。Eg:查询所有Name以字母,C为第二个字符,员工的Name和Salary的内容。select Name,Salary from t_emp where Name like '_C%'
select 字段名 from 表名 where 字段名 is null
Eg:返回数据表中字段Name不重复的内容。select distinct Name from t_emp
Select 字段名 from 表名 where 表达式1 and/or 表达式2
–查询10号部门,工资大于1000的员工信息
select * from emp where deptno=10 and sal>1000;
–查询10号部门,或者工资小于2000的员工信息
select * from emp where deptno=10 or sal<2000;
in后面跟一个集合或者子查询
oracle中表示一个简单的集合 ,(元素,元素,… 元素) 例如 (1,2,3,4,5) (‘a’,‘b’,‘c’,‘d’)
–查询emp表中,工资是800,1600,或者3000的员工信息
select * from emp where sal in(800,1600,3000);
select 字段名 from 表名 group by 待分组的字段名
Select 字段名 from 表名 where 条件 order by 待排序字段名 asc/descasc:升序(默认值可省略) desc:降序Eg:查询class_id为1的所有信息,以score降序的方式显示结果。select * from tb_score where class_id = 1 order by score desc
Select 字段名 from 表名 limit 偏移值 记录个数Eg:按成绩降序后查询班级中第2名到第5名的学生信息。select * from tb_score group by score desc limit 1,4注:偏移值默认为0,可不写,1代表从第一个数开始取,4代表共记录4个结果
count(*):表示计算总行数,括号中写星与列名,结果相同
例1.查询登录系统学总数
select count(*) from StudentBindPaperTypeEntity
–查询员工表中的员工人数
select count(empno) from emp;
select count(*) from emp;
max(列):求此列的最大值
例2.求下表的最大编号
select max(StudentID) from StudentBindPaperTypeEntity
–查询10号部门的最高工资
select max(sal) from emp where deptno=10;
min(列):求此列的最小值
例3.求下表编号最小编号
select min(StudentID) from StudentBindPaperTypeEntity
–查询最低工资
select min(sal) from emp;
sum(列):求此列之和(注:sum运算符与数字类型连用)
例4.查询当前在线的学生(IsUse=0表示未在线,1表示在线)
select SUM(IsUse) from StudentBindPaperTypeEntity
–查询工资总和
select sum(sal) from emp;
avg(列) :表示求此列的平均值(注:avg运算符与数字类型连用)
例5:查询学生编号的平均数
select avg(StudentID) from StudentBindPaperTypeEntity
–查询员工的平均工资
select avg(sal) from emp;
例1.查询学号>18832650890的学生
select * from StudentBindPaperTypeEntity where StudentID>18832650890
例2.查询学号!=18832650890的学生(<>同效)
select * from StudentBindPaperTypeEntity where StudentID!=18832650890
%表示任意多个字符
例1.查询1月8号考试的学生
select * from StudentBindPaperTypeEntity where TimeTamp like ‘2020-01-08%’
例2.查询不是1月8号考试的学生
select * from StudentBindPaperTypeEntity where TimeTamp not like ‘2020-01-08%’
in关键字为非连续查询
例1.查询两个不相邻的学号的学生
select * from StudentBindPaperTypeEntity where StudentID in(‘19100142001’,‘19100142006’)
Between…and…为连续查询(注:sql软件情况不一样,可能不包含and后的值)
例2.查询两个学号之间的学生
select * from StudentBindPaperTypeEntity where StudentID Between 19100142001 and 19100142006
is null判断为空
例1.查询没有试卷的学生
select * from StudentBindPaperTypeEntity where PaperType is null
is not null 判断非空
例2.查询有试卷的学生
select * from StudentBindPaperTypeEntity where PaperType is not null
优先级由高到低的顺序为:
小括号,not,比较运算符,逻辑运算符
and比or先运算,如果同时出现并希望先算or,需要结合()使用
作用:将字段间一对多的关系,向一的方向靠拢分组
例1.查出参加考试有几个学院
select CollegeID from StudentBindPaperTypeEntity group by CollegeID
例2.查出各个学院参加考试的人数
select CollegeID, count(StudentID) from StudentBindPaperTypeEntity group by CollegeID
–查询各个部门的部门编号和部门的平均工资
select deptno,avg(sal) from emp group by deptno;
–查询各个部门的员工人数
select deptno,count(empno) from emp group by deptno;
where和having的异同:
where:条件,where后面跟的条件比having后的条件先执行,条件中不允许使用聚合函数。
having:条件中可以使用聚合函数,一般having和group by联用。
group by+having:having的作用跟where子句功能一样,只不过having只用在group by
例3.查出学院ID大于10的学院
select CollegeID from StudentBindPaperTypeEntity group by CollegeID having CollegeID>10
排序查询语法:
select * from 表名 order by 列1 asc|desc [,列2 asc|desc,…]
如果列1的值相同,则按照列2排序,以此类推。
1.asc从小到大(升序)
2.desc从大到小(降序)
例1.根据学院分组ID降序(desc)
select CollegeID from StudentBindPaperTypeEntity group by CollegeID order by CollegeID desc
例2.将上表升序(asc)
select CollegeID from StudentBindPaperTypeEntity group by CollegeID order by CollegeID asc
–查询员工信息,按照部门编号升序排序,如果部门编号相同时,按照工资的升序排序。
select * from emp order by deptno asc,sal asc;
连接查询:
1.内连接:Inner join
2.左连接:Left join
3.右连接:Right join
左外连接:左表的值会全部显示出来,右表的值显示on条件搜索的的结果,搜索不到为NULL。
select score.studentID,score.score,s.CollegeID,s.major,s.majorClass
from StudentInfoEntity as s left join ScoreEntity as score on s.studentID=score.studentID
右外连接与左外连接相反(右表的值全部显示出来)。
select score.studentID,score.score,s.CollegeID,s.major,s.majorClass
from ScoreEntity as score right join StudentInfoEntity as s on s.studentID=score.studentID
表示查询得到的结果如果有重复,就删掉。
select distinct sex from student;
后面都跟一个集合或者子查询
all:表示大于集合中最大的元素 >all(1,2,3,4,5) 等价于 >5
any:表示大于集合中最小的元素 >any(1,2,3,4,5) 等价于 >1 –查询员工信息,要求员工的工资比以下的值都低 1600,2000,3000 –查询比下任意一个(1600,2000,3000)工资高的员工信息 –查询工资比所有人(1600,2000,3000)都高的员工信息 Create table Student( insert into Student(Sid,Sname,Sage,Ssex) values (‘1’,‘张三’,‘20’,‘女’); select * from Student where Sname=‘张三’ update Student set Sname=‘李四’ where Sid=‘2’ delete from Student where Sname=‘张三’ select teacher.Tname from teacher select a.Sname,a.Sage,a.Ssex,b.score from Student a, SC b select a.Sid,a.Sname 3306,Windows通过更改my.ini配置更改端口,linux通过更改my.conf来更改端口。 mysql索引的建立大大的提高了mysql的检索速度,但是对数据的update,insert,delete的效率就有所降低。
select * from emp where sal
select * from emp where sal>any(1000,2000,3000);
select * from emp where sal > all(1600,2000,3000);MySQL常见命令:
(1)创建学生表 Student :
Sid varchar(10) not null primary key,
Sname varchar(10) not null,
Sage int(2) not null,
Ssex varchar(2),
)(2)插入数据:
(3)查询名字为张三的学生表数据:
(4)修改编号2的学生名字为李四:
(5) 删除名字为张三的学生:
(6)查询语文老师的名字:用inner join联查两个表
inner join course
where teacher.Tid = course.Tid and course.Tname = ‘语文’(7)统计语文成绩大于70的学生信息:
where a.Sid=b.Sid and b.Cid=1 and b.score >70(8)统计各科分数大于80的人:
from student a,
(select Sid from sc
group by Sid
having min(score) > 80) as b
where b.Sid = a.Sid;2、mysql默认端口为:
3、mysql索引: