基于SQLServer的SQL语言使用--查询

查询 select

查询也称检索。
必须给出俩条信息:
要选择什么?
选择那一行或那几行;选择那些列或者全部列。
从什么地方选择?
要检索的数据来自那个表。

查询数据库中的表

查询此时所在的数据库中的表(非系统表):

SELECT NAME FROM SYSOBJECTS where type = 'U';

查询数据库中所有表(包含表信息):

SELECT * FROM INFORMATION_SCHEMA.TABLES

查看表的结构

知道了表名,根据表名查看表的结构(包含列名)
查询表结构时,最好单独执行

sp_columns table_1;
sp_help table_1;

知道了表的列名,可以开始查询表中数据了。

开始查询表中数据

查询表中部分列的所有行:
表1中共有四列,此条语句选择三列

select id,age,gender from table_1;

更换检索列的排列,就可以得到排列不同的结果集:

select age,id,gender from table_1;

检索表中所有列的所有行

select * from table_1;

检索语句带星号(*)表示查询所有列
得到表中所有数据
如果表中数据记录非常多时,使用此语句就不太合适了。

限制检索数据的数量

select top 5 * from table_1;

只显示表的前5条数据,但每条数据包含所有列。

包含部分列的限制查询:

select top 3 id,age,phone from table_1;

检索表的前3条数据,包含3个选择列。

只检索不重复的值–distinct

创建一个新的表:
包含三个列(column)

create table 
distinct_table
 (id int not null primary key,
 name varchar(5) not null,
 age int );

为新表插入数据:
因为我们是为了进行去重查询,所以为name,age列填入重复数据

insert into 
distinct_table
 (id,name,age)
values
 (1,'张大',11),
 (2,'张大',22),
 (3,'张三',33),
 (4,'张四',33);

开始去重查询:

select distinct age from table_33;
--得到只包含age列的不重复数据结果集。
select distinct age,name from distinct_table;
--distinct关键字后添加多个列虽然不会报错,但也不会再起到去重作用。
select distinct * from distinct_table;
--执行‘所有列’的去重,效果与不执行去重相同。
select * from distinct_table;

过滤数据检索–where

为检索数据添加条件,指定范围
检索条件在表名之后给出(from表名 where条件)

select * from distinct_table 
where age = 11;
--添加过滤条件,年龄等于11
--检索年龄等于11的记录
select * from distinct_table
where name = '张大';
--where子句中用''单引号来限定字符串。
select * from distinct_table 
where age <= 22;
--检索表中,年龄小于等于22的记录,包括22。
select * from distinct_table
where age >=22;
--检索年龄大于等于22的记录,包括22.
select * from distinct_table
where age < 33;
--检索年龄小于33的记录。
select * from distinct_table
where age > 22;
--检索年龄大于22的记录。
select * from distinct_table
where age != 33;
--检索年龄不等于33的记录。
select * from distinct_table
where age <> 33;
select * from distinct_table
where age !< 33;
--检索年龄不小于33的记录,不包括33.
select * from distinct_table
where age !> 33;
--检索年龄不大于33的记录,因为没有年龄大于33记录所有全部检索。

空值检索–is null

select * from table_2
where nowdate is null;
--检索现在时间列是空的记录。

非空值检索

select * from table_2
where nowdate is not null;
--检索现在时间列不为空的记录。

高级过滤–and–or

and ‘与’ 操作符,俩个或多个条件同时满足时,返回记录。
or ‘或’ 操作符,有一个条件满足时就返回数据。
WHERE语句可以包含任意数量的AND和OR操作符。
同时使用时and的优先级高于or
圆括号有着比and和or更高的优先级。

select * from distinct_table
where id =2 and name = '张大';
--检索id等于2与名称是张大的记录。
--俩个或多个条件同时满足时,返回记录。
select * from distinct_table
where id =2 or age = 77;
select * from distinct_table
where   age = 77  or id =2;
/*
俩条检索的结果相同
检索id等于2或者年龄等于77的记录。
年龄列中没有77的值,所以只返回id等于2的记录。
*/
select * from distinct_table
where id =2 or age = 33;
--俩个条件都满足时,则俩个条件的结果都返回。

范围值检索–between

在俩者之间

select * from distinct_table
where age between 11 and 22;
--检索年龄在11与22之间的记录。and‘与’操作符

IN操作符

用来指定条件范围,范围中的每个条件都可以进行匹配,即使有不匹配 的选项也不会报错

select * from distinct_table
where   name  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值