sql基础汇总
--根据函数别名排序
--排序规则,默认是升序
sleect LENGTH(NAME) nameLength from user ORDER BY nameLength DESC
--特殊符号
特殊符号<=> 安全等于可以比较null和数值,正常比较null通过is null 和is not null 数值是 = <> !=和<>相同,in(1,2)是1或者2 between 1 and 10 是1~10的所有整数
IFNULL(num,0) num不为null 返回num否则返回0
--两个以上字段排序,不写默认升序 asc
SELECT * FROM user ORDER BY age DESC, salary ASC;
--查询年龄在20-40之间
SELECT * FROM user WHERE age BETWEEN 20 AND 40 ORDER BY age DESC
--查询年龄在20-40之间
SELECT * FROM user WHERE age NOT BETWEEN 20 AND 40 ORDER BY age DESC
--模糊查询
SELECT * FROM user WHERE name LIKE "%a%" ORDER BY LENGTH(name) DESC, phone ASC
--函数
--单行函数 concat()字符串拼接、ifnull、length()
--分组函数
--分组函数又称为统计函数、聚合函数、组函数
--拼接字符串
SELECT CONTACT(name,"--",age) FROM user;
--大小写切换
SELECT UPPER(name),LOWER(type);
--截取字符串,下表从1开始,从第2截取到末尾
SELECT SUBSTRING("ABCDEFG",2);
--截取(1,2),从第一位开始,截取两个字符
SELECT SUBSTRING("ABCDEFG",1,2);
--返回子串第一次出现的索引
SELECT INSTR("ABCDEF",'DE')
--去除空格
SELECT TRIM(" DDD ");
--去除a,只能去除字符串的前后,不能去除中间
SELECT TRIM('a' FROM "AaaaaBCDEFDALKDFJAaaaaaaaa");
--指定长度的左填充,格式 2222222AAA
SELECT LPAD("AAA",10,"2");
--右填充
SELECT LPAD("AAA",10,"2");
--字符串替换,将A替换为UUU
SELECT REPLACE("111AAA222","A","UUU")
--取整数函数,四舍五入
SELECT ROUND(1.2);
--保留两位小数
SELECT ROUND(1.556,2);
--向上取整,返回大约等于该值的最小整数
SELECT CEIL(1.02);
--向下取整,返回小于等于该值的最大整数
SELECT FLOOR(1.2);
--阶段,保留小数位数
--SELECT TRUNCATE(1.34,1);
--取余是1
SELECT MOD(10,3);
--日期+时间
SELECT NOW();
--日期
SELECT CURDATE();
--时间
SELECT CURTIME();
--获取年份
SELECT YEAR(NOW());
--月
SELECT MONTH(NOW());
--英文月份名称
SELECT MONTHNAME(NEWO());
--字符串转日期
SELECT STR_TO_DATE("1994-3-9","%Y-%C-%D");
--日期转字符串
SELECT DATE_FORMAT(NOW(),"%Y年-%m月-%d日");
--查询两个日期 相差天数
SELECT DATEDIFF();
--流程控制
--大于,大,否则小
SELECT IF(10>5,"大","小");
--case函数
SELECT salary,id
CASE id
when 30 THEN SALARY*1.3
WHEN 40 THEN SALARY*1.5 FROM user
SELECT salary,id
CASE
when 条件1 THEN SALARY*1.3
WHEN 条件2 THEN SALARY*1.5 FROM user
--分组函数 ,用作统计
SELECT SUM(salary),AVG(salary),MIN(salary),MAX(salary),COUNT(salary) FROM user;
--查询部门最大工资和平均工资
SELECT AVG(salary),MAX(salary) FROM user where age is not null GROUP BY department;
--查询部门员工个数大于2,分组后的筛选用having
SELECT COUNT(*),department_id
FROM employees
GROUP BY department_id
HAVING COUNT(*)>2
--查询每个工种有奖金的员工的最高工资大于12000的
SELECT job_id,MAX(salary)
FROM employees
where reward is not null
group by job_id
having MAX(salary)>12000;
--查询每个领导编号>102的领导手下工资>5000的领导编号和最低工资
SELECT manage_id,MIN(salary)
FROM employees
GROUP BY manage_id
WHERE manage_id>102
HAVING MIN(salary)>5000;
--查询每个部门每个工种的平均工资,降序
SELECT job_id,department_id,AVG(salary) as avg
FROM employee
WHERE department_id is not null
GROUP BY job_id,department_id
HAVING avg>120000
ORDER BY agv DESC
--聚合函数通过HAVING筛选,不能用where
--查询工资和级别
select salary,grade
from user,grade
where selary between grade.low and grade.up
--内链接 筛选条件放在where 连接条件放在on
select name,job
from user u inner join job j on u.id = j.id
where name like "%a%";
select a.name,b.id,c.type
from a inner join b on a.id=b.id inner join c on a.id=c.id
where a.name like "%a%"
group by a.name;
--内连接是 显示交集,外连接,左是已左表为主表,显示全部主表