Day2(11.1):(4)最简单的查询语句
2015-11-07 00:21
267 查看
-- 2 ****************************************************
-- 最简单的查询语句
-- 2.1 --------------------------------------------------
-- 使用 select 语句查询表中的数据
-- SELECT * FROM table_name
use pubs -- 切换当前数据库
select * from authors
-- 2.2 --------------------------------------------------
-- 使用完全限定名称
-- SELECT * FROM [server_name].[database_name].[owner].[object]
select * from Northwind.dbo.Employees
select * from Northwind..Orders
-- 2.3 --------------------------------------------------
-- 查询数据表中指定的列
-- SELECT column_1,column_2 FROM table_name
select * from authors
select au_id,au_lname,au_fname,city from authors
-- 2.4 --------------------------------------------------
-- 使用 where 字句筛选指定的行
-- SELECT * FROM table_name WHERE <search_condition>
select * from jobs
select * from jobs where job_id=7
select * from authors
select * from authors where au_lname = 'White'
-- 2.5 --------------------------------------------------
-- where 字句中的搜索条件
-- 比较操作符 =, <, >, <=, >=, <>
-- 字符串比较符 like, not like
-- 逻辑操作符 and, or, not
-- 值的域 between, not between
-- 值的列表 in, not in
-- 未知的值 is null, is not null
select * from jobs where job_id >=10
select * from jobs where job_desc like '%manager%'
select * from jobs where job_id >=10 AND job_desc like '%manager%'
select * from jobs where min_lvl between 100 and 150
select * from jobs where job_id in (1,3,5,7,11,13)
select * from discounts where stor_id is null
-- 2.6 --------------------------------------------------
-- 使用 like
-- % 代表 0 个或多个字符串
-- _ 代表任何单个的字符
-- [] 代表指定区域内的任何单个字符
-- [^] 代表不在指定区域内的任何单个字符
select * from authors where au_lname like 'G%'
select * from authors where address like '%Av.%'
select * from authors where au_fname like 'A__'
select * from authors where au_fname like '[AS]%'
select * from authors where au_lname like 'S[^m]%'
-- 2.7 --------------------------------------------------
-- 格式化结果集
-- order by 排序结果集
-- distinct 消除重复的行
-- as 改变字段的名字
select * from employee order by fname
select * from employee order by hire_date desc
select distinct type from titles
select au_id,au_lname as [Last Name],au_fname as [First Name] from authors
-- 练习 --------------------------------------------------
-- 1 查询本数据库服务器上有哪些数据库
-- 2 查询 Northwind 数据库中 Products 数据表中 ProductID 为 14 的产品信息
-- 3 查询 Northwind 数据库中 Orders 数据表中没有按时送达的订单
-- 4 查询 Pubs 数据库中 Titles 数据表中 书名含有 'computer' 的书,并按照价格降序排序
-- 5 查询 Northwind 数据库中 Territories 数据表中的地域信息,按照地域名升序排序,去掉重复项
-- 最简单的查询语句
-- 2.1 --------------------------------------------------
-- 使用 select 语句查询表中的数据
-- SELECT * FROM table_name
use pubs -- 切换当前数据库
select * from authors
-- 2.2 --------------------------------------------------
-- 使用完全限定名称
-- SELECT * FROM [server_name].[database_name].[owner].[object]
select * from Northwind.dbo.Employees
select * from Northwind..Orders
-- 2.3 --------------------------------------------------
-- 查询数据表中指定的列
-- SELECT column_1,column_2 FROM table_name
select * from authors
select au_id,au_lname,au_fname,city from authors
-- 2.4 --------------------------------------------------
-- 使用 where 字句筛选指定的行
-- SELECT * FROM table_name WHERE <search_condition>
select * from jobs
select * from jobs where job_id=7
select * from authors
select * from authors where au_lname = 'White'
-- 2.5 --------------------------------------------------
-- where 字句中的搜索条件
-- 比较操作符 =, <, >, <=, >=, <>
-- 字符串比较符 like, not like
-- 逻辑操作符 and, or, not
-- 值的域 between, not between
-- 值的列表 in, not in
-- 未知的值 is null, is not null
select * from jobs where job_id >=10
select * from jobs where job_desc like '%manager%'
select * from jobs where job_id >=10 AND job_desc like '%manager%'
select * from jobs where min_lvl between 100 and 150
select * from jobs where job_id in (1,3,5,7,11,13)
select * from discounts where stor_id is null
-- 2.6 --------------------------------------------------
-- 使用 like
-- % 代表 0 个或多个字符串
-- _ 代表任何单个的字符
-- [] 代表指定区域内的任何单个字符
-- [^] 代表不在指定区域内的任何单个字符
select * from authors where au_lname like 'G%'
select * from authors where address like '%Av.%'
select * from authors where au_fname like 'A__'
select * from authors where au_fname like '[AS]%'
select * from authors where au_lname like 'S[^m]%'
-- 2.7 --------------------------------------------------
-- 格式化结果集
-- order by 排序结果集
-- distinct 消除重复的行
-- as 改变字段的名字
select * from employee order by fname
select * from employee order by hire_date desc
select distinct type from titles
select au_id,au_lname as [Last Name],au_fname as [First Name] from authors
-- 练习 --------------------------------------------------
-- 1 查询本数据库服务器上有哪些数据库
-- 2 查询 Northwind 数据库中 Products 数据表中 ProductID 为 14 的产品信息
-- 3 查询 Northwind 数据库中 Orders 数据表中没有按时送达的订单
-- 4 查询 Pubs 数据库中 Titles 数据表中 书名含有 'computer' 的书,并按照价格降序排序
-- 5 查询 Northwind 数据库中 Territories 数据表中的地域信息,按照地域名升序排序,去掉重复项
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- 数据库链接字符串查询网站
- 我是运营,我没有假期
- DB2实例管理
- DB2实例管理
- 保障MySQL数据安全的14个最佳方法
- mysql问答汇集
- 创建一个空的IBM DB2 ECO数据库的方法
- Access 2000 数据库 80 万记录通用快速分页类
- 开通一个数据库失败的原因的和解决办法
- 一个简单的asp数据库操作类
- DB2数据库的安装
- C#实现把指定数据写入串口
- CentOS下DB2数据库安装过程详解
- “传奇”图象数据存储方式
- EasyASP v1.5发布(包含数据库操作类,原clsDbCtrl.asp)第1/2页
- sql2008 还原数据库解决方案
- Oracle 数据库自动存储管理-安装配置
- Oracle 数据库 临时数据的处理方法