MySQL数据库的基本操作

创建student表

CREATE TABLE student(
id int not null auto_increment primary key,
code varchar(10) not null,
name varchar(20) not null,
sex varchar(2),
birth date,
school varchar(20) not null,
class varchar(10) not null,
location varchar(16) not null,
score float not null default 0.0,
unique index codeidex(code asc)
);

效果如下图:

2308133192李飞1985-10-01计算机学院2013-1山西太原526
2408133193李大伟1986-10-01计算机学院2013-2山西交口568
2508133194李霞1986-09-01计算机学院2013-3江苏徐州525
2608133195王霞1986-08-01计算机学院2013-4江苏南京528
2708133196王俊1985-08-01计算机学院2013-5北京548
2808133197王飞1985-11-01计算机学院2013-4陕西西安558
2908133198宋小倩1985-12-12计算机学院2013-1江苏杨州598
3008133199宋小俊1985-01-12计算机学院2013-2江苏南通578
31081331910王静1985-08-12计算机学院2013-3江苏南通508

创建class_table表:

效果如下:

2308133192李飞计算机学院2013-1数据结构88
2408133192李飞计算机学院2013-1数据库原理98
2508133192李飞计算机学院2013-1微机原理98
26081331910王静计算机学院2013-3微机原理98
27081331910王静计算机学院2013-3数据库原理98
28081331910王静计算机学院2013-3数据结构78
2908133193李大伟计算机学院2013-2数据结构82
3008133193李大伟计算机学院2013-2数据库原理86
3108133193李大伟计算机学院2013-2微机原理80
3208133198宋小倩计算机学院2013-1微机原理81
3308133198宋小倩计算机学院2013-1数据库原理87
3408133198宋小倩计算机学院2013-1数据结构68
35081331910王静计算机学院2013-3web设计98
36081331910王静计算机学院2013-3C#程序设计98
37081331910王静计算机学院2013-3ASP.net程序设计98
38081331910王静计算机学院2013-3马克思主义98
39081331910王静计算机学院2013-3英语198
40081331910王静计算机学院2013-3高等数学98
41081331910王静计算机学院2013-3概率论98
42081331910王静计算机学院2013-3线性代数98

1、普通查询

查询学生表中所有信息

select * from student;

2、查询特定行

查找姓名为王静的学生的情况

select * from student where name='王静';

3、查询特定列

查询学生姓名为王静的性别(查询结果的 同时显示姓名)

select name,sex from student where name='王静';

4、条件查询

  1. 查找性别为男并且分数大于528分的学生情况。
select * from student where sex='男' and score>528;

  1. 查找姓名不是王静的学生情况。
select * from student where name<>'王静';

  1. 查找分数大于等于545分的学生情况。
select * student where score>545; 

  1. 查找姓名为王静或者姓名为李霞的学生情况。
select * from student where name='王静'

5、模糊查询

  1. 查询姓名中带有"李"字的学生情况。
select * from student where name like '%李%';

  1. 查询姓‘李’且姓名为两个字的学生情况。
select * from student where name like '李_';

6、查询排序

  1. 按分数升序排序
select * from student order by score asc;

  1. 按分数降序排序
select * from student order by score desc;

7、查询分组与统计

  1. 对学生表按班级分组。
select * from student group by class;

  1. 统计每班的学生姓名。
 select class,GROUP_CONCAT(name) from student group by class;

  1. 统计每班的人数。
select class,count(*) from student group by class;


4. 统计每班的总分。

select class,sum(score) from student group by class;

  1. 统计每班的最高分。
select class,max(score) from student group by class;

  1. 统计每班的总分,列出总分超过1100分的班级及其总分。
select class,sum(score) as sum from student group by class having sum(score)>1100;


7. 统计每班的总分,并汇总求和。

select class,sum(score) as sum from student group by class with rollup;

8、多表查询

  1. 在student 表和class_table 表中查询姓名为王静的学生情况。
select student.*, class_table.* from student join class_table on student.name = class_table.name where student.name='王静';

  1. 在student 表和class_table 表中查询姓名相同的所有记录。

左连接

select student.*,class_table.classname,class_table.score from student left join class_table on student.name=class_table.name;

右连接

select class_table.*,student.name,student.sex from class_table right join student on student.name=class_table.name;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小猿--

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值