数据库————Select 查询
时间:2021-07-01 10:21:17
帮助过:3人阅读
--
水果表
create table Fruit
(
Ids varchar(50) primary key,
[Name] varchar(50) not
null,
Price decimal(
8,
2),
Source varchar(50),
Numbers int, --
库存数量
[Image] varchar(50)
)
go
--向表中添加数据
insert into fruit values(‘k001‘,
‘苹果‘,
2.4,
‘烟台‘,
100,
‘image/0.gif‘)
insert into fruit values(‘k002‘,
‘菠萝‘,
1.4,
‘广东‘,
100,
‘image/1.gif‘)
insert into fruit values(‘k003‘,
‘桔子‘,
2.4,
‘福州‘,
100,
‘image/2.gif‘)
insert into fruit values(‘k004‘,
‘葡萄‘,
2.4,
‘新缰‘,
100,
‘image/3.gif‘)
insert into fruit values(‘k005‘,
‘樱桃‘,
2.4,
‘青岛‘,
100,
‘image/4.gif‘)
insert into fruit values(‘k006‘,
‘桃子‘,
2.4,
‘花果山‘,
100,
‘image/5.gif‘)
insert into fruit values(‘k007‘,
‘香蕉‘,
2.4,
‘济南‘,
100,
‘image/6.gif‘)
--
用户表
create table Login
(
UserName varchar(50) primary key, --
用户名
[Name] varchar(50), --
真实姓名
Password varchar(50), --
密码
Account decimal(
18,
2) --
账户余额
)
go
insert into login values(‘zhangsan‘,
‘张三‘,
‘666666‘,
50)
insert into login values(‘lisi‘,
‘李四‘,
‘666666‘,
50)
insert into login values(‘wangwu‘,
‘王五‘,
‘666666‘,
50)
--
订单表
create table Orders
(
Code varchar(50) primary key, --
以“用户名yyyyMMddhhmmssms”的形式作主键
UserName varchar(50) references Login(Username), --
订购人员代号作外键
OrderTime datetime, --
订购时间
)
go
--
订单内容表
create table OrderDetails
(
Ids int identity primary key,
OrderCode varchar(50) references Orders(Code),--
订单号
FruitCode varchar(50) references Fruit(Ids),--
水果代号
[Count] int , --
水果的个数
)
go
select *
from fruit
select *
from login
select *
from orders
select *
from orderdetails
--查询
select *
from Fruit --
查所有
select Name,Source
from Fruit --
查特定列
select Ids
‘代号‘,Name
‘名称‘,Price
‘价格‘,Source
‘产地‘ from Fruit --
修改列名
select *
from Fruit
where Ids=
‘K006‘
select *
from Fruit
where Price=
2.4 and Source=
‘烟台‘ --
查指定行按条件查
select *
from Fruit
where Price between
2.0 and
4.0 --
查指定行按范围查
select *
from Fruit
where Numbers
in (
90,
80,
70)--
查指定行,离散查
select distinct Numbers
from Fruit --
去重查询
select *
from News
select *
from News
where title like
‘%户口‘ --
模糊查询,查以户口结尾的
select *
from News
where title like
‘大熊猫%‘ --
模糊查询,查以大熊猫开头的
select *
from News
where title like
‘%大熊猫%‘ --
模糊查询,查以包含大熊猫的
select *
from News
where title like
‘%外币货_‘--
模糊查询,查外币货之后只有一个字符的
select *
from Fruit order by Numbers asc --
按照Numbers列升序排,如果不加asc默认以升序排
select *
from Fruit order by Numbers desc --
按照Numbers列降序排
select *
from Fruit order by Numbers,Price --
先按照Numbers排,然后再按照Price排
select COUNT(*)
from Fruit --
返回Fruit表里面有多少条数据
select AVG(Numbers)
from Fruit --
返回某一列的平均值
select SUM(Numbers)
from Fruit --
返回某一列的所有数据和
select MAX(Numbers)
from Fruit --
返回某一列中的最大值
select MIN(Numbers)
from Fruit --
返回某一列中的最小值
select *,(Price*
0.8)
as ‘折后价格‘ from Fruit --
加一列数据库中没有的列,这里是加了8折后的价格列
select Numbers, COUNT(*)
from Fruit group by Numbers --
根据某一列分组,求出该组内成员的个数
select Numbers, COUNT(*)
from Fruit group by Numbers having COUNT(*)>
1--根据某一列分组,求出该组内成员的个数,返回成员个数大于1的
%----代表任意多个字符
‘%户口’---以户口结尾,户口前有多个字符
‘户口%’----以户口开头,户口后面有多个字符
‘%户口%‘----包含户口,户口前后有多个字符
‘%外币货_’----外币货后面只有一个字符,之前有多个字符
like----模糊查询
distinct---去除重复
数据库————Select 查询
标签: