数据库查询语句

简单的查询语句 select  *  from   tabename
查询的列给别名  select  column1 as  AA ,column2  as BB  from tablename  where 条件

top 的使用     select  top  3   *   from  tablename
                        select  top  30  percent  *   from   tablename  (取前30%的数据)

Distinct 去除重复数据(DISTINCT是对查询出的整个结果集进行数据重复处理的,而不是针对某一个列)

聚合函数
MAX (最大值)、 MIN (最小值)、 AVG (平均值)、 SUM (和)、 COUNT (数量 : 记录的条数。)
聚合函数对 null 值不计算。
如果一行的数据都是 null count(*) 包含对空值行、重复行的统计。

Between…and… 在之间
例:select   *   from  tabename  where  id between  2  and   20    //查询ID为 2到20之间的记录

select   *   from  tabename  where  id  in  (2,3,4) //查询ID为2,3,4的记录

通配符
_    、   %   、  []   、  ^
^ 只有 MSSQL Server 支持,其他 DBMS not like
通配符 % 多字符匹配的通配符,它匹配任意次数(零或多个)出现的任意字符
通配符 _ 单字符匹配,它匹配单个出现的字符
[] 只匹配一个字符  并且这个字符必须是 [] 范围内的    [0-9]  [a-z]
not like 一起使用: notlike ….
要通配 _ % [ ^ 这些字符怎么办? [_] [%] [ [ ] ^( 不需要放到中括号里,因为 ^ 只有放到中括号中才认为是通配符 )  其实中括号就相当于转义字符

空值处理
数据库中,一个列如果没有指定值,那么值就为 null ,数据库中的 null 表示“ 不知道 ”,而不是表示没有。因此 select null+1 结果是 null ,因为“不知道”加 1 的结果还是“不知道”。
select* from score where english = null
select* from score where english != null ;都没有任何返回结果,因为数据库也“不知道”。
SQL 中使用 is null is not null 来进行空值判断: select * from score where english is null select * from score where english is notnull
ISNULL ( check_expression , replacement_value )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值