目录
2.带有between .. and .. 关键字的条件数据查询
一、向表中增、删、更新数据
①新增数据
不同的数据之间用逗号隔开
#一条
insert into table_name (field1,field2,field3,...) values (value1,value2,value3,..) ;
#多条
insert into table_name (field1,field2,field3,...) values (value11,value12,value13,..),(value21,value22,value23,..),...;
insert into commodity (c_type,c_name) values (1,'玩具');
insert into commodity (c_type,c_name) values (2,'书籍'),(3,'文具');
在新增数据时:
数据表约束 | 数据输入 |
defalut '100' | default |
可用空(即无not null 设置) | null |
②删除数据
#删除整张表的数据(这里仅删除全部数据,表结构依然存在)
delete from table_name;
#按条件删除
delete from table_name where ... ;
delete from commodity where c_type=1;
delete from commodity;
③修改/更新数据
update table_name set field1=value1,field2=value2 ... where ...;
update commodity set c_name='文具' where c_type=2;
如果没有where,则相当于把整个字段c_name都设置成'文具' ,即该字段全部设定成相同的值
二、查看、筛选、打印数据
select(SQL)==print(Python)
①简单查看select ... from..
#查看表中所有数据
select * from table_name;
#查看特定数据
select c_type,c_name from commodity;
#给数据列取别名
select c_type as 商品类型, c_name as 商品名称 from commodity;
②非重复查看-distinct
#查看单一字段的取值
select distinct field from table_name;
#去掉重复
select distinct field1,field2 from table_name;
③条件查看-where的使用
用到where的地方都能按照下面的方式设置条件
1.带有关系运算符和逻辑运算符的条件数据查询
#查询进价大于100的商品
select c_name,c_inprice from commodity where c_inprice>100;
#查询进价大于100的玩具
select c_name,c_inprice from commodity where c_inprice>100 and c_type=1;
注意:一个字段不能同时为2个值
#输出所有玩具和书籍商品
select c_name,c_type from commodity where c_type=1 and c_type=2; ×
select c_name,c_type from commodity where c_type=1 or c_type=2; √
2.带有between .. and .. 关键字的条件数据查询
between 10 and 100,包括10和100,查询一个范围
#进价在10到100之间的商品
select c_name,c_inprice from commodity where c_inprice between 10 and 100;
#上面等价于
select c_name,c_inprice from commodity where c_inprice>=10 and c_inprice<=100;
#not between and
select c_name,c_inprice from commodity where c_inprice not between 10 and 100;
#上面等价于
select c_name,c_inprice from commodity where c_inprice<10 and c_inprice>100;
3.带有is null关键字的条件数据查询
is null是用来判断为空的关键字,即找出为空的数据;大多数是输出不为空的数据:is not null
select c_name,c_outprice from commodity where c_outprice is not null;
4.带有in关键字的条件数据查询
in关键词是判断数据是某个或者某几个具体的数字
#进价是10或者是20或者是30或者是40的商品
select c_name,c_outprice from commodity where c_outprice in (10,20,30,40);
#进价不是10且不是20且不是30且不是40的商品
select c_name,c_outprice from commodity where c_outprice not in (10,20,30,40);
5.带有like关键字的条件数据查询
like==像,用于模糊查询,与通配符配合使用
% 任意多个任意字符;_ 任意一个字符
#查询所有带"玩具"两个字的商品名称
select c_name from commodity where c_name like "%玩具%";
整理
#数据操作
#增
insert into table_name (field1,field2,...) values (value1,value2,...);
#删
delete from table_name where field1=value1,field2=value2,...;
#更新
update table_name set field1=value1 where field2=values;
#数据查看
#简单查看
select * from table_name;
select c_type,c_name from commodity;
select c_type as 商品类型, c_name as 商品名称 from commodity;
#非重复查看
select distinct field from table_name;
select distinct field1,field2 from table_name;
#条件查看
select c_name,c_inprice from commodity where c_inprice>100 and c_type=1;
select c_name,c_inprice from commodity where c_inprice between 10 and 100;
select c_name,c_outprice from commodity where c_outprice is not null;
select c_name,c_outprice from commodity where c_outprice in (10,20,30,40);
select c_name from commodity where c_name like "%玩具%";
三、正则表达式完成字符串匹配查询
正则表达式是匹配文本的特殊字符串
正则表达式等价于使用通配符的like,如果like不使用通配符结合查找,则like等于=;
[0-9]=0 or 1 or .... or 9
#匹配c_name中有字母a的数据
select c_name from commodity where c_name like '%a%';
select c_name from commodity where c_name regexp 'a';
#查找c_name中有a或者玩的商品
select c_name from commodity where c_name like '%a%' or c_name like '%玩%';
select c_name from commodity where c_name regexp 'a|玩';
#查看a..具,或者玩..具
select c_name from commodity where c_name like '%a%具%' or c_name like '%玩%具%';
select c_name from commodity where c_name regexp '[a|玩]具';
四、order by
order by的作用是根据指定字段进行排序输出
①从小到大(default=acs)
select c_name,c_inprice from commodity order by c_inprice;
select c_name,c_inprice from commodity order by c_inprice asc;
②逆序(desc)
select c_name,c_inprice from commodity order by c_inprice desc;
③order by与where一起使用
where必须写在order by前面
#非空数据逆序输出
select c_name,c_outprice from commodity
where c_outprice is not null
order by c_ourprice desc;
④实现多个字段排序
字段从左往右,即先按照左边的字段排,左边字段相同,再按照右边的字段排序
select c_name,c_inprice,c_outprice from commodity
where c_outprice is not null
order by c_inprice,c_outprice;
五、limit
limit用于限制输出的条目数
①输出xx条数据
select * from commodity limit 10;
②orderby和limit结合使用,实现排行榜功能
1.输出前5名
limit n--输出n个数据
#按顺序输出进价最贵的5件商品
select c_name,c_inprice from commodity
order by c_inprice desc
limit 5;
2.输出第6到10名
limit n,m n--从第n条数据开始,n从0开始计数,比如第6条数据,这里n为5;
m--输出几条数据,这里6-10名,共5条数据
#输出进价最贵的第6到第10个商品
select c_name,c_inprice from commodity
order by c_inprice desc
limit 5,5;