您的位置:首页 > 数据库

Sql server 查询语句详写

2013-09-12 20:48 134 查看
在Sql server中查询语句是比较难学习的一块,在查询语句中会涉及到一些函数和几个表的链接。我们用一些代码详细了解一下select的用法。

1、select * from 表名 最简单的查询语句 例如:select * from Seller

2、select 字段名 from 表名 查询所要查询字段的语句

3、在查询时还可以查询计算出的结果,比如在数据库product表中存有商品的单价为price 库存为 number,如果要查询总价可以这样写:

select prize*number as 总价 from product ,as 总价 是为查询出来的结果写一个别名,也可以不要as,select prize*number 总价 from product,也可以用别名等于结果:select 总价=prize*number from product。

4、查询语句里面用到的一些函数

DATEDIFF(YEAR,Birthday ,GETDATE()) from Seller 这个函数datediff()求两个日期之间的时间。如在数据库Seller表中有生日Birthday,需要求出这个人的年龄,就可以用这个函数,例如:datediff(year,Birthday,getdate()) from Seller ,其中getdate()函数表示的是当前日期。year那儿也可以换成month,day,这样就可以求出两个日期之间的月数或天数了。

avg(字段或表达式) 求平均数函数

max(字段或表达式)求最大值函数

min(字段或表达式)求最小值函数

sum()求和函数,求某个字段的值的总和。比如:在product表中有商品数number求商品总数:select SUM(number) from product 。

count()统计表某字段的行数。 例如:在product表中number字段有十行select count(number) from product。

5、查询条件

where ,between and ,in(),

例如:查出表product编号为5的产品数量number select number from product where id=5;

查出表product编号在5到10之间产品的所有信息 select * from product where id between 5 and 10

查出表product编号是5,20,15的产品的所有信息 select * from product where id in(5,20,15)

查出表product编号不在5,20,15的产品的所有信息 select * from product where id not in(5,20,15)

模糊查询

使用LIKE关键字进行模糊查询

使用四种匹配符:%、_、[]、[^]。

like 'aa%' 查询以字母aa开头的所有字符串

like '%bc' 查询以bc结尾的所有字符串

like '%ir%' 查询中间有ir的所有字符串

like‘_mt’ 查询结尾为mt的三个字的字符串 例如:select name from student where name like '_鹏飞' 姓名是张鹏飞,李鹏飞,孙鹏飞的都可以被查出来,但必须是三个字的。再例如:select name from student where name like '张_' ,查询所有姓张的两个字的人名。 若是like '张_
_'就是查询姓张的三个字的人名。

like'[bc]%' 查询开头是b或c的所有字符串

like'[a-f]air' 查询以a~f任意一个字母开头,以air结尾的所有数据

like ‘B[^a]%' 以字母B开头,第二个字母不为a的所有数据

如果用户要查询的匹配字符串本身就含有“%”或“-”,比如要查找名字为“佳能XS200_IS”的产品信息,这时就要使用“ESCAPE”关键字对通配符进行转义

select * from Reader where readerName like '李_' 两句代码的区别就是第一句查出姓李的另个字的写姓名,第二句查询叫李_的人的信息。
select * fromReader where readerName like '李\_' escape '\'

6、group by 句子的使用

包含group by
子句的查询语句的格式:(注意,各个子句的位置)

select column_name1[,…n]from table_namewhere search_conditiongroup
by colum_name1[,…n][ having search_condition ][order by 字段 asc/desc]

group by 字段名 根据需要分组的字段名分组,查询分组后的部分或全部数据,having 是把查询出来的结果集根据having条件再次分组,与wehere的分组时不一样的,where是在数据库里面分组。having子句是配合group by子句使用的。后面的 order by 是排序,asc/desc,asc表示升序,默认为asc,desc表示降序,这个也是将查询出来的结果集进行排序,不影响数据库里面的数据。

7、order by的使用

关键字ASC表示按升序排列,可省略;关键字DESC表示按降序排列

每个排序字段的后面都应给出自己的排序关键字,可以省略。但不要理解为多个字段可以使用同一个排序关键字。如,order by字段1,字段2
desc 该语句的意思其实是指,字段1按升序排序,字段2按降序排序。

8、 into子句的使用

可以在select语句中使用into子句将查询的结果存入一个新建的表中

select *|字段列表 into 新表 from 已有的表 where 查询条件

9、两张表及多张表的查询。

内连接:

from 表1 inner join 表2 on 表1.公共字段=表2.公共字段 其中表1是链接的第一个表,表2是链接的第二个表,表1和表2存在一个表的主键是另一个表的外键的关系。主键和外键是公共字段,内连接用inner join 来链接两张表,on关键字后面写两张表的公共字段,多张表也是这样查询。

外连接:

左外连接:left outer join 左外连接是显示左表中的所有行,和右表中的与左表匹配的行

select .productid,productname,price,orderid,quantityfrom product P left outer join
orderdetail O on P.productid=O.productid

用左外链接查询product表和orderdetail表的productid,productname,price,orderid,quantity字段的信息。

右外连接:right outer join 内连接的结果+右表中的找不到匹配的剩余行

select productid,productname,price,orderid,quantity from product P right ourter join orderdetail O on P.productid=O.productid

用右外连接查询product表和orderdetail表的productid,productname,price,orderid,quantity字段的信息。

完全外连接:full outer join 内连接的结果+左表中的找不到匹配的剩余行+右表中的找不到匹配的剩余行

select prodictid,productname,price,orderid,quantity from product Pfull outer join orderdetail O on P.productid=O.productid。

用外连接查询product表和orderdetail表的productid,productname,price,orderid,quantity字段的信息

10,、 子查询

子查询不是一条独立的查询语句,是查询语句的一部分,作为其他语句的条件或数据。子查询的特点就是在子查询的两边加小括号。

1,、单行单列子查询

select CustomerID,CompanyName, ConnrtName from Customer where CustomerID=(select CustomerID from Orders where OrderID='10249')

括号里面的即为子查询,本条语句的意思查询以CustomerID的值等于,子查询(OrderID号为10249的CustomerID的值)为条件的CustomerID,CompanyName,ConnrtName字段的信息。

2、多行单列子查询

需要用到关系运算符any和all

select Stocks from Product where Stocks>all(select Stocks from Product) 查询product表中Stocks字段,以Stocks大于所有子查询查出product表中Storks的值为条件的所有Stocks的信息。

select Stocks from Productwhere Stocks>any(select Stocks from Product)查询product表中stocks字段,以Stocks大于子查询查出的任意一个product表中Storks的值为条件的所有信息。

3,、多行多列查询

多行多列子查询是指返回多个行的多个字段的值,即查询结果中返回一个二维表结构的结果集。

select * from(select SaleID,Salename,DATEDIFF(YEAR,Birthday,GETDATE()) as 年龄 from Seller) 销售员表

select Salename,年龄 from (select SaleID,Salename,DATEDIFF(YEAR,Birthday,GETDATE()) as 年龄 from Seller)销售员表

4 、相关子查询

查询出每个班的成绩最高分的同学

select * from score t1 where t1.score in(select MAX(score) from score group by sclass having sclass=t1.sclass)

score t1的意思是给score表起一个别名叫t1。子查询的意思是:以班级分组,查询出每个班级的最高分。再把查询出的最高分结果集安条件显示,条件就是班级与前面查询班级一致。比如前半句查询的是1班子查询就会查出1班的最高成绩。这个可以自己建立几张表试试。

5、查询需要判断某个字段为0时有效为1时无效用下面语句

select case 列名 when 0 then '无效' when 1 then '有效' end from 表名
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: