基本由 select+from+where组成
--> select 字段--> from 表或视图名(就是来自哪)--> where 条件--> 最简单的全部查询--> select * from `表名`
02.条件查询_where
01.比较运算符
# >,< , >= , <= , = , <>(不等于)
02.范围查询
between...and...(简化代码)# 例如:查询80 -90之间的学生记录select StudentResult
from result
where StudentResult >=80and StudentResult <=90select StudentResult
from result
where StudentResult between80and90
03.in列表查询
# in (值列表) -- 包含括号中的所有数据# 例: 查询科目id为1 , 2,3的成绩 in:在....里面select*from result
where SubJectId in(1,2,3)
04.模糊查询
# like %(任意数量的任意字符) _(任意的一个字符)# 例1:查询地址中带有京的select*from student
where address like"%京%"# 例2:查询姓徐的两个字的名字 的学生信息 _:匹配一个字符select*from student
where StudentName like"徐_"
05.null
# is null:判断是否为'空' / is not null :判断不是空的值# 例1:查询包含邮箱为null的字段select*from student
where Email isnullselect*from student
where Email =''# 例2:查询不为null 的字段select*from student
where Email isnotnull
06.逻辑运算符
# and--&&(与) , or--||(或) , not--!(非)
07.排序
orderby 字段名 asc(升序--默认可以不写) / desc(降序);
08.分页查询
# 作用:限制查询的条数 (每页显示几条 ,第几页)# 格式: 起始下标,每页显示的条数# 起始下标 = (当前页 - 1) * 每页显示的条数 # 例题:查询c语言中前5的学号 姓名 成绩select s.StudentNo '学号',s.StudentName '姓名',r.StudentResult'成绩'from student s
innerjoin result r on r.StudentNo = s.StudentNO
where r.SubjectNo =(select SubjectNo
from`subject`where SubjectName ='C语言-1')orderby r.StudentResult desclimit5
09.分组
# group by 字段名[ , 字段2 , 字段3 ....]# 补充: where having 分组后数据筛选-- 例:分组查询# 按照不同的课程分组,分别算出其平均分、最高分和最低分,对于低于60分平均分的不予显示# 课程名 最高分 最低分 平均分# 本来max 只会有一个结果 分组后 就会按照分组来求出多个结果select s.SubjectName,s.SubjectNo,max(r.StudentResult),min(r.StudentResult),avg(r.StudentResult) avg
from`subject` s
innerjoin result r on r.SubjectNo = s.SubjectNo
groupby s.SubjectName
having avg >60
03.补充
01.起别名 as
例如: from`Student`as's'=from Student s(简化)
02.去重 distinct
# 去除返回结果中重复的字段
例如:selectdistinct StudentNo
from result # 默认全部显示(all)
03.查询MySQL版本
select VERSION() mysql_v,3.14*5as result # 避免各种诡异的报错
04.笛卡尔积
# 笛卡尔积 :数据膨胀(只有在等值查询中会有出现,而非等值查询不会出现这种问题) -- 解决方法:连表查询
例:
# 笛卡尔积 (两张表中拥有相同数据)select student.StudentNo,result.StudentNo,StudentName,StudentResult
from student,result
# 解决方法 (关联两表)select student.StudentNo,result.StudentNo,StudentName,StudentResult
from student,result
where student.StudentNo = result.StudentNo
--> delete from 表名;truncate student;
不会清空表结构会清空自增计数器
--> delete from student;
不会清空计数器
06.连接查询
# 01--> 内连接: 两张表中的数据必须匹配才能返回(输出共同拥有的)
格式: from 主表格
innerjoin 副表格 on 等值条件
例如:使用内连接查询科目表 java是那个年级的
内连查询
select s.SubjectName,g.GradeName
from`subject` s
innerjoin grade g on g.GradeId = s.GradeId
where s.SubjectName ='Java'
连表查询
select s.SubjectName,g.GradeName
from`subject` s,grade g
where g.GradeId = s.GradeId and s.SubjectName ='Java'# 02外连接 # 01--> 左外连接: 返回所有左表中的数据和匹配到的右表数据 没有匹配到的使用null填充(输出共同拥有的跟左边的).
格式:from 主表格 leftjoin 副表格 on 等值条件
右外连接: 和左外相反
# 02--> 自连接: 在一张表中右父级字段和子级字段 相互有关联
例如:查询所有的动作游戏
select gameType
from game
where parentId =2;
07.子查询
形式:
# 例1:查询课程为《高等数学-2》且分数不小于80分的学生的学号和姓名select StudentNo,StudentName
from student
where StudentNo in(select StudentNo
from result
where StudentResult >=80and SubjectNo =(select SubjectNo
from`subject`where SubjectName ='高等数学-2'))
-- 三表联查的综合应用# 例2:查询<<JAVA第一学年>>课程成绩前十且分数大于80的学生信息select s.StudentNo '学号',s.StudentName'姓名',su.SubjectName'课程名',r.StudentResult '成绩'# 三链接from Student s
innerjoin result r on r.StudentNo = s.StudentNo
innerjoin`subject` su on su.SubjectNo = r.SubjectNo
where s.StudentNo in(select s.StudentNo
from result
where r.StudentResult >80and SubjectName ="JAVA第一学年")# 设置逆序排列,抽取前十成绩orderby r.StudentResult desclimit10-- Ctrl+/:注释快捷键
-- 1.ERROR 1064 (42000):You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'love China' at line 1-- 语法错误-- -- 2.ERROR 1007 (HY000): Can't create database 'ilovechina'; database exists-- 数据库已存在-- -- 3.ERROR 1008 (HY000): Can't drop database 'ilovachina';database doesn't exist-- 数据库不存在-- -- 4.ERROR 1049 (42000):Unknown database 'database'-- 数据库不识别-- -- 5.ERROR 1046 (3D000): No database selected-- 没有选中数据库-- -- 6.ERROR 1113 (42000):A table must have at least 1 column-- 数据表至少要有一列-- -- 7.ERROR 1146 (42S02): Table 'ishop1.cuetomer' doesn't exist-- 数据表"cuetomer" 在数据库“ishop1"中不存在-- -- 8.