找回密码
 立即注册
  • 便民服务
  • 关注我们
  • 社区新手
本帖最后由 成都8班-姜培欢 于 2021-9-22 21:30 编辑

1、查询表中所有数据:select * from student;
2、查询表中的某个字段的值: select name from student;
3、查询表中多个字段的值:select class,name from student;
4、根据条件查询数据:select * from student where class=1833;
5、and和or
select  * from student where class=1834 or class=1833; 查询1834班和1833班的学生信息
select  * from student where class=1834 and sex=’女’; 查询1834班的女生信息
6、!= 和 <> (不等于)
select * from student where class!=1833;
select * from student where class<>1833;
7、between and
select * from grade where math between 60 and 80; 查询数学成绩在60分到80分之间的学生的信息
8、查询某个字段为null的信息
     Select * from student where class is null ;  查询班级为空的学生信息
9、like
select * from student where name like ‘%ao%’;   查询名字中包含‘ao’的学生信息
10、limit
select * from student where id limit 2,8;  查询3-10行数据(2表示第三行的下标是2,8表示取多少行,3到10行一共有8行)
select * from student where id limit 5;  查询前5行数据
11、order by 排序
asc :升序  select name,age from student order by age asc; 查询所有学生的姓名和年龄,并且按照年龄升序排序
desc:降序   select name,age from student order by age desc; 查询所有学生的姓名和年龄,并且按照年龄降序排序
12、count() 函数 :统计函数
select count(*) from student;  查询student表中一共有多少条数据
13、sum() 函数 :求和函数
         select sum(math) from grade;  查询所有学生数学成绩总和
14、avg() 函数:求平均分函数
  select avg(math) from grade; 查询所有学生数学平均分
15、max() 函数 :求最大值
        select max(math) from grade;  查询所有学生中数学的最高分
16、min() 函数 :求最小值
select min(math) from grade;  查询所有学生中数学的最低分
17、distinct() 函数 :去重函数
select distinct(sex) from student; 对sex字段进行去重
18、group by :分组
select class,count(*) from student group by class; 查询每个班的人数
19、having
select class from grade group by class having avg(math)>70; 查询数学平均分大于70分的班级
20、数据库和表的备份
复制表 :like
①create table student2 like student:创建一张和student相同结构的空表
②insert into student2 select * from student:把student中数据插入student2中
③insert into student2(id,name) select id,name from student:选择插入特定的字段
数据库备份:mysqldump
mysqldump -uroot -p123456 dcs8>/dcs8_back; 把dcs8这个数据库备份到根目录下并且命名为dcs8_back
数据库还原:
mysql -uroot -p123456 dcs9<dcs8_back;  把dcs8_back这个备份数据库还原到已经存在的数据库dcs9
21、in 和 not in
①in  :
select * from student where class in(1833,1834); 查询1833班和1834班的学生信息
②not in :
select * from student where class not in (1833,1834); 查询除了1834班和1833班的学生信息

分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册