关键字查询(含代码)

/**表的简单查询**/
/**
格式:
select * |字段名1[,字段名2,字段名3,...]
from 表名称
[where 条件表达式]
注意:
1.*:表示所有字段名
2.表名称:可以是一张表,也可以是多张表, 多表则用,隔开
3.where 条件表达式:可以没有,则表示查询所有记录

**/

--1.进入Goods库,查询customer表中所有字段信息
use Goods
go
select * from customer

--2.部分字段查询:只查询customer表中的客户姓名,联系电话,地址
select 客户姓名,联系电话,地址 from customer

--3.查询customer表中的地址为"合肥"的信息
select * from customer where 地址='合肥'

--4.插入几条地址相同的记录,地址都是上海
insert into customer values('20180007','TOM','1361236789','上海','tom@163.com')
insert into customer values('20180008','Jack','1387736789','上海','Jack@163.com')
insert into customer values('20180009','John','1399936789','上海','John@163.com')
insert into customer values('20180010','Rose','1369936789','上海','Rose@163.com')
select * from customer

--5.查询customer表中的地址为"上海"的信息
select * from customer where 地址='上海'

--6.查询customer表中的所有地址信息
select 地址 from customer

/**
关键字:distinct用法

格式:select distinct *|字段名[,字段名,...]
作用:去除重复记录
**/

--7.查询customer表中的所有地址信息,去除重复的地址,即:地址相同的只显示一次,用distinct:去除显示字段全部重复的记录
select distinct 地址 from customer
select distinct 地址,客户姓名 from customer /*去除重复的地址、客户姓名完全重复的记录*/

--8.查询stuinfo表中姓名为张三的所有记录,去除重复的记录
select distinct stuname,sex from stuinfo where stuname='张三'

/**
关键字:top用法

格式 :select top n|n percent *|字段名[,字段名,...]
作用:查询前n条或总记录数的前百分n条记录

**/

--8.查询customer表中前三条记录
select top 3 * from customer


--9.查询customer表中前50%记录
select top 50 percent * from customer

--10.查询customer表中地址为"上海"的前三条记录
select top 3 * from customer where 地址='上海'

/*
关键字:between...and...的用法

格式:where 字段名 [not] between 低值 and 高值
含义:查询出字段名大于等于低值且小于等于高值的记录,加上not,表示不在[低值,高值]之间的记录
*/


--1.在Goods数据库中创建一个库存信息表
use Goods
go
create table 库存信息表(
库存编号 smallint not null,
商品编号 char(8) not null,
库存数量 smallint
)

--2.库存信息表向插入多条记录
use Goods
go
insert into 库存信息表 values(1,'11110001',160)
insert into 库存信息表 values(2,'11110002',60)
insert into 库存信息表 values(3,'11110003',210)
insert into 库存信息表 values(4,'11110004',90)
insert into 库存信息表 values(5,'11110005',300)
insert into 库存信息表 values(6,'11110006',196)
insert into 库存信息表 values(7,'11110007',500)
insert into 库存信息表 values(8,'11110008',420)
insert into 库存信息表 values(9,'11110009',189)
insert into 库存信息表 values(10,'11110010',600)

select * from 库存信息表


--3.查询库存信息表中库存数量在100到300之间(含100,300)
select * from 库存信息表 where 库存数量 between 100 and 300

--4.查询库存信息表中库存数量在100到300之间(用比较运算符)
select * from 库存信息表 where 库存数量 >=100 and 库存数量<=300

--5.查询库存信息表中库存数量不在100到300之间(含100,300)(between ...and...)
select * from 库存信息表 where 库存数量 not between 100 and 300

--6.查询库存信息表中库存数量不在100到300之间(用比较运算符)
select * from 库存信息表 where 库存数量 <100 or 库存数量>300
select * from 库存信息表 where not(库存数量 >=100 and 库存数量<=300)


--7.查询出进货信息表中进货数量在[50,100]的所有信息
select * from 进货信息表 where 进货数量 between 50 and 100


/*
关键字:in 的用法

格式:where 字段名 [not] in(值1,值2,值3,...)
含义:用来查询出指定字段名的值在(值1,值2,值3,...)范围中的记录,加上not 则取反
*/

--1. 查询出库存信息表中库存数量是300,500,600的记录
select * from 库存信息表 where 库存数量 in (300,500,600)
select * from 库存信息表 where 库存数量 =300 or 库存数量 =500 or 库存数量 =600

--2. 查询出库存信息表中库存数量不是300,500,600的记录
select * from 库存信息表 where 库存数量 not in (300,500,600)


--3. 查询出customer表中地址是上海,北京,合肥的记录
select * from customer where 地址 in('上海','北京','合肥')

--4. 查询出customer表中地址不是上海,北京,合肥的记录
select * from customer where 地址 not in('上海','北京','合肥')


/*
关键字:like 用法

格式:字段名 [not] like '<字符表达式>'
作用:查询出字段名符合或不符合like后面字符表达式的所有记录
注意事项:
1)字符表达式可以是完整字符也可含有通配符
2)_:下划线,仅表示一个字符,也可以是0个
3)%:百分号,表示若干个字符,也可以是0个
4)[]:表示符合方括号内任意一个字符
4)[^]:表示匹配不在方括号取值范围内的字符
*/

--1.查询customer表中所有姓'赵'客户信息,查询前再添加几条记录,含有姓赵的
select * from customer
select * from customer where 客户姓名 like '赵%'
select * from customer where 客户姓名 like '赵__' /*三个字姓赵的*/
select * from customer where 客户姓名 like '赵_' /*两个字姓赵的记录*/


--2.查询customer表中客户姓名第二个字为"五"的所有信息
select * from customer where 客户姓名 like '_五%'

--3.查询customer表中所有姓"李"和姓"赵"的客户信息
select * from customer where 客户姓名 like '李%' or 客户姓名 like '赵%'


--4.查询出姓名最后一个字在‘三、四、五、六、七、八’中任意一个的所有记录
select * from customer where 客户姓名 like '%[三,四,五,六,七,八]'

--5.查询出姓名最后一个字不在‘三、四、五、六、七、八’中任意一个的所有记录
select * from customer where 客户姓名 like '%[^三,四,五,六,七,八]'
select * from customer where 客户姓名 not like '%[三,四,五,六,七,八]'

--6.查询出电话是139开头的所有记录
select * from customer where 联系电话 like '139%'
select * from customer where 联系电话 like '139[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'


/*
关键字:is null 用法

格式:字段名 is [not] null
作用:查询出字段的值是否为空
注意事项:
1)is 不能用=代替
2)null不能用0或空代替
*/
--1.查询出customer表中email为空的所有记录,之前先插入几个email为空的记录
select * from customer where email is null

--2.查询出customer表中email为非空的所有记录,
select * from customer where email is not null;

本文章归作者所有侵权必究 若作者侵权联系删除。
原文地址:https://www.cnblogs.com/woniu11/p/13780364.html