上篇结束了java基础,本篇主要对Mysql中的一些常用的方法进行了总结,主要对查询方法进行了讲解,包括重要的多表查询用到的内连接和外连接等,以下代码可以直接复制到可视化软件中,方便阅读以及练习;
SELECT *FROM employees;
SELECT employee_id,salary*(1+IFNULL(commission_pct,0))*12 "年工资"FROM employees;
#着重号:表名和关键字重合了需要用:
SELECT *FROM `order`;
#查询常数:会自动在前面加一列:
SELECT "hello",123,employee_id FROM employees;
#显示表结构:表的信息:
DESCRIBE employees;
#去重复:distinct:
SELECT job_id FROM employees;
SELECT DISTINCT job_id FROM employees;
#where条件查询:一定要在from结构的后面:
SELECT *FROM employees WHERE last_name='kING';
SELECT *FROM employees WHERE department_id=90;
#运算符:此处判断是否等于不用两个==,一个=即可;
SELECT employee_id FROM employees WHERE employee_id %2=0;
SELECT 1=0,1!=0;
#安全等于<=>,专门为null而使用;以下是例子:首先null参与比较的话结果全是null,无论是1=null还是1>null结果都是null;如果用安全等于,就会结果正常值;
SELECT 1=NULL;
SELECT 1<=>NULL;
SELECT employee_id,salary,commission_pct FROM employees WHERE commission_pct=NULL;
SELECT employee_id,salary,commission_pct FROM employees WHERE commission_pct<=>NULL;
#运算符:isnull is not null isnull()这个是一个方法;
SELECT employee_id,salary,commission_pct FROM employees WHERE commission_pct IS NULL;
SELECT employee_id,salary,commission_pct FROM employees WHERE isnull(commission_pct);
SELECT employee_id,salary,commission_pct FROM employees WHERE commission_pct IS NOT NULL;
#between and:
SELECT employee_id,salary FROM employees WHERE salary BETWEEN 6000 AND 8000;
#in not in:例如查询部门号为10,20,30的员工信息:
SELECT employee_id,salary,department_id FROM employees WHERE department_id=10 OR department_id=20 OR department_id=30;
SELECT employee_id,salary,department_id FROM employees WHERE department_id IN(10,20,30);
#LIKE 模糊查询:
#查询last_name中包含字符串‘a’的员工信息:注意 % 代表前面后面有不确定个字符;例如以a开头:a%;以a结尾:%a;下划线代表一个不确定的字符,例如查询lastname第二个字符是a的员工信息可以用:_a%;
SELECT employee_id,salary,last_name FROM employees WHERE last_name LIKE '%a%';
#排序order by 默认是从低到高;从高到低是用ASC;
SELECT employee_id,last_name,salary FROM employees order BY salary DESC;
#此处计算出来了年薪然后起别名annual_salary;
#一定要注意,列的别名只能在order by中使用,不能在where中使用;
SELECT employee_id,last_name,salary,salary*12 annual_salary FROM employees order BY annual_salary DESC;
#二级排序:按照department_id升序排序,此时可能有想同的,那对于相同的按照salary降序排序:一样的三级排序就是在后面在加上一部分
SELECT employee_id,last_name,salary FROM employees order BY employee_id ASC,salary DESC;
#limit实现分页显示:在后面加上即可;例如查询32、33这两条数据:limit后面第一个参数是第几个的前一个,然后第二个参数代表每几个为一页;
SELECT *FROM employees LIMIT 31,2;
#多表查询:分为等值连接和非等值连接;自连接和非自连接;内连接和外连接三种类型;
#例如想查询一个数据,需要先从一个表中找到对应信息,然后根据这个对应信息再去查询另一个表中查询重要信息,
#然后根据这个重要信息再去查找另一个表最终得到数据;因此,这种方法非常麻烦,所以想将表进行连接,多表查询;
#以下department_name在departments表中,employee_id在employees表中,然后共同的属性是departent_id,但是
#这样查询是错误的,得到的是笛卡尔积,缺少了多表的连接条件;
SELECT employee_id,department_name FROM employees,departments;
#正确方式:此处多查询一下department_id,这两个表中都有这个属性,查询的时候要指明是哪个表里的;
SELECT employee_id,department_name,employees.department_id
FROM employees,departments
WHERE employees.department_id=departments.department_id;
#三表连接:查询employee_id,last_name,department_name,city;
SELECT employee_id,last_name,department_name,city
from employees,departments,locations
WHERE employees.department_id=departments.department_id AND locations.location_id=departments.location_id
#非等值连接:上面例子是等值连接,下面是非等值连接:
SELECT last_name,salary,grade_level
FROM employees,job_grades
WHERE salary BETWEEN lowest_sal AND highest_sal;
#自连接和非自连接:
#想查询员工id和姓名,以及其管理者的id和姓名,这两个都在一张表中,要自己和自己连接:这时必须要起别名;
#!!!注意,如果对表起别名后,select和where语句必须要使用别名,不能使用原来的表名;
SELECT emp.employee_id,emp.last_name,manager.employee_id,manager.last_name
FROM employees emp,employees manager
WHERE emp.manager_id=manager.employee_id;
#!!!内连接和外连接:
#内连接:合并具有同一列的两个以上的表的行,结果集中不包含一个表与另一个表不匹配的行;上面那种就是内连接;
#外连接:有的员工可能没有部门,也有的部门建好了,但是还没有员工,此时想把这些数据也查出来;
#上面的多表查询都是都是内连接,并且用的都是SQL92语法的,最好是要用SQL99语法:举例:三表连接:
SELECT employee_id,last_name,department_name,city
FROM employees JOIN departments
ON employees.department_id=departments.department_id
JOIN locations
ON departments.location_id=locations.location_id;
#外连接分为左外连接,右外连接,满外连接;
#左外连接:两个表在连接过程中除了返回满足连接条件的行以外还返回左表中不满足条件的行;右外连接就是返回了右表中不满足条件的行;
#练习:查询所有的员工的employee_id,last_name,department_name;!!!注意是所有的,说明就是要用外连接;
#Mysql不支持SQl92实现外连接,只能用SQL99语法:
#左外连接:也就是练习答案:
SELECT employee_id,department_name
FROM employees LEFT OUTER JOIN departments#此处要用左外连接,可以看到就查询到了除了符合的,也查询到了没有部门的那个员工;
ON employees.department_id=departments.department_id;
#右外连接:
SELECT employee_id,department_name
FROM employees RIGHT OUTER JOIN departments#查询到了没有人员的部门
ON employees.department_id=departments.department_id;
#满外连接:sql不支持ful,因此需要重新实现:
#SELECT employee_id,department_name
#FROM employees FULL JOIN departments
#ON employees.department_id=departments.department_id;
#引入UNION,返回两个查询结果集的并集,去除重复记录(中间交集部分);UNIONALL效率更高,没有去重,也就是中间交集部分加了两遍;
下面按照本图进行外连接查询的练习:
#左中图(左外连接去除交集部分):参考word文档,
SELECT employee_id,department_name
FROM employees LEFT JOIN departments
ON employees.department_id=departments.department_id
WHERE departments.department_id IS NULL;#右边没有就用右表null;
#右中图(右外连接去除交集部分):
SELECT employee_id,department_name
FROM employees RIGHT JOIN departments
ON employees.department_id=departments.department_id
WHERE employees.department_id IS NULL;#左边没有就用左表null;
#左下图:满外连接:左上图(左外连接) UNION ALL 右中图(去除交集的右外连接):
SELECT employee_id,department_name
FROM employees LEFT OUTER JOIN departments
ON employees.department_id=departments.department_id
UNION ALL
SELECT employee_id,department_name
FROM employees RIGHT JOIN departments
ON employees.department_id=departments.department_id
WHERE employees.department_id IS NULL;
#右下图(右中图UNION ALL左中图)
SELECT employee_id,department_name
FROM employees LEFT JOIN departments
ON employees.department_id=departments.department_id
WHERE departments.department_id IS NULL
UNION ALL
SELECT employee_id,department_name
FROM employees RIGHT JOIN departments
ON employees.department_id=departments.department_id
WHERE employees.department_id IS NULL;