数据库中最核心最重要的语言
SELECT * FROM student
SELECT `StudentNo`, `StudentName` FROM student
SELECT `StudentNo` AS 序号 ,`StudentName` AS 姓名 FROM student
SELECT CONCAT('姓名:', StudentName) FROM student
SELECT DISTINCT `StudentNo` FROM `result`
SELECT VERSION() —————— 查询系统版本
SELECT 520*1314 AS 结果 —————— 计算结果
SELECT `StudentNo`,`StudentResult` AS 原成绩,`StudentResult`+1 AS 加分后 FROM `result`
SELECT studentNo,`StudentResult` FROM result
WHERE StudentResult>=95 AND StudentResult<=100
SELECT studentNo,`StudentResult` FROM result
WHERE `SubjectNo`!= 1;
like
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `StudentName` LIKE '刘%'
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `StudentName` LIKE '刘_'
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `StudentName` LIKE '刘__'
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `StudentName` LIKE '%十%'
in
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `StudentNo` IN (1001,1002,1003)
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `Address` IN ('北京东城区','安徽')
null
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `Address` = ''
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `BornDate` IS NOT NULL
SELECT `StudentNo`,`StudentName` FROM `student`
WHERE `BornDate` IS NULL
连接语句 | 解释 |
---|---|
inner join | 若表中有一个匹配 则连接成功 |
left join | 查询左表中所有值 即使右表没有 |
right join | 查询右表中所有值 即使右表没有 |
inner join
SELECT s.`StudentNo`,`StudentName`,`SubjectNo`,`StudentResult`
FROM student s
INNER JOIN result r
ON s.`StudentNo` = r.`StudentNo`
right join
SELECT s.`StudentNo`,`StudentName`,`SubjectNo`,`StudentResult`
FROM student s
RIGHT JOIN result r
ON s.`StudentNo` = r.`StudentNo`
left join
SELECT s.`StudentNo`,`StudentName`,`SubjectNo`,`StudentResult`
FROM result r
LEFT JOIN student s
ON s.`StudentNo` = r.`StudentNo`
SELECT s.`StudentNo`,`StudentName`,`SubjectNo`,`StudentResult`
FROM result r
LEFT JOIN student s
ON s.`StudentNo` = r.`StudentNo`
WHERE `StudentResult` IS NULL
SELECT s.`StudentNo`,`StudentName`,`SubjectName`,`StudentResult`
FROM student s
INNER JOIN result r
ON s.`StudentNo` = r.`StudentNo`
INNER JOIN `subject` sub
ON r.`SubjectNo` = sub.`SubjectNo`
SELECT `StudentNo`,`StudentName`,`GradeName`
FROM student s
INNER JOIN `grade` g
ON s.`GradeId` = g.`GradeID`
SELECT `SubjectName`,`GradeName`
FROM `subject` sub
INNER JOIN `grade` g
ON sub.`GradeId` = g.`GradeID`
SELECT s.`StudentNo`,`StudentName`,`SubjectName`,`StudentResult`
FROM student s
INNER JOIN `subject` sub
ON s.`GradeID` = sub.`GradeId`
INNER JOIN `result` r
ON sub.`SubjectNo` = r.`SubjectNo`
WHERE `SubjectName` = '数据库结构-1'
SELECT a.`categoryName` AS 父栏目 ,b.`categoryName` AS 子栏目
FROM `category` AS a, `category` AS b
WHERE a.`categoryid` = b.`pid`
DESC——降序
ASC——升序
SELECT s.`StudentNo`,`StudentName`,`SubjectName`,`StudentResult`
FROM student s
INNER JOIN `subject` sub
ON s.`GradeID` = sub.`GradeId`
INNER JOIN `result` r
ON sub.`SubjectNo` = r.`SubjectNo`
WHERE `SubjectName` = '数据库结构-1'
ORDER BY `StudentResult` ASC
LIMIT 0,10
方法一:联表查询
SELECT `StudentNo`,r.`SubjectNo`,`StudentResult`
FROM `result` r
INNER JOIN `subject` sub
ON r.SubjectNo = sub.SubjectNo
WHERE SubjectName = '数据库结构-1'
ORDER BY StudentResult DESC
方法二:子查询
SELECT `StudentNo`,`SubjectNo`,`StudentResult`
FROM `result`
WHERE SubjectNo = (
SELECT SubjectNo FROM `subject`
WHERE SubjectName = '数据库结构-1' )
ORDER BY StudentResult DESC
联表查询
SELECT s.`StudentNo`,`StudentName`
FROM student s
INNER JOIN `result` r
ON s.`StudentNo` = r.`StudentNo`
INNER JOIN `subject` sub
ON r.`SubjectNo` = sub.`SubjectNo`
WHERE `SubjectName` = '高等数学-2' AND `StudentResult`>=80
子查询
SELECT s.`StudentNo`,`StudentName`
FROM student s
INNER JOIN `result` r
ON s.`StudentNo` = r.`StudentNo`
WHERE `StudentResult`>=80 AND`SubjectNo` = (
SELECT `SubjectNo` FROM `subject`
WHERE `SubjectName`='高等数学-2'
)
SELECT SubjectName, AVG(StudentResult) AS 平均分,MAX(StudentResult) AS 高 分,MIN(StudentResult) AS 低分
FROM result r
INNER JOIN `subject` sub
ON r.`SubjectNo` = sub.`SubjectNo`
GROUP BY r.SubjectNo -- 平均分>80
SELECT ABS(-8) —————— 绝对值
SELECT CEILING(9.4) —————— 向上取整
SELECT FLOOR(9.4) —————— 向下取整
SELECT RAND() —————— 返回一个 0~1 之间的随机数
SELECT SIGN(10) —————— 判断一个数的符号 0-0 负数返回-1,正数返回 1
SELECT CHAR_LENGTH('即使再小的帆也能远航') —————— 字符串长度
SELECT CONCAT('我','爱','你们') —————— 拼接字符串
SELECT INSERT('我爱编程helloworld',1,2,'超级热爱') —————— 查询从某个位置开始替换某个长度
SELECT LOWER('KuangShen') —————— 小写字母
SELECT UPPER('KuangShen') —————— 大写字母
SELECT INSTR('kuangshen','h') —————— 返回第一次出现的子串的索引
SELECT REPLACE('狂神说坚持就能成功','坚持','努力') —————— 替换出现的指定字符串
SELECT SUBSTR('狂神说坚持就能成功',4,6) —————— 返回指定的子字符串 (源字符串,截取的位置,截 取的长度)
SELECT REVERSE('清晨我上马') —————— 反转
SELECT REPLACE(studentname,'周','邹') FROM student
WHERE studentname LIKE '周%'
SELECT CURRENT_DATE() —————— 获取当前日期
SELECT CURDATE() —————— 获取当前日期
SELECT NOW() —————— 获取当前的时间
SELECT LOCALTIME() —————— 本地时间
SELECT SYSDATE() —————— 系统时间
SELECT YEAR(NOW()) —————— 年
SELECT MONTH(NOW()) —————— 月
SELECT DAY(NOW()) —————— 日
SELECT HOUR(NOW()) —————— 时
SELECT MINUTE(NOW()) —————— 分
SELECT SECOND(NOW()) —————— 秒
SELECT SYSTEM_USER() ——————查询系统用户
SELECT USER() —————— 查询用户
SELECT VERSION() —————— 查询版本号
函数名称1 | 描述 |
---|---|
Count( ) | 计数 |
Sum( ) | 求和 |
AVG( ) | 平均值 |
Max( ) | 最大值 |
Min( ) | 最小值 |
SELECT COUNT(`StudentNo`) FROM student —————— 计数
SELECT SUM(`StudentResult`) AS 总和 FROM result
SELECT AVG(`StudentResult`) AS 平均分 FROM result
SELECT MAX(`StudentResult`) AS 高分 FROM result
SELECT MIN(`StudentResult`) AS 低分 FROM result