mysql 之增删改查(一)

来源:互联网 发布:大数据在教育行业应用 编辑:程序博客网 时间:2024/05/18 03:27

高薪:数据库优化,线程,算法,框架源码,设计模式

pagesize:页面大小(每页显示的数据条数)
pageno:当前页(页码)

SELECT <字段名列表>FROM <表名或视图>[WHERE <查询条件>][GROUP BY <分组的字段名>]

[ORDER BY <排序的列名>[ASC 或 DESC]]

[LIMIT (pageno-1)*pagesize,pagesize];

5.切换数据库
use holly;

6.查看数据库表
show tables;

7.查看表结构
desc student;

8.添加数据

insert into student(sname,sex,age,studytime,subject,score) values('超群','女',18,'2017-01-01','java',85.6);insert into student(sname,sex,age,studytime,subject,score) values('晶晶','女',18,'2017-01-02','sql',96),('奔奔','男',28,'2017-01-03','sql',96);commit;

9.查看数据

select * from student;

+—–+——-+——+——+————+———+——-+
| sid | sname | sex | age | studytime | subject | score |
+—–+——-+——+——+————+———+——-+
| 1 | holly | ? | 18 | 2010-05-06 | java | 90.00 |
| 2 | 超群 | 女 | 18 | 2017-01-01 | java | 85.60 |
| 3 | 晶晶 | 女 | 18 | 2017-01-02 | sql | 96.00 |
| 4 | 奔奔 | 男 | 28 | 2017-01-03 | sql | 96.00 |
+—–+——-+——+——+————+———+——-+

10.修改数据

update student set sex='女' where sid=1;commit;

11.查看数据
select * from student;
+—–+——-+——+——+————+———+——-+
| sid | sname | sex | age | studytime | subject | score |
+—–+——-+——+——+————+———+——-+
| 1 | holly | 女 | 18 | 2010-05-06 | java | 90.00 |
| 2 | 超群 | 女 | 18 | 2017-01-01 | java | 85.60 |
| 3 | 晶晶 | 女 | 18 | 2017-01-02 | sql | 96.00 |
| 4 | 奔奔 | 男 | 28 | 2017-01-03 | sql | 96.00 |
+—–+——-+——+——+————+———+——-+

12.复制表结构(列名)和表数据:不复制序列和主外键

 create table newstudent(select * from student);

13.删除某一条表数据:序列不变

delete from student where sid=4;commit;

14.查看数据

select * from student;

+—–+——-+——+——+————+———+——-+
| sid | sname | sex | age | studytime | subject | score |
+—–+——-+——+——+————+———+——-+
| 1 | holly | 女 | 18 | 2010-05-06 | java | 90.00 |
| 2 | 超群 | 女 | 18 | 2017-01-01 | java | 85.60 |
| 3 | 晶晶 | 女 | 18 | 2017-01-02 | sql | 96.00 |

15.插入数据

insert into student(sname,sex,age,studytime,subject,score) values('奔奔','女',18,'2017-01-01','java',85.6);commit;
  1. 查看数据库表
select * from student;

+—–+——-+——+——+————+———+——-+
| sid | sname | sex | age | studytime | subject | score |
+—–+——-+——+——+————+———+——-+
| 1 | holly | 女 | 18 | 2010-05-06 | java | 90.00 |
| 2 | 超群 | 女 | 18 | 2017-01-01 | java | 85.60 |
| 3 | 晶晶 | 女 | 18 | 2017-01-02 | sql | 96.00 |
| 5 | 奔奔 | 男 | 28 | 2017-01-03 | sql | 96.00 |
+—–+——-+——+——+————+———+——-+

–18.删除数据表

drop table student;

–19.如果有两个表,这个两个表有关联,先查找一方和多方,分清主表,从表
– 一方:主表,多方:从表
– 创建表时创建主表再创建从表
–创建主表classes表

create table classes(  cid int unsigned auto_increment primary key,  cname varchar(20) not null);insert into classes(cname) values('TB113'),('TB116');commit;

–创建从表student表

create table student(  sid int unsigned auto_increment primary key,  sname varchar(20) unique,  sex char(4) default '男',  age int(10) default 18,  studytime date,  cid int references classes(cid));insert into student(sname,studytime,cid) values('张三','2017-01-01',1),('李四','2017-01-02',2),('魏巍','2017-01-03',2),('王五','2017-01-04',2),('王二','2017-01-05',2);commit;

–20.两边联查加别名

select s.cid scid,c.cid as ccid from student s,classes c where c.cid=s.cid;

+——+——+
| scid | ccid |
+——+——+
| 1 | 1 |
| 2 | 2 |
| 2 | 2 |
| 2 | 2 |
| 2 | 2 |
+——+——+

21.统计所有的学生人数

select count(*) from student;select count(1) from student;

22.获取最大的学生编号

select max(sid) from student;

23.获取最小的学生编号

select min(sid) from student;

23.获取编号的平局值

select avg(sid) from student;

24.获取年龄值的总和

select sum(age) from student;

25.排序升序

select cid from classes order by cid;select cid from classes order by cid asc;

25.排序降序

select cid from classes order by cid desc;

26.按照cid分组分组(只能显示分组字段和聚合函数)
–统计每个班级下的学生人数

select cid,count(cid) from student group by cid;

+——+————+
| cid | count(cid) |
+——+————+
| 1 | 1 |
| 2 | 4 |
+——+————+

–对分组再次筛选建议使用having 而不是where

select cid,count(cid) from student group by cid having cid>1;

27.先分组再排序

select cid,count(cid) from student group by cidorder by cid desc;

28.分页查询,每页显示3条数据,查询第一页,第二页

select * from student limit 0,3;

+—–+——-+——+——+————+——+
| sid | sname | sex | age | studytime | cid |
+—–+——-+——+——+————+——+
| 1 | 张三 | 男 | 18 | 2017-01-01 | 1 |
| 2 | 李四 | 男 | 18 | 2017-01-02 | 2 |
| 3 | 魏巍 | 男 | 18 | 2017-01-03 | 2 |
+—–+——-+——+——+————+——+

select * from student limit 3,3;

+—–+——-+——+——+————+——+
| sid | sname | sex | age | studytime | cid |
+—–+——-+——+——+————+——+
| 4 | 王五 | 男 | 18 | 2017-01-04 | 2 |
| 5 | 王二 | 男 | 18 | 2017-01-05 | 2 |
+—–+——-+——+——+————+——+

–29.查询班级编号为2的第一页数据,每页显示3条数,

select * from student where cid=2 limit 0,3;

–30.查询和李四同班(cid一样)的所有的学生信息
–查询李四同学的班级编号

select cid from student where sname='李四'

–查询和李四同班的所有的学生信息

select * from student where cid=(select cid from student where sname='李四');

–31.统计和李四同班的所有的学生人数

select count(*) from student where cid=(select cid from student where sname='李四');

–32.查询学生编号在1,3,5,7,9范围内的学生信息
–in返回范围内存在的数据,

select * from student where sid in(1,3,5,7,9);

–33.查询班级编号为2,并且姓名存在与所有学生姓名中的学生信息

select * from student where cid=2 and sname in (select sname from student);

–34 查询学生编号不在1,3,5,7,9范围内的学生信息

select * from student where sid not in(1,3,5,7,9);