MySQL随记
一、基本sql语句
1.查看指定数据库中的所有表 show tables from mysql; 2.查看自己在哪个数据库中 select database(); 3.查看表的结构 desc user; 4.查看当前数据库版本 select version(); 5.终端里查看 mysql --version 或 mysql -V 6.注释 (1)单行注释:#注释语句 (2)单行注释:-- 注释语句 (3)多行注释:/*注释语句*/
二、基础查询
# 进阶1:基础查询 /* 1.语法 select 查询列表 from 表明; 2.特点 (1)查询列表可以是:表中的字段、常量值、表达式、函数 (2)查询的结果是一个虚拟的表格 */ USE myemployees ; # 1.单字段查询 SELECT `last_name` FROM employees; # 2.多字段查询 SELECT `last_name`, `first_name`, `email` FROM employees; # 3.所有字段查询 SELECT * FROM employees; # 4.起别名,两种方式:AS或空格;如果别名有特殊符号用引号 SELECT 100%98 AS 结果; SELECT last_name AS '姓', first_name AS '名' FROM employees; SELECT last_name 姓, first_name 名 FROM employees; # 5.去重 # 案例:查询员工表中涉及到所有部门编号 SELECT DISTINCT `department_id` FROM employees; # + 作用:只作为数值相加 # 案例:员工姓、名连接成一个字段 SELECT CONCAT(last_name, first_name) AS '姓名' FROM employees;
三、条件查询
# 进阶2:条件查询 /* 1.语法 select 查询列表 from 表名 where 筛选条件; 2.分类 (1)按条件表达式筛选[> = < != <> >= <=] (2)按逻辑表达式筛选[&& || ! and or not] (3)按模糊筛选[like、between and、in、is null] */ # 一、条件查询 # 案例1:查询工资大于12000员工信息 SELECT * FROM employees WHERE salary > 12000; # 案例2:查询部门编号!=90的员工名和部门编号 SELECT first_name, department_id FROM employees WHERE department_id<>90; # 二、逻辑表达式 # 案例1:查询工资在10000到20000之间的员工名、工资以及奖金 SELECT first_name, salary, commission_pct FROM employees WHERE salary >=10000 AND salary <=20000; # 案例2:查询部门编号不是90到110之间,或者工资高于15000的员工信息 SELECT * FROM employees WHERE NOT(department_id >= 90 AND department_id <= 110) OR salary >15000; # 三、模糊查询 /* 通配符: ① % 任意多个字符,包含0个字符 ② _ 任意单个字符 */ # 案例1:查询员工名中包含字母a的员工信息(like关键字) SELECT * FROM employees WHERE last_name LIKE '%a%' # 案例2:查询员工名中第三个字母n,第五个字符为l的员工名、工资 SELECT last_name, salary FROM employees WHERE last_name LIKE '__n_l%'; # 案例3:查询员工名中第二个字符为 _ 的员工名,需要使用\对_进行转义 SELECT last_name, salary FROM employees WHERE last_name LIKE '_\_%'; # 案例4:查询员工编号在100到120之间的员工信息(between and 是包含临界值的) SELECT * FROM employees WHERE employee_id BETWEEN 100 AND 120; # 案例5:查询员工的工种ID是IT_PROG/AD_VP/PU_MAN中之一的员工名工种ID(in 关键字) SELECT last_name, job_id FROM employees WHERE job_id IN ('IT_PROG', 'AD_VP', 'PU_MAN'); # 案例6:查询没有奖金的员工名和奖金率(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; # 案例7:安全等于 SELECT last_name, commission_pct FROM employees WHERE commission_pct <=> NULL; SELECT last_name, commission_pct FROM employees WHERE commission_pct <=> 0.20; /* IS NULL:仅仅可以判断NULL值(√) <=>:既可判断NULL值,也可判断数值 */
# 查询员工号为176的员工姓名和部门号、年薪(如果利率为NULL则为0计算) SELECT last_name, department_id, salary*12*(1+IFNULL(commission_pct, 0)) AS annual_salary FROM employees WHERE employee_id=176;
四、排序查询
# 进阶3:排序查询 /* 语法: select 查询列表 ③ from 表 ① where 筛选条件 ② order by 排序列表[asc | desc] ④ 特点: ① 如果不写,默认升序 ② order by 支持单个字段、多个字段、表达式、函数、别名 ③ order by 一般放查询语句最后面,limit子句除外 */ # 案例1:查询员工信息,要求工资从高到低排序(asc升序,desc降序) SELECT * FROM employees ORDER BY salary DESC; SELECT * FROM employees ORDER BY salary ASC; # 案例2:查询部门编号>=90的员工信息,按入职时间的先后进行排序 SELECT * FROM employees WHERE department_id >= 90 ORDER BY hiredate ASC; # 案例3;按年薪高低显示员工信息和薪资 SELECT *, salary*12*(1+IFNULL(commission_pct, 0)) AS annual_salary FROM employees ORDER BY annual_salary DESC; # 案例4:按员工名长度显示员工姓名和工资 SELECT last_name, salary, LENGTH(last_name) AS name_length FROM employees ORDER BY name_length DESC; # 案例5:查询员工信息,要求先按工资排序,再按员工编号排序 # 先按照工资升序,如果工资相同再按照员工id降序 SELECT * FROM employees ORDER BY salary ASC, employee_id DESC;
相关推荐
苏康申 2020-11-13
vitasfly 2020-11-12
BiPerler 2020-06-28
hungzz 2020-06-16
专注前端开发 2020-10-21
oraclemch 2020-11-06
liuyang000 2020-09-25
FellowYourHeart 2020-10-05
赵继业 2020-08-17
whyname 2020-08-16
Seandba 2020-08-16
dbasunny 2020-08-16
拼命工作好好玩 2020-08-15
langyue 2020-08-15
写程序的赵童鞋 2020-08-03
Accpcjg 2020-08-02