jdbc学习笔记4
来源:互联网 发布:上海人工智能产业园区 编辑:程序博客网 时间:2024/06/13 08:08
在java代码下操作mysql 数据库的常见操作
//————————-以上就是 增加 修改 删除 表记录 相关语句 ,(DML)———————————————————–
//———————————————————————————————————————————-
DQL语句(DML) 查询语句. (必须掌握)
语法:
SELECT selection_list /*要查询的列名称*/ FROM table_list /*要查询的表名称*/ WHERE condition /*行条件*/ GROUP BY grouping_columns /*对结果分组*/ HAVING condition /*分组后的行条件*/ ORDER BY sorting_columns /*对结果排序*/ LIMIT offset_start, row_count /*结果限定*/
1.1 查询所有行所有列
select * from stu;*号 是通配符.通配所有列. 上面语句与下面是一模一样的 select sid,sname,age,gender from stu;谁的效率更高? 下面的效率更高. *需要运算.
1.2 查询所有行指定列
select sname from stu;
2.1 条件查询介绍
条件查询就是在查询时给出WHERE子句,在WHERE子句中可以使用如下运算符及关键字:
? =、!=、<>、<、<=、>、>=;? BETWEEN…AND;? IN(SET)/NOT IN(SET)? IS NULL/IS NOT NULL//---条件连接符? AND; &&? OR; ||? NOT; !
2.2 查询性别为女,并且年龄小于50的记录
select * from stu where gender='female' and age<50;
2.3 查询学号为S_1001,或者姓名为liSi的记录
select * from stu where sid='S_1001' or sname='liSi';数据库中,sql语句不区分大小写 ,但是 数据区分大小写.
2.4 查询学号为S_1001,S_1002,S_1003的记录
select * from stu where sid='S_1001' or sid='S_1002' or sid='S_1003';select * from stu where sid in('S_1001','S_1002','S_1003');
2.5 查询学号不是S_1001,S_1002,S_1003的记录
select * from stu where not (sid='S_1001' or sid='S_1002' or sid='S_1003');select * from stu where sid not in('S_1001','S_1002','S_1003');
2.6 查询年龄为null的记录
select * from stu where age=null;null的特性: null不等于null 所以判断时应如下写法:select * from stu where age is null;
2.7 查询年龄在20到40之间的学生记录
select * from stu where age >= 20 and age <= 40;
select * from stu where age between 20 and 40;
2.8查询性别非男的学生记录
select * from stu where gender!= 'male';select * from stu where not gender='male';select * from stu where gender not in ('male');
2.9 查询姓名不为null的学生记录
select * from stu where sname is not null;select * from stu where not sname is null;
//————————————————————————————————–
where 字段 like ‘表达式’;
% => 通配 通配任意个字符.
_ => 通配 通配单个字符.
说明: LIKE 条件后 根模糊查询表达式, “_”==> 代表一个任意字符
3.1查询姓名由5个字母构成的学生记录
select * from stu where sname like '_____';
3.2查询姓名由5个字母构成,并且第5个字母为“i”的学生记录
select * from stu where sname like '____i';
3.3 查询姓名以“z”开头的学生记录
说明: "%"该通配符匹配任意长度的字符.select * from stu where sname like 'z%';
3.4查询姓名中第2个字母为“i”的学生记录
select * from stu where sname like '_i%';
3.5 查询姓名中包含“a”字母的学生记录
select * from stu where sname like '%a%';
//—————————————————————–
4.1 去除重复记录
关键词: distinct => 去除重复查询结果记录.select gender from stu; ==> 出现大量重复的记录select distinct gender from stu; =>去除重复的记录
4.2查看雇员的月薪与佣金之和
select sal*12+comm from emp; null与任何数字计算结果都是null.上面的写法是错误的. 使用IFNULL(参数1,参数2) 函数解决. 判断参数1的值是否为null,如果为null返回参数2的值. select sal*12 + IFNULL(comm,0) from emp; *这个函数在所有数据库通用吗? 不通用.
4.3 给列名添加别名
select sal*12 + IFNULL(comm,0) as '年收入' from emp;** select sal*12 + IFNULL(comm,0) '年收入' from emp;select sal*12 + IFNULL(comm,0) 年收入 from emp;
//——————————————————————————————————————————
5.1 查询所有学生记录,按年龄升序排序
asc: 升序
desc:降序
select * from stu order by age asc;默认就是升序select * from stu order by age;
5.2 查询所有学生记录,按年龄降序排序
select * from stu order by age desc;
5.3 查询所有雇员,按月薪降序排序,如果月薪相同时,按编号升序排序
select * from emp order by sal desc , empno asc;
聚合函数
聚合函数是用来做纵向运算的函数:
? COUNT():统计指定列不为NULL的记录行数;? MAX():计算指定列的最大值,如果指定列是字符串类型,那么使用字符串排序运算;? MIN():计算指定列的最小值,如果指定列是字符串类型,那么使用字符串排序运算;? SUM():计算指定列的数值和,如果指定列类型不是数值类型,那么计算结果为0;? AVG():计算指定列的平均值,如果指定列类型不是数值类型,那么计算结果为0;
6.1 COUNT
当需要纵向统计时可以使用COUNT()。
1>查询emp表中记录数: select count(*) from emp;2>查询emp表中有佣金的人数: select count(*) from emp where comm is not null and comm >0;3>查询emp表中月薪大于2500的人数: select count(*) from emp where sal > 2500;4>统计月薪与佣金之和大于2500元的人数: select count(*) from emp where sal+IFNULL(comm,0) > 2500;5>查询有佣金的人数并且有领导的人数: select count(*) from emp where comm > 0 and mgr is not null;
6.2 SUM(计算总和)和AVG(计算平均值)
当需要纵向求和时使用sum()函数。1>查询所有雇员月薪和: select sum(sal) from emp;2>查询所有雇员月薪和,以及所有雇员佣金和: select sum(sal),sum(comm) from emp;3>查询所有雇员月薪+佣金和: select sum(sal+IFNULL(comm,0)) from emp;4>统计所有员工平均工资: select avg(sal) from emp;
6.3 MAX和MIN
查询最高工资和最低工资: select max(sal),min(sal) from emp;
//—————————————————————————————————————————————
- 分组查询
当需要分组查询时需要使用GROUP BY子句,例如查询每个部门的工资和,这说明要使用部分来分组。
1>查询每个部门的部门编号和每个部门的工资和: select deptno,sum(sal) from emp group by deptno;2>查询每个部门的部门编号以及每个部门的人数: select deptno,count(ename) from emp group by deptno;3>查询每个部门的部门编号以及每个部门工资大于1500的人数: select deptno,count(ename) from emp where sal>1500 group by deptno ;
HAVING子句
4>查询工资总和大于9000的部门编号以及工资和:
select deptno,sum(sal) from emp group by deptno having sum(sal)>9000;使用having在分组之后加条件.where和having都可以加条件? 1.where在分组之前加条件. 2.having在分组之后加条件. where的效率要远远高于having. 分组本身消耗资源非常大.
//———————————————–以上是查询———————————————————————————
//———————————————-以下是分页相关知识—————————————————————————————
LIMIT(MySQL方言) (必须掌握)
———————- LIMIT用来限定查询结果的起始行,以及总行数。
1>查询5行记录,起始行从0开始
select * from emp limit 0,5;
2> 查询10行记录,起始行从3开始
select * from emp limit 3,10;
3>如果一页记录为5条,希望查看第3页记录应该怎么查呢?
第一页记录起始行为0,一共查询5行; select * from emp limit 0,5;第二页记录起始行为5,一共查询5行; select * from emp limit 5,5;第三页记录起始行为10,一共查询5行; select * from emp limit 10,5;
//—————————————————————————————————————————————-
- jdbc学习笔记4
- Spring学习笔记4-JDBC
- Java学习笔记4-JDBC数据库连接池
- JDBC学习笔记(二)
- JDBC学习笔记(二)
- JDBC学习笔记(三)
- JDBC学习笔记(四)
- JDBC学习笔记(六)
- JDBC学习笔记
- JDBC学习笔记
- JDBC学习笔记1
- jdbc学习笔记2
- jdbc学习笔记
- JDBC学习笔记
- jdbc学习笔记(1)
- jdbc学习笔记(2)
- jdbc学习笔记(3)
- JDBC学习笔记
- javascript的模块化编程
- java中的四种引用类型
- jdbc学习笔记3
- 2017实习心得体会以及自己的小目标
- JAVA学习笔记-public、protected、private、abstract、static和final
- jdbc学习笔记4
- C++中int main(int argc, char *argv[])解释
- 第五十一讲 Spark优化之“钨丝计划”
- 关于前端性能优化的小想法
- JavaScript中数组slice和splice的对比小结
- 《Docker……》笔记-11 附录A常见问题汇总
- C# 设计模式巩固笔记
- STM32F4、F2系列拥有两个片上USB-OTG的应用问题
- 构建maven的SSH项目