mysql有哪些基本语法

发布时间:2020-05-19 14:55:38 作者:三月
来源:PHP中文网 阅读:206

下面讲讲关于mysql有哪些基本语法,文字的奥妙在于贴近主题相关。所以,闲话就不谈了,我们直接看下文吧,相信看完mysql有哪些基本语法这篇文章你一定会有所受益。

                                                          

-- 增,删,改 insert  delete  update

-- 增  必须向所有列填充数据,除了(自增列,有默认值列,允许为空)可以不填充
INSERT [INTO] 表(列列表) values (值列表)


-- 删
DELETE from 表[where 条件]
DELETE from student

-- 改
UPDATE 表 set 列 = 值,列 = 值 [where 条件]
update student set name = '张亮',set  sex = '女' where studentno = '4'

-- 查询 模糊查询  分页  
like between in is null


-- 查询  排序  分组  连接
-- 排序 order by 默认是升序:asc  降序:desc
-- 按多个列来排序,先按第一个字段排序,在此基础上再按第二个字段进行排序.
select * from student order by age,studentno
-- 分组 聚合函数 sum avg max min count
select sum(age),avg(age),max(age),min(age) from student;
-- count 是统计有多少数据行,如果是统计某个列,则会忽略列中的NULL值。
select count(email) from student
-- 统计有多少学生没有录入邮箱信息??
select count(*) from student where email is null


-- 分组,group by  是把数据进行分类再汇总,必须要配合聚合函数使用,
-- 关键点:按什么进行分组,用什么聚合函数进行统计。
-- 如果某个列出现在from关键字前,且没有包含在聚合函数中,则此列必须出现在group by 子句中
-- 统计每个年级有多少学生?
select gradeId,count(*) from student group by gradeId
-- 统计每个年级男女学生各有多少?  按年级和性别进行分组,用count函数
select gradeid,sex,count(*) from student group by sex,gradeId;
-- 统计每个年级有多少课时?
select gradeid,sum(classHours) from subject group by gradeid
-- 统计每个年级有多少课程?
select gradeid,count(*) from subject group by gradeid
-- 统计每个学生的总成绩和平均成绩?
select studentno,sum(result),avg(result) from score group by studentno


-- 连接查询 内连接 外连接 交叉连接
-- 当数据来自两个或两个以上的表时,则才用连接查询来实现。
-- where 条件是两个表的主键列相等。
select * from student s,grade g where s.gradeid=g.gradeid
-- 建议使用下面的写法,性能好一些。
select * from student s inner join grade g on s.gradeid=g.gradeid
-- 查询姓名,学号、课程名、分数  数据来自于3个表?
select name,s.studentno,subjectname,result from student s
 inner join score c on s.studentno = c.studentno
 inner join subject j on c.subjectno= j.subjectno


-- 外连接  左外连接  右外连接
/* 左外连接,在前面的表是主表,后面的表是子表,主表的数据全部显示,
 再用子表的数据进行填充,如果子表中没有对应的数据,则用NULL来填充 */
select * from student s
 left join score c on s.studentno = c.studentno


-- 查询有哪些学生没有参加过考试,用左外连接实现??
select * from student s
 left join score c on s.studentno = c.studentno
 where c.studentno is null
-- 查询哪些学生没有参加考试,用子查询实现??
-- 子查询的结果只能是返回一列值,返回的值如果有多个,就只能用in 不能用 =
select * from student where studentno
not in( select studentno from score)

对于以上mysql有哪些基本语法相关内容,大家还有什么不明白的地方吗?或者想要了解更多相关,可以继续关注我们的行业资讯板块。

推荐阅读:
  1. MYSQL基本语法命令
  2. css基本语法有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql 语法 有哪些

上一篇:python和php哪个好

下一篇:apache日志文件在哪

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》