SELECT
select的完整语法:
selectcol1, col2,... # 业务查询的字段fromtable_name # 选取的哪张表[where single_conditions]# single_conditions条件表达式,个体约束(条件)[[group by column_name1]# column_name1以哪个字段名分组[having group_conditions]] # group_conditionds条件表达式,分组约束[order by column_name2]# column_name2以哪个字段进行排序[limit N,M] # 执行完之后,跳过N条记录,选取M条记录
上述如果都有:执行顺序from->where->group by->having->order by->limit->select
列的结果显示
1、去掉重复的数据:distinct(针对于记录而言,不是针对于列的数据而言)
# 查看员工的职位selecttitlefroms_emp;select distincttitlefroms_emp;
# 每个部门下有哪些职位selectdept_id,titlefroms_emp;select distinctdept_id,titlefrom s_emp;# 联合唯一
2、运算符:+、-、*、/、%(只列举一个)
# 查看员工年薪select salary*12
froms_emp;select id,last_name,salary*12 asyear_salaryfroms_emp;
# 查看员工当前年薪以及月薪提高100美元后的年薪select id,last_name,salary*12 old_year_salary,(salary+100)*12 asnew_year_salaryfrom s_emp;
3、列与列的数据拼接:concat(column_name1, "拼接符", column_name2[, "拼接符"])
# 查看员工基本信息(编号,全名,工资)select id,concat(first_name,' ',last_name) asname,salaryfrom s_emp;
4、将null值转换为特定值:ifnull(column_name, 特定值)
# 查看员工工资(没有工资的显示为0)select id,last_name,ifnull(salary,0.00) assalaryfrom s_emp;
where conditions
MySQL的运算符概念及作用:
主要是根据conditions的条件查询结果集
1、比较运算符:=、、>=、<=、!=、<=>(同is null)、<>(同!=)
# 查看拥有白领工资的员工有哪些(1200,2000)selectid,last_name,salaryfroms_empwhere salary>1200 and salary<2000;
# 查看有工资的员工信息selectid,last_name,salaryfroms_empwhere salary is not null;selectid,last_name,salaryfroms_empwhere salary <> null;# erro
# 查看没有工资的员工的信息selectid,last_name,salaryfroms_empwhere salary is null;selectid,last_name,salaryfroms_empwhere salary <=> null;selectid,last_name,salaryfroms_empwhere salary != 1200;# null数据未取到
2、逻辑运算符:and(&&)、or(||)、not
查看41号部门的员工信息并且工资大于1200或者43号部门工资小于2000的员工信息selectid,last_name,salaryfroms_empwhere (dept_id=41 and salary>1200) or (dept_id=43 and salary<2000);
3、在XXX区间:between ... and ... 不在XXX区间:not between ... and ...
查看拥有白领工资的员工有哪些(1200,2000)select id,last_name,salary from s_emp where salary>1200 and salary<2000;
# 下面是闭区间selectid,last_name,salaryfroms_empwhere salary between 1199 and 2001; #[1199,2001]# 不在区间内selectid,last_name,salaryfroms_empwhere salary not between 1200 and 2000; #(-&,1200)or(2000,+&)
4、在集合中: in () 不在集合中:not in ()
# 查看41,42,43号部门的员工有哪些selectid,last_name,salaryfroms_empwhere dept_id=41 and dept_id=42 and dept_id=43;
# 和上面的等价selectid,last_name,salaryfroms_empwhere dept_id in(41,42,43);
# 查看不是41,42,43号部门的员工有哪些selectid,last_name,salaryfroms_empwhere dept_id!=41 and dept_id!=42 and dept_id!=43;
# 和上面的等价selectid,last_name,salaryfroms_empwhere dept_id not in(41,42,43)
6、模糊匹配:like :%:0到多个字符匹配;_:1个字符匹配;[ ]:范围内的匹配的单个字符;[^ ]:范围外的匹配的单个字符; 不模糊匹配: not
# 查看职位以VP开头的员工有哪些selectid,last_name,salaryfroms_empwhere title like 'VP%';selectid,last_name,titlefroms_empwhere title not like 'VP%';
# 查看员工信息,名字以C开头,并且字符数不小于5个字符selectid,last_namefroms_empwhere last_name like 'C____%';
#查看客户信息,客户名称中包含单引号的客户select id,name froms_cutomerwhere name like "%'%";
group by column_name
涉及的组函数:计数count()、最小值min()、最大值max()、平均值avg()、总和sum()
# 查看员工总数select count(*) ascount_numfroms_emp; # 默认分组(以表格为单元)select count(id) ascount_numfroms_emp;
# 统计有工资的员工个数select count(salary) ascount_numfroms_emp;
# 查看每个部门的员工个数select dept_id,count(*) asnumsfroms_empgroup bydept_id;
# 进行分组后,select的结果只能是组的概念,不允许出现个体概念(last_name)select dept_id,count(*) asnums,last_namefroms_empgroup bydept_id; # errro
# 默认以逗号拼接 group_concat(),这个函数很重要select dept_id,count(*) asnums,group_concat(last_name)froms_empgroup bydept_id;
# 查看每个部门薪资大于1200的员工总数(信息)select dept_id,count(*) nums,group_concat(last_name),group_concat(salary)froms_empwhere salary > 1200
group bydept_id;
# 查看部门平均薪资select avg(salary)froms_empgroup bydept_id;
# 查看部门平均薪资>2000员工总数select dept_id,count(*) nums,avg(salary)froms_empgroup bydept_idhaving avg(salary)>2000;
# 查看每个部门员工总数,部门平均薪资大于1000,并且每个员工的薪资>900
select dept_id,count(*),avg(salary)froms_empwhere salary >900
group bydept_idhaving avg(salary)>1000
排序order by:升序ASC;逆序DESC
#查看员工的员工ID,名字,月薪,部门ID,部门ID进行升序排序,相同部门的员工在一起按照薪资从高到低排序
select id,last_name,dept_id,salaryfroms_emp
order by dept_id asc,salary desc;#如果进行排序的时候,需要对字段先进行转码,后排序
select id,last_name,dept_id,salaryfroms_emp
order by conver(dept_id asc,salary using gbk2312) desc;
限制记录数目:limit N,M 跳过N条记录,查询M条记录
# 跳过3条记录,查询5条记录,这一般用于分页比较合理
# 擦昏地当前页数和记录数selectid,last_name,dept_id,salaryfroms_emporder by dept_id asc,salary desclimit3, 5# 当数据表的记录上万条,比如超过1万条记录
# 建议使用子查询进行分页,这样效率高点,因为子查询是在索引文件(所以你文件比较小,数据文件比较大,处理上就比较慢)上执行的selectid, last_name, dept_id, salaryfroms_empwhere id >=(selectidfroms_emporder byid
limit10000, 1)
limit10;