一.基础查询
1.去重 distinct
SELECT DISTINCT `department_id` FROM `employees`;
2.拼接 concat
select concat(`last_name`,`first_name`) from `employees`;
SELECT CONCAT('a','b','c');
3.DESC 显示表的字段和类型
DESC `employees`;
4.模糊查询
like
between and
in
is null
查询名字中带“a”的员工信息
select `last_name` from `employees` where `last_name`like '%a%';
查询第三个字符和第五个字符分别为"t""s"的
select `last_name`,`salary` from `employees` where `last_name`like '__t_s%';
查询名字中第二个字符为_的名字
SELECT `last_name` FROM `employees` WHERE `last_name` LIKE '_\_%';
查询员工编号在100 到120之间的员工编号(包含临界值信息)
SELECT `employee_id` FROM `employees` WHERE `employee_id` BETWEEN 100 AND 120;
用于判断某字段的值是否属于in列表中的一项
查询员工的工种编号是'AC_MGR','AD_VP'的相关信息
SELECT `job_id` FROM `employees` WHERE `job_id` IN('AC_MGR','AD_VP');
查询没有奖金的
SELECT `last_name`, `commission_pct` FROM `employees` WHERE `commission_pct` IS NULL;
SELECT `last_name`, `commission_pct` FROM `employees` WHERE `commission_pct` IS NOT NULL;
5.逻辑运算符
&& || !
and or not
select `last_name`, `salary`, `commission_pct` is not null from `employees` where 10000<`salary`<12000;
SELECT * FROM `employees` WHERE NOT(90<`department_id`<110) OR`salary`>15000;
6.安全等于<=>
SELECT `last_name`,`salary` FROM `employees` WHERE `salary`<=>12000;
二.排序查询
排序查询
select 查询列表
from 表
where 筛选条件
order by 排序列表 asc|desc
1.order by 加上desc(降序),如果写默认是升序
select `salary` from `employees` order by `salary` desc;
2. 按照入职日期排序,也就是升序
select * from `employees` where `department_id`>=90 order by `hiredate` asc;
3.出现null值用0代替,计算年薪
SELECT *,`salary`*12*(1+IFNULL(`commission_pct`,0)) 年薪 FROM `employees` ORDER BY 年薪 ASC;
select `salary`*12*(1+ifnull(`commission_pct`,0)) 年薪 from `employees` order by 年薪 desc;
4.按照名字长度排序
SELECT CONCAT(`first_name`,`last_name`), LENGTH(CONCAT(`first_name`,`last_name`)) 名字长度 FROM`employees` ORDER BY 名字长度 DESC;