加法:在java中有运算和拼接的功能,但是数据库中加号只做运算。
拼接是concat(...,...)
ifnull(字段,0) 为null就返回逗号后面的东西0.
1.条件表达式 :>、<、=、!=/<>、>=、<=
2.逻辑表达式 :&&、||、!、and、or、not
3.模糊查询 :like、between and、in、is null
在模糊查询时,下划线分不清楚的时候可以用转义。
例如:查找名字第三个字符为e? like '__e%'
百分号和下划线
' %a% '? ' _e?'
1.between 100 and 120 包含边界值100、120.并且100和120不可以调换顺序。
2.in( )? 里面的值类型要兼容统一,不支持通配符
3.安全等于:<=>, 判断是否等于
-- 加号+
-- mysql中的加号功能就是作为运算符。
select 100+90;
SELECT '123'+90; -- 一方为字符 就将字符转化成数值型
SELECT 'John'+90; -- 转化失败就将字符当0 继续做运算
SELECT NULL+90; -- 一方为null 结果为null
-- 拼接 concat函数
SELECT CONCAT(last_name,first_name) as 姓名 FROM employees;
-- 练习
SELECT last_name,job_id,salary as sal FROM employees;
SELECT * FROM employees;
DESC employees;
SELECT * FROM departments;
SELECT DISTINCT job_id FROM employees;
-- IFNULL(commission_pct,0))判断是否为空函数
SELECT CONCAT(hiredate,first_name,IFNULL(commission_pct,0)) as out_put FROM employees;
SELECT IFNULL(commission_pct,0) AS 奖金率,commission_pct FROM employees;
-- 条件查询 where + 筛选条件
-- and ,or ,not ,<> ,like ,between and ,in ,is null.
-- 按条件表达式筛选
-- 查询工资大于10000的员工信息
SELECT * FROM employees WHERE salary>10000;
-- 查询部门编号不等于90号的员工名和部门编号
SELECT last_name,department_id FROM employees WHERE department_id!=90;
SELECT last_name,department_id FROM employees WHERE department_id<>90;
-- 按逻辑表达式筛选
-- 查询工资在10000到20000之间的员工名、工资以及奖金
SELECT employee_id,salary,commission_pct FROM employees WHERE salary>=10000 AND salary<=20000;
-- 查询部门编号不是在90到110之间的,或者工资高于15000的员工信息
SELECT * FROM employees WHERE department_id<90 OR department_id>110 OR salary>=15000;
SELECT * FROM employees WHERE department_id>=90 AND department_id<=110 OR salary>=15000;
-- 模糊查询
-- like \ BETWEEN AND \ in \ IS NULL \ is not null
-- 查询员工名中包含字符a的员工信息
SELECT * FROM employees WHERE first_name LIKE '%a%';
-- 查询第三个字符为n 第五个字符为l
SELECT last_name,salary FROM employees WHERE last_name LIKE '__n_l%';
-- 查询员工名中第二个字符为_的员工名
SELECT last_name FROM employees WHERE last_name LIKE '_\_%';
SELECT last_name FROM employees WHERE last_name LIKE '_*_%' ESCAPE '*';
-- ESCAPE转义
-- BETWEEN AND
-- 查询员工编号在100到120之间的员工数据
SELECT * FROM employees WHERE department_id BETWEEN 100 AND 120;
-- in 提高语句简洁度
-- 查询员工的工种编号是 IT_PROG AD_VP
SELECT last_name,job_id FROM employees WHERE job_id IN('IT_PROT','AD_VP');
-- is null
-- 查询没有奖金的员工名和奖金率
SELECT last_name,commission_pct FROM employees WHERE commission_pct is NULL;
-- <=> 安全等于
SELECT last_name,commission_pct FROM employees WHERE commission_pct <=> NULL;
-- 练习
-- 查询员工号为176的员工的姓名和部门号和年薪
SELECT last_name,department_id,salary*12*(1+ISNULL(commission_pct,0)) as 年薪 FROM employees;
-- 查询工资大于12000的员工姓名和工资
SELECT last_name,salary FROM employees WHERE salary>12000;
-- 选择工资不在5000 到12000 的员工姓名和工资
SELECT last_name,salary FROM employees WHERE salary<=5000 or salary >12000;
-- 选择在20或50号部门工作的员工姓名和工资
SELECT last_name,salary,department_id FROM employees WHERE department_id=20 or department_id=50;
-- 选择公司中没有管理者的员工姓名及job_id
SELECT last_name,job_id FROM employees WHERE job_id NOT in('ST_MAN');
-- 选择公司中有奖金的员工姓名,工资和奖金级别
SELECT last_name,salary,commission_pct FROM employees WHERE commission_pct is NOT NULL;
-- 选择员工姓名的第三个字母是a的员工姓名
SELECT last_name FROM employees WHERE last_name LIKE '__a%';
-- 选择姓名中有字母a和e的员工姓名
SELECT last_name FROM employees WHERE last_name LIKE '%ae%';
-- 查询表结构
DESC departments;
-- 查询部门departments 表中涉及到了哪些位置编号
SELECT DISTINCT location_id FROM departments;