一、在数据库中创建一个表格,语法是:
---------创建表
create table T_Student
(Stu_Id int primary key identity,//将id设为主键,并且是自动增加,所以在用操作语句加入数据时不用加入id.
Stu_Name varchar(16) not null,
Stu_Mobile varchar(16) not null,
Stu_Address varchar(32) not null,
Stu_Age int not null,
Stu_Email varchar(32) not null
)
---------向数据库中插入数据句
insert into T_Student values('张三','1234556','河北保定市',21,'12335465@qq.com')
insert into T_Student values('刘丽','7654334','保定',20,'123456788@qq.com')
insert into T_Student values('小红','32354657','河北保定',19,'76543234@qq.com')
insert into T_Student values('李四','7644356','河北邢台',22,'1287689@qq.com')
insert into T_Student values('李宁','0978675','河北邯郸市',20,'65789043@qq.com')
insert into T_Student values('张娃','3465768','邢台',19,'45678432@qq.com')
insert into T_Student values('杜天','8754535','保定满城',21,'324578898@qq.com')
---------查询数据库中的数据
*表示要查询所有列
select * from T_Student
--查询所有人的姓名信息
select Stu_Name from T_Student
--条件查询//where可以进行条件查询
select Stu_Mobile,Stu_Address from T_Student WHERE Stu_Name='杜天';
--查询表中年龄大于21岁的所有人姓名,手机号和地址
select Stu_Name,Stu_Age,Stu_Mobile,Stu_Address from T_Student where Stu_Age>21;
--查询表中年龄小于21岁(包括21岁)的所有人姓名,手机号和地址
select Stu_Name,Stu_Age,Stu_Mobile,Stu_Address from T_Student where Stu_Age<=21;
select Stu_Name,Stu_Age,Stu_Mobile,Stu_Address from T_Student where Stu_Age!>21;
--查询表中年龄不等于21岁的所有人姓名,手机号和地址
select Stu_Name,Stu_Age,Stu_Mobile,Stu_Address from T_Student where Stu_Age!=21;
select Stu_Name,Stu_Age,Stu_Mobile,Stu_Address from T_Student where Stu_Age<>21;
--查询表中年龄=21岁和25岁的人的信息
select * from T_Student where Stu_Age=21 or Stu_Age=25;
--查询表中年龄在19到22岁之间的人的信息(包括19和22)
select * from T_Student where Stu_Age between 19 and 22;
--另一种写法
select * from T_Student where Stu_Age>=19 and Stu_Age<=22;
--查询表中年龄在21到25之间的信息
select * from T_Student where Stu_Age between 21 and 25 and Stu_Address='河北省保定市';
select * from T_Student where Stu_Age in(21,23,25);
----不在21到25之间的信息
select * from T_Student where Stu_Age not in(21,23,25);
二、一些重要的方法
1.通配符
%:表示任意多个字符
_:表示1个字符
[charlist]:多个字符列表中的任意一个
[^charlist]或[!charlist]:不是多个字符列表中的任意一个
例如:
select * from T_Student where Stu_Address like '%保定%'//表示只要地址中包含保定即可,保定字前字后可以有多个字符。
--"保定"前不能有任何字符,也就是说以“保定”开头,并且“保定”后面只能有一个字符
select * from T_Student where Stu_Address like '保定_'
--没有符合“查询值前面只有一个字符,且后面也只有一个字符”条件的记录
select * from T_Student where Stu_Address like '_保定_'
--当使用_符号时,表示必须条件,就是记录值中必须包含制定数量的字符数(有几个_,就表示必须有几个字符),不象%,可以有0个,1个,或任意多个
select * from T_Student where Stu_Address like '___保定_'
--查询表中所有姓"王"的同学信息
select * from T_Student where Stu_Name like '张%'
--查询表中名字的最后一个字符为"阳"的同学信息
select * from T_Student where Stu_Name like '%天'
--查询表中姓"李","张","刘"的同学信息
select * from T_Student where Stu_Name like '李%' or Stu_Name like '张%' or Stu_Name like '刘%'
select * from T_Student where Stu_Name like '[王张刘]%'//[ ]表示多个字符列中的任意一个
--查询表中除姓"王","张","刘"的同学信息
select * from T_Student where Stu_Name not like '王%' and Stu_Name not like '张%' and Stu_Name not like '刘%'
--有时候对不同的数据库 !和 ^ 不一定都管用
select * from T_Student where Stu_Name like '[!王张刘]%' // !号表示不包含
select * from T_Student where Stu_Name like '[^王张刘]%' // ^号表示不包含
--去除重复
select distinct Stu_Name from T_Student
三、分组查询
create table T_Product
(
P_Id int primary key identity,
p_Name varchar(32) not null
)
create table T_Order
(
O_Id int primary key identity,
P_Id int not null,
O_Numb int not null,
O_Date date not null
)
insert into T_Product(p_Name) values('螺丝'),('钉子'),('漏电保护器'),('有机玻璃')
insert into T_Order(P_Id,O_Numb,O_Date) values(2,1500,'2011-02-01'),(1,2000,'2011-02-02'),
(2,1600,'2011-03-01'),(3,3000,'2012-01-01'),(1,1300,'2012-02-01'),(3,4000,'2012-01-07'),
(2,2000,'2011-03-01'),(1,1030,'2011-02-02'),(4,2200,'2013-05-01')
select SUM(O_Numb) from T_Order
--查询每种商品的订单总量
--需要先分组,再计算每组的订单数量总和
//查询语句中查询的字段必须是group by 后的字段,或者是聚合函数
select P_Id, SUM(O_Numb) from T_Order group by(P_Id)
--查询每种商品的订单的平均数量
select P_Id, AVG(O_Numb) from T_Order group by(P_Id)
--查询每种商品的最大一笔订单的数量
select P_Id, MAX(O_Numb) from T_Order group by(P_Id)
--查询每种商品的最小的一笔订单数量
select P_Id, MIN(O_Numb) from T_Order group by(P_Id)
--查询每种商品各有几笔订单
select P_Id, count(*) from T_Order group by(P_Id)
--查询订单总量大于5000的商品信息
select P_Id,SUM(O_Numb) from T_Order group by(P_Id) having(SUM(O_Numb)>5000)
select * from (select P_Id,SUM(O_Numb) as sumnumb from T_Order group by(P_Id) having(SUM(O_Numb)>5000))t
where t.sumnumb>=7000
--where子句实在分组之前对数据进行过滤;如果想对分组之后的结果进行过滤,就只能使用having
select P_Id,SUM(O_Numb) from T_Order where P_Id=2 group by(P_Id) having(SUM(O_Numb)>5000)
四、连接查询
select p.P_Id,p_Name,o.O_Numb from T_Product p inner join T_Order o on p.P_Id=o.P_Id
执行顺序
1)执行 on p.P_Id=o.P_Id,会用左表中每一行的p_id和右表中的每一行的p_id进行比较,如果相等,就查询p.P_Id,p_Name,o.O_Numb这三个字段,并列出来
select p.P_Id,p_Name,o.O_Numb from T_Product p inner join T_Order o on p.P_Id=o.P_Id
执行结果如下:
其它连接查询:
insert into T_Product values('黑板');
select p.P_Id,p_Name,o.O_Numb from T_Product p left join T_Order o on p.P_Id=o.P_Id//左外连接查询结果
执行结果是:
insert into T_Order values(7,2000,'2012-09-08');
select p.P_Id,p_Name,o.O_Numb from T_Product p right join T_Order o on p.P_Id=o.P_Id//右外连接方式
执行结果是:
select p.P_Id,p_Name,o.O_Numb from T_Product p full join T_Order o on p.P_Id=o.P_Id//完全外连接
执行结果是: