/*----查找------ 1.select 需要查找的字段(用逗号隔开) from 表名 (where 条件); 2.*号,代表所有字段 (一).where 后加筛选条件; 1.关系运算符 < 小于 = 等于 > 大于 <= 小于等于 >= 大于等于 != 不等于1 <> 不等于2 2.逻辑运算符 and 并且 or 或者
3.加在where后的关键字 -- 字段名 between ...and... (介于两者之间,包含首尾) -- 字段名 in 包含 例:... where id in(1,3,9); -- 字段名 is null 找某个字段中为空的记录 4.模糊查找 -- 使用like关键字,需要配合占位符使用 -- name like "_张%"; -- _ 代表一位字符 -- % 代表任意位字符
8.分组聚合查询 -- group by 与聚合函数联合使用,代码运行过程是:通过group by把数据按某个字段分成各组,然后聚合函数对每个组起作用
9.用As关键字为表格或字段起别名,As可以省略;起别名以后,之后的代码都要用别名,原名不在起作用
/*(二).多表查询 select * from student,class where student.c_id = class.id; -- 多个表格之间用逗号隔开 -- 多表查询,只能查找两个表格之间有关联的部分
(三).连表查询 -- 内连接 join ... on ... -- 内连接与多表查询的效果相同 select * from student join class on student.c_id = class.id;
-- 外连接 left/right join ... on ... 1.左外连接(可显示出join左边表格的非公共部分) select * from student left join class on student.c_id = class.id; 2.右外链接(可显示出join右边表格的非公共部分) select * from student right join class on student.c_id = class.id; */
练习:
select name,age,id,sex from student;
select * from student where age>18 order by age desc limit 3,2;
select * from student where age<20 and sex="男";
select * from student where name like "_张%";
select * from student where sex between "男" and "男";#找出所有男性
select avg(age) as leveAge from student;
select max(age) as maxAge from student;
select min(age) as maxAge from student;
select techer,count(*) from student,class where student.c_id = class.id group by class.techer;
select class,count(*) from student,class where student.c_id = class.id group by class.class;
- group by 与聚合函数联合使用,代码运行过程是:通过group by把数据按某个字段分成各组,然后聚合函数。-- limit (页码-1)*每页的大小,每页的大小;(此写法实现分页查找)-- 字段名 in 包含 例:... where id in(1,3,9);-- count(字段名) 获取某个字段下的记录数 (不统计值为null的记录)-- 字段名 between ...and... (介于两者之间,包含首尾)-- limit a,b a是起始索引值(从0开始);