作者:叁念
数据库SQL之DML语句的使用
求绝对值
SELECT ABS(-5.1);
求出比这个数大的最小的整数
SELECT CEILING(0.1);
求出比这个数小的最大的整数
SELECT FLOOR(-0.1);
求出最大值
SELECT GREATEST(5,3,2,12);
求出最小值
SELECT LEAST(5,3,2,12);
取余数
SELECT MOD(3, 2);
四舍五入
SELECT ROUND(10.555999, 3);
截断
SELECT TRUNCATE(10.999, 1);
求随机数
SELECT RAND(10);
开根号
SELECT SQRT(25);
求几的几次方
SELECT POW(2,3);
求字符串的长度
SELECT LENGTH('hello world');
SELECT LENGTH('你好');
全部变小写
SELECT LCASE('HeLLo');
SELECT LOWER('HeLLo');
全部变大写
SELECT UCASE('hello');
SELECT UPPER('hello');
字符串比较 前面一个大结果就是1,前面一个小结果就是-1,前后一样就是0
SELECT STRCMP('zello','yes');
子串的位置
SELECT POSITION('lo' IN 'hello');
替换
SELECT REPLACE('yes','y','hh');
插入
在hello的第2个位置开始,替换1个字符,插入xx
SELECT INSERT('hello',2,1,'xx');
拼接
SELECT CONCAT('hello ','world ','sql');
SELECT CONCAT_WS(';',1,2,3);
取左边的字符
SELECT LEFT('hello', 3);
取右边的字符
SELECT RIGHT('hello', 3);
左边填充
SELECT LPAD('hello',10,'x');
右边填充
SELECT RPAD('hello',10,'y');
去空格
SELECT LTRIM(' hello ');
SELECT RTRIM(' hello ');
SELECT TRIM(' hello ');
子串
SELECT SUBSTRING('51code',3,4);
当前的时间
SELECT NOW();
SELECT SYSDATE();
当前日期
SELECT CURRENT_DATE();
当前的时间
SELECT CURRENT_TIME();
当前的时间戳
SELECT CURRENT_TIMESTAMP();
打印年份
SELECT YEAR('97-08-24');
打印月份
SELECT MONTH('97-08-24');
打印月份名
SELECT MONTHNAME('97-08-24');
打印是一年中第几天
SELECT DAYOFYEAR('2017-8-24');
日期的加法
SELECT DATE_ADD(NOW(),INTERVAL 3 DAY);
求平均值
SELECT AVG(sheight) FROM student;
求和
SELECT SUM(sage) FROM student;
求最大值
SELECT MAX(sheight) FROM student;
求最小值
SELECT MIN(sheight) FROM student;
求个数
SELECT COUNT(sid) FROM student;
求标准差
SELECT STDDEV(sheight) FROM student;
求方差
SELECT VARIANCE(sheight) FROM student;
SELECT * FROM student;
SELECT sname, sage FROM student;
别名查询
SELECT sname AS 姓名, sage AS 年龄 FROM student;
SELECT sname 姓名, sage 年龄 FROM student;
去重查询
查询有多少老师
SELECT DISTINCT teacher FROM lesson;
查询有参加考试的学生的学号
SELECT DISTINCT sid FROM score;
运算查询
查询所有人的分数 - 20 分之后的分数
SELECT score - 20 FROM score;
查询所有学生的年龄增加一岁之后的年龄
SELECT sage + 1 FROM student;
条件记录查询
查询分数大于70分的所有记录
SELECT * FROM score WHERE score > 70;
查询身高在170到180之间的所有记录
SELECT * FROM student WHERE sheight BETWEEN 170 AND 180;
SELECT * FROM student WHERE sheight >= 170 AND sheight <= 180;
查询身高小于160或大于190的所有记录
SELECT * FROM student WHERE sheight < 160 OR sheight > 190;
空值查询
查询年龄是空的记录
SELECT * FROM student WHERE sage IS NULL;
查询年龄不是空的记录
SELECT * FROM student WHERE sage IS NOT NULL;
集合中记录查询
查询身高是170,180,190的所有记录
SELECT * FROM student WHERE sheight IN(170,180,190);
查询身高不是170,180,190的所有记录
SELECT * FROM student WHERE sheight NOT IN(170,180,190);
模糊查询 %表示多个字符 _表示一个字符
查询姓王的学生的记录
SELECT * FROM student WHERE sname LIKE '王%';
查询姓张但是名字只有2个字的学生记录
SELECT * FROM student WHERE sname LIKE '张_';
查询第二个字是飞的学生记录
SELECT * FROM student WHERE sname LIKE '_飞%';
排序查询
默认是升序
SELECT * FROM student ORDER BY sheight;
SELECT * FROM student ORDER BY sheight ASC;
降序
SELECT * FROM student ORDER BY sheight DESC;
多字段排序,当身高一样的时候,年龄小的在前面
SELECT * FROM student ORDER BY sheight DESC, sage ASC;
限制数量查询(重点)
总共读5条记录
SELECT * FROM student LIMIT 5;
从第(2+1)条记录开始,再读5条记录
SELECT * FROM student LIMIT 2,5;
分组查询
SELECT lid, AVG(score) FROM score GROUP BY lid;
交叉连接 10 * 4 , 9 * 3 = 90 * 7
SELECT * FROM student, score;
SELECT * FROM student CROSS JOIN score;
内连接
SELECT * FROM student,score WHERE student.sid = score.sid;
SELECT * FROM student INNER JOIN score WHERE student.sid = score.sid;
SELECT * FROM score, student WHERE student.sid = score.sid;
外连接
左外连接 - 左表记录完全保留的情况下,把右表拼接上去
SELECT * FROM student LEFT JOIN score ON student.sid = score.sid;
右外连接
SELECT * FROM score RIGHT JOIN student ON student.sid = score.sid;
1. 打印出所有学生的基本信息,以及1号课程的考试分数
SELECT * FROM student LEFT JOIN score ON student.sid = score.sid AND score.lid = 1;
2. 打印出所有考试分数的信息,以及对应的学生信息,以及对应的任课老师
SELECT score.*, student.sname, student.sage, student.sheight, teacher
FROM score,student,lesson
WHERE score.sid = student.sid AND score.lid = lesson.lid;
【子查询】
单行单列子查询
查询比王五年龄大的学生信息
SELECT * FROM student WHERE sage > (SELECT sage FROM student WHERE sname = '王五');
查询考了100分的学生记录
SELECT *
FROM student
WHERE sid = (SELECT sid FROM score WHERE score = 100)
SELECT *
FROM student,score
WHERE student.sid = score.sid AND score = 100;
单行多列子查询
查询年龄和身高都和王五一样的学生信息
SELECT *
FROM student
WHERE (sage, sheight) = (SELECT sage, sheight FROM student WHERE sname = '王五');
多行单列子查询
1. IN 关键字 - 查询的条件在子查询的查询结果中
查询考了85分的学生信息
SELECT *
FROM student
WHERE sid IN (SELECT sid FROM score WHERE score = 85);
2. ANY 关键字 - 查询的条件满足子查询结果的任意一条记录
查询比张三、李四、王五中任意一个人年龄小的学生信息
SELECT *
FROM student
WHERE sage < ANY (SELECT sage FROM student WHERE sname IN('张三','李四','王五'));
3. ALL 关键字 - 查询的条件满足子查询的所有记录
查询比张三、李四、王五中所有人的年龄都小的学生信息