网站建设哪家有名,阿里云商业网站建设视频,永州网站制作建设,摄影网页设计说明之前学习了怎么创建数据库#xff0c;创建数据表以及给数据表添加数据#xff0c;我们今天就学习一下数据的查询
一. 查询语句的语法
select 列名#xff08;字段名#xff09; form 表名 [where #xff08;查询条件表达式#xff09;] [order by 排序的列明[…之前学习了怎么创建数据库创建数据表以及给数据表添加数据我们今天就学习一下数据的查询
一. 查询语句的语法
select 列名字段名 form 表名 [where 查询条件表达式] [order by 排序的列明[ASC或DESC]]
1.查询所有的行和列
* 代表所有列一般在项目中不允许使用按需搜索
select * from 表名;
2.查询部分列
select 字段1字段2,... from 表名;
3.where 查询条件
select 字段1字段2...from 表名 where 条件;
4.别名 as可以省略不推荐
select 字段1 as 别名1 字段2 as 别名2, .... form 表名;
5.查询null
select * from 表名 where 字段 is null;
6.被清空的列 使用‘’
select * from 表名 where 字段;# 既要查询null的数据也要查询数据被删除的数据select * from student where sex or sex is null ;
7.分页开始数量
注意初始记录行的偏移量从0开始。limit 要放到语句的最后
select * from 表名 limit 开始数显示数量;#查询学生表的第6到第18条数据
select * from student limit 5,13;# 解释 第6条数据起始值从0开始所以是 6-15.# 第6条数据到第18条数据中间有13条数据是最后一条数据减去起始数据1 18-612 113# 所以是 5开始的下标13显示的数量# 查询学生表性别为男生前五个人select * from student where sex男 limit 0,5; 以上是部分查询语法我们练习一下 -- 1.创建表/*Book_type(分类)(序号t_id,类型名称t_name)Book书(序号b_id,书名b_name,价格price,作者author,类型编号t_id,出版时间publication,库存数量num)1)图书分类主键自增,图书主键自增2)添加外键关系
*/# 创建图书分类表
create table book_type(t_id int primary key auto_increment comment序号,t_name varchar(255) comment类型名称
);# 创建图书表
create table book(b_id int primary key auto_increment comment序号,b_name varchar(255) comment书名,price double comment价格,author varchar(255) comment作者,t_id int comment类型编号,foreign key (t_id) REFERENCES book_type(t_id),publication date comment出版时间,num int comment库存数量
);# 注意数据都是模拟的没有特殊含义
-- 2.给book_type添加3条数据insert into book_type(t_name) values(悬疑),(武侠),(散文);-- 3.给book 添加10条数据
insert into book values(null,稻城亚丁,35.00,雪狼子,3,2007-01-01,10),
(null,楚留香传奇,38.00,古龙,2,2005-01-07,20),
(null,陆小凤传奇,50.00,古龙,2,2006-01-02,130),
(null,三体,35.00,刘慈欣,1,2007-01-02,50),
(null,第三命运体,28.90,,2,2008-01-06,70),
(null,时间简史,33.00,古龙,2,2009-02-03,110),
(null,格林童话,23.00,,1,2010-10-01,120),
(null,命运,108.00,张三,1,2011-05-01,130),
(null,球形闪电,99.00,刘慈欣,1,2020-12-12,60),
(null,童话世界,80.00,唐福睿,1,2012-03-25,150)-- 4.查询所有图书类型select * from book_type;-- 5.查询所有的图书
select * from book;-- 6.查询类型编号为1的所有图书
select * from book where t_id1;-- 7.查询作者是:”古龙”的所有图书
select * from book where author古龙;-- 8.查询 出版时间为2020-12的图书select * from book where publication2020-12-01 and publication2020-12-31 ;-- 9.价格在20~50之间的图书
select * from book where price between 20 and 50;-- 10.查询 “格林童话”,”三体”,”时间简史”的详细信息
select * from book where b_name格林童话 or b_name三体 or b_name时间简史 ;-- 11.查询 库存数量为60的图书信息
select * from book where num60;-- 12.查询 没有作者的图书信息
select * from book where author is null or author;-- 13.修该书名为”球形闪电”的图书价格为68元# 修改
update book set price68 where b_name球形闪电;
#查询
select * from book where b_name球形闪电;-- 14.删除 书名为 “童话世界”的图书#删除
delete from book where b_name童话世界;
#查看
select * from book;