基本查询语句

单表查询

东西太多了,要慢慢消化。各种order by、group by、having和函数等尤其以group by最不好理解。不过在学习过程中,加深了对数据查询的了解,在今后工作中定然不负现在付出。

聚合函数查询

2019-04-12学习完成

连接查询

2019-04-12学习部分,有点复杂,光看书,理解起来有点恼火,最多作为了解来学习。

子查询

2019-04-14学习完成

合并查询结果

2019-04-14学习完成

为表和字段取别名

2019-04-14学习完成

使用正则表达式查询

2019-04-14学习完成

案例

2019-04-14学习完成

经典习题

2019-04-14学习完成
#(1) 计算所有女员工(‘F’) 的年龄。(没有年龄,查询的是工作时间多少年)
select e_gender, year(now()) - year(hireDate) age from employee where e_gender=’f’;
#(2) 使用LIMIT查询从第3条记录开始到第6条记录。
select from employee limit 3,6;
#(3) 查询销售人员(SALSEMAN) 的最低工资。
select e_name,min(e_salary) from employee where e_job = ‘SALESMAN’;
#(4) 查询名字以字母N或者S结尾的记录。
select e_name from employee where e_name regexp ‘[ns]$’;
#(5) 查询在Beijing工作的员工的姓名和职务。
# 待优化空格
select e_name,e_job from employee where dept_no = (select d_no from dept where d_location = ‘BeiJing ‘);
#(6) 使用左连接方式查询employee和dept表。
select
from employee left join dept on employee.dept_no = dept.d_no;
#(7) 查询所有2001~2005年入职的员工的信息, 查询部门编号为20和30的员工信息并使用UNION合并两个查询结果。
select from employee where year(hireDate) >=2001 and year(hireDate) <= 2005 union select from employee where dept_no in (20,30);
#(8) 使用LIKE查询员工姓名中包含字母a的记录。
select e_name from employee where e_name like ‘%a%’;
#(9) 使用REGEXP查询员工姓名中包含T、 C或者M 3个字母中任意1个的记录。
select e_name from employee where e_name regexp ‘[tcm]’;