SQL基础语句
/*
语法:
select 查询列表 from 表名
特点:
1.查询列表可以是:表中的字段、常量值、表达式、函数
2.查询的结果是虚拟的表格
*/
-- 注意查询之前,要打开指定的库,use myemployees;
USE myemployees;
SELECT last_name FROM employees;
SELECT last_name,salary,email FROM employees;
SELECT*FROM employees;
-- 顺序和表中的字段一样
SELECT 100;
SELECT 'JOIN';
SELECT 100*99;
SELECT VERSION();-- 8.0.11
/*
好处:
1.便于理解
2.如果要查询的字段有重名的情况,使用别名可以区分开
*/
SELECT 100*77 AS 结果 haha;
SELECT last_name AS 姓,first_name AS 名 FROM employees;
SELECT 100*77 结果 haha;
SELECT last_name 姓,first_name 名 FROM employees;
-- SELECT salary AS OUT put FROM employees;会报错
-- 因为out是关键词,所以,如果别名里面有关键词的,请加双引号,或者单引号。
SELECT salary AS "OUT put" FROM employees;
SELECT DISTINCT department_id FROM employees;
/*
1.+号只有一个功能:运算符。
select 100+90; 这个做加法
2.SELECT NULL+10; --只要一方为NULL,则结果肯定是NULL。
*/
-- 错误❌:SELECT last_name+first_name AS 姓名 FROM employees;
SELECT CONCAT(last_name,first_name) AS 姓名 FROM employees;
DESC departments;
SELECT * FROM departments;
SELECT DISTINCT job_id FROM employees;
-- commission_pct有些值是NULL
-- 因为NULL和谁拼接都是NULL
-- 所以下面的结果可能为NULL
SELECT CONCAT(employee_id,',',first_name,',',last_name,",",commission_pct) AS "OUT_PUT" FROM employees;
SELECT IFNULL(commission_pct,0) AS 奖金率,commission_pct FROM employees;
-- 上面题的做法:
SELECT CONCAT(employee_id,',',first_name,',',last_name,",",IFNULL(commission_pct,0)) AS "OUT_PUT" FROM employees;
/*
语法:
select 查询列表 from 表名 where 筛选条件;
查询列表可以是:表中的字段、常量值、表达式、函数
分类:
一。按条件表达式筛选
条件运算符:> < = !=或者<> >= <=
二。按逻辑表达式筛选
逻辑运算符:z
作用:连接条件表达式
&& || ! 或者 and or not
三。模糊查询
like
BETWEEN AND
in
is null 或者 is not null
*/
SELECT * FROM employees WHERE salary>12000;
SELECT employee_id,department_id FROM employees WHERE department_id<>90;
SELECT last_name,salary,commission_pct
FROM employees
WHERE salary>=10000 AND salary<=20000;
SELECT * FROM employees WHERE department_id<90 or department_id >110 or salary>15000;
-- 或者写成
SELECT * FROM employees WHERE NOt(department_id>=90 AND department_id <=110) or salary>15000;
/*like 特点:
一般和通配符搭配使用,
通配符
% :任意多个字符,包含0个字符
_ :占一个字符
*/
-- %代表通配符
SELECT * FROM employees WHERE last_name LIKE '%a%';
SELECT last_name,salary FROM employees WHERE last_name LIKE '__n_l%';
-- \转义字符
SELECT last_name FROM employees WHERE last_name LIKE ' _%';
-- 或者 告诉系统 %' ESCAPE '$';
/*
1.使用between and 可以提高语句简洁度
2.包好临界值
3.前小后大,不能颠倒顺序
*/
SELECT * FROM employees WHERE employee_id>=100 AND employee_id<=120;
-- 或者
SELECT * FROM employees WHERE employee_id BETWEEN 100 AND 120;
/*
含义:判断某字段的值是否属于in列表中的某一项
特点:
1.in 比 or 简洁
2.in列表的值类型必须是相同的或者兼容的
3.in列表的值不能使用通配符
*/
SELECT last_name,job_id FROM employees WHERE job_id='IT_PROG' or job_id='AD_PRES' or job_id='AD_VP';
-- 或者
SELECT last_name,job_id FROM employees WHERE job_id IN('IT_PROG' , 'AD_PRES' ,'AD_VP');
/*
=或者<>不能用于判断null值,所以只能用 is null和 is not null
*/
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;
-- 安全等于作用:判断是否等于,包括null,如果等于返回true。可读性较低
-- is null :仅仅判断null值,可读性较高。
-- 直接写=是不行的
SELECT last_name,commission_pct FROM employees WHERE commission_pct <=> null;
SELECT * FROM employees WHERE salary <=> 12000;
-- 注意这个IFNULL
SELECT last_name,department_id,salary 12 (1+IFNULL(commission_pct,0))
as 年薪
FROM employees
WHERE employee_id=176;
SELECT salary,last_name FROM employees WHERE commission_pct IS NULL AND salary<18000;
SELECT * FROM employees WHERE job_id<>'IT' OR salary=12000;
DESC departments;
SELECT DISTINCT location_id FROM departments;
SELECT * FROM employees WHERE commission_pct like '%%' and last_name like '%%';-- commission_pct有字段有null,and之后就是null了