5.数据行级别SQL语句
增
insert into 表名(列名,列名) values(数据);
增加一行数据
insert into tb1(name,age) values('alex', 18);
insert into 表名(列名,列名) values(数据),(数据)(数据);
增加多行数据
insert into tb1(name,age) values('alex', 18),('eric', 19),('eric', 19);
insert into 表名(列名,列名) select 列名,列名 from 表名;
用于将一个表中某些列的数据增加到另一个列中。如果涉及的两列的数据类型不相同,能强转的的就转换后再添加,无法强转的就报错
insert into tb2(capition,class) select name,age from tb1;
删
delete from 表名 ;
清空表
delete from 表名 where 条件;
删除符合条件的数据
selete from tb2 where nid=1 and class=12;
改
update 表名 set 列名=值;
将表中的某一列的值全部修改
update 表名 set 列名=值 where 条件;
将符合条件的一行数据的某一列值进行修改
update 表名 set 列名=值,列名=值 where 条件
将符合条件的一行数据的某些列值进行修改
查
select * from 表名;
查找表中的所有数据
select * from 表名 where 条件;
查找表中符合条件的行的所有数据
select 列名 from 表名 where 条件;
查找表中符合条件的行中的某一列数据
select nid,age from tb1 where name='tom';
条件的形式:
or and not in
id between 5 and 16 id in (11,22,33) id not in (11,22,33) id in (select nid from 表)
通配符like
用来模糊搜索,
%匹配任意长度的字符 _匹配一个长度的字符
like 'xxx%' 以xxx开头的搜索条件
like '%xxx' 以xxx结尾的搜索条件
select * from 表 where name like 'alex%'; #alex开头的所有(只要开头是alex,无论有多少字符串再alex后面) select * from 表 where name like 'alex_'; #以alex为来头,且总字符串只能为5个的数据
分页limit
如果查找的数据过多,同时显示太多数据,会让系统死机,因此要将数据分页打印出来。来限制打印出的数据的长度。
select * from 表名 limit num; 打印表中的前num行数据
select * from 表名 limit a,num; 从第a行开始,取num行数据
select * from 表名 limit num offset a; 从第a行开始,取num行数据
select * from tb1 limit 3,5; #从第三行开始取5条数据 select * from tb1 limit 3 offset 1; #从第一行数据开始取3条数据
排序order by
限制查找出数据的排列顺序
asc 从小到大 desc 从大到小
select * from 表名 order by 列名 asc; 根据某一列从小到大排
select * from 表名 order by 列名 desc; 根据某一列从大到小排
elect * from 表名 order by 列名 desc/asc,列名 desc/asc; 在第一个排序的基础上,果有些值相等,就将小范围的数值按照第二种再排一次
select * from tb1 order by nid desc; #根据nid的值从大到小排列 select * from tb1 order by name desc,nid asc;# 首先将数据按照名称从大到小排列,如果有的数据名称相同,就将名称相同的数据按照nid 从大到小排列
分组 group by
select * from 表名 group by 列名; 将数据库按照某一列进行分组,值相同的为一组,压缩成一条数据,
select sun(a) 表名 group by 列名;按照某一列进行分组,显示出一组数据中a列的和
select count(nid) 表名 group by 列名;按照某一列进行分组,显示出一组数据中nid的总数
相似的语句还有max min
select class,count(student_num) from userinfo group by class; 查找每个班级中有几个学生 (按照班级进行分组,一个班级的为一组,显示班级和每组中有几个学生) select class as a,count(student_num) as b from userinfo group by class; 与上面语句作用相同,不同的是上面语句的表头为class,count(student_num),该语句的表头为a,b
as起重命名表头作用
注意:count max min 类似的方法名为聚合函数,使用聚合函数时,不能使用关键字where
因此,要对使用聚合函数的语句添加条件筛选,不能使用where,要使用having
select class,count(student_num) from userinfo group by class having count(student_num)=2; 按照class进行分组,计算出每个class有多少人,如果人数等于2就显示出来
连表操作
数据库中数据表中通常是使用外键连接在一起的,使用连表操作,可以将使用外键相连的数据表的内容显示出来。
select * form a,b where a.x = b.o;
select student_num,age,class from userinfo,class_info where userinfo.class=class_info.nid; #连表操作,将userinfo.class与class_info.nid相对于的形式将两个表打印出来
这种方式的连表,只是将表格的数据查找出来,没有将相连的部分放在一起。
left join
select * from a LEFT JOIN b ON a.x = b.o;
谁在前面,谁就是主表,要将数据全部显示出来,第一张表需要什么数据,第二张表的数据才能显示出来
如果主表的某个数据在第二个表中没有匹配值,就会出现null
select * from userinfo left outer join class_info on userinfo.class=class_info.nid; 连表操作,将userinfo.class与class_info.nid相对于的形式将两个表打印出来
inner join
永远不会出现Null
select * from a inner JOIN b ON a.x = b.o
注意:
1.如果一个表格有多个外键,想要同时显示多个外键的内容,就将其他外键的连表操作写在下方就可。
如下,显示两个外键的内容 SELECT * FROM score LEFT JOIN student ON student.sid = score.student_id INNER JOIN course ON course.cid = score.course_id;
2.如果只想显示与外键有关的列的内容,就要修改*为想要显示的列;
3.如果有列名相同,就要在其前方加入表名,还能自己调整显示的数据
SELECT score.sid, student_id, sname, course_id, cname, number FROM score LEFT JOIN student ON student.sid = score.student_id INNER JOIN course ON course.cid = score.course_id;

浙公网安备 33010602011771号