本章讲解 SELECT 和 SELECT * 语句。

SQL SELECT 语句

SELECT 语句用于从表中选取数据。

结果被存储在一个结果表中(称为结果集)。

SQL SELECT 语法

SELECT 列名称 FROM 表名称

以及:

SELECT * FROM 表名称

注释:SQL 语句对大小写不敏感。SELECT 等效于 select。

SQL SELECT 实例

如需获取名为 "LastName" 和 "FirstName" 的列的内容(从名为 "Persons" 的数据库表),请使用类似这样的 SELECT 语句:

SELECT LastName,FirstName FROM Persons

"Persons" 表:

Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing

结果:

LastName FirstName
Adams John
Bush George
Carter Thomas

SQL SELECT * 实例

现在我们希望从 "Persons" 表中选取所有的列。

请使用符号 * 取代列的名称,就像这样:

SELECT * FROM Persons

提示:星号(*)是选取所有列的快捷方式。

结果:

Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing

在结果集(result-set)中导航

由 SQL 查询程序获得的结果被存放在一个结果集中。大多数数据库软件系统都允许使用编程函数在结果集中进行导航,比如:Move-To-First-Record、Get-Record-Content、Move-To-Next-Record 等等。

类似这些编程函数不在本教程讲解之列。如需学习通过函数调用访问数据的知识,请访问我们的 ADO 教程PHP 教程

Select top 3 * from class order by id desc 表示查询前3条的数据并且按降来排
select distinct 年龄 from class  去除重复行的查询
select top 2 * from class order by newid() 表示随便查询2行数据
select * from class where 年龄=20 or 年龄=19 and 性别='男' 表示先查询年龄等于19 并且是男的 在查询所有年龄等于20的人  先执行AND查询 再执行OR
select * from class where id not in (select id from class1) 表示查询表一中在表2中没有的数据
select * from class where id between 1 and 5 表示查询1到5的数据
select * from class where id not between 1 and 5 表示查询不是1到5的数据
select * from class where 姓名like '刘%' 表示查询刘开头的人
select * from class where 姓名like '%丹%'查询包含丹的人
select * from class where 姓名like '[刘丹]%'查询以刘或者以丹开头的数据
select * from class where 姓名like '[%刘丹]%'查询包含丹或者刘的数据
select * from class where 年龄like '[^1-2]%'查询数据不是以1到2之间开头的
select * from class where 年龄like '[^刘陈]%' 查询不是以刘或者陈开头的数据
select * from class where 工作地址is null 表示查询工作地址不是空值的数据
select * from class where 工作地址is not null 查询不是空植的数据
select * from class where id <> all(select id from class1)表示查询表2在表一中没有的ID数据
select * from class where id = any(select id from class1) 查询表一中和表2相同的ID
elect * from class where (性别not in ('男'))and (not (年龄between 18 and 21))
not语句的查询 在条件语句前面加上NOT
select top 3 * from  class order by id 前面最三个数据
use liudan
select top 3 * from  class order by id desc 最后三个的数据
select ltrim(rtrim(姓名)) from class 里面函数RTRIM代表删除结尾空格数据 外面函数 ltrim 删除前面空格的数据
select 姓名+',' as 姓名,lower(gege) as gege from class    给姓名的每条数据加上 ,; 把gege 列转换为小写
select 姓名+',' as 姓名,upper(gege) as gege from class 后面函数把数据全部转换为大写
select * from class where month(日期)=9 and year(日期)=1993 and day(日期)=9
分别是三个函数 第一个是对指定的日期 月 ,年,日
select 年龄-id as a from class 表示用每一行的数据用年龄-id得到的数据+ - * / 都可以用
select sum(年龄) as a from class  求和
select * from class where 年龄>(select avg(年龄) from class)  求年龄大于平均值的数据
select * from class where 年龄= (select max(年龄) from class) 求年龄最大的、
select * from class where 年龄= (select min(年龄) from class) 年龄最小的人
select count(*) from class 表中数据总条数
select count(年龄) from class 查询年龄不为空的
select sum(年龄) from class where 性别='男' 得到性别为男的的总年龄数
select 年龄,count(*) from class group by 年龄      表示对年龄进行汇总,就是说 对相同年龄的人人数进行汇总
select 年龄,count(*) from class group by 年龄having 年龄>20 对年龄大于20的才进行汇总
select 年龄,count(*) from class group by 年龄having (年龄 in (20))  年龄在20范围内的汇总
select * from class union select * from class3 将两个表连接到一起来 删除重复的行
select * from class union all select * from class3 保留重复的行
select id from class intersect
select id from class1    两个表相同的数据 这里因为没有两个相同的表所以只查询ID相同的
select * from class except select * from class3 两个相同的表的不相同的数据
insert into class3 (姓名)values ('dadad')  表示在表中的姓名列插入一个数据  只所以要写是哪个列的数据 是因为不用我插入id了 我门一般设置id是自动生成的 所以再这里要注明;;
update class3 set 工作地址='湖北' where id=2 对ID=2的行 的工作地址的列进行修改
update class3 set 工作地址='湖北' 对所有工作地址的列进行修改
delete top(1) class 删除表中前1条的数据
delete from class3 where id=2删除ID=2的行
select top 3 * from class order by newid() 随机查询3条数据
select * from class order by 名字 collate chinese_prc_cs_as  按音序查询

SQL SELECT 语句的更多相关文章

  1. SQL SELECT语句

    基本SQL SELECT语句   1.       下面的语句是否可以执行成功 select ename , job , sal as salary  from emp; 2.       下面的语句 ...

  2. SQL Select语句完整的执行顺序(转)

    SQL Select语句完整的执行顺序: 1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函 ...

  3. 170607、SQL Select语句完整的执行顺序

    SQL Select语句完整的执行顺序: 1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函 ...

  4. SQL-W3School-基础:SQL SELECT 语句

    ylbtech-SQL-W3School-基础:SQL SELECT 语句 1.返回顶部 1. 本章讲解 SELECT 和 SELECT * 语句. SQL SELECT 语句 SELECT 语句用于 ...

  5. 优化 SQL SELECT 语句性能

    SELECT语句的性能调优有时是一个非常耗时的任务,在我看来它遵循帕累托原则.20%的努力很可能会给你带来80%的性能提升,而为了获得另外20%的性能提升你可能需要花费80%的时间. 检查索引:在SQ ...

  6. SQL select语句执行顺序

    sql查询原理和Select执行顺序 关键字: 数据库 一 sql语句的执行步骤 1)语法分析,分析语句的语法是否符合规范,衡量语句中各表达式的意义. 2) 语义分析,检查语句中涉及的所有数据库对象是 ...

  7. SQL Select语句完整的执行顺序

    1.from子句组装来自不同数据源的数据: 2.where子句基于指定的条件对记录行进行筛选: 3.group by子句将数据划分为多个分组: 4.使用聚集函数进行计算: 5. 使用having子句筛 ...

  8. oracle 10g 学习之基本 SQL SELECT 语句(4)

    本篇文章中,对于有的和MSSQL Server相同的语法我就没有再写了,这里我只写Oracle和MSSQL Server有点不同的 定义空值 l  空值是无效的,未指定的,未知的或不可预知的值 l  ...

  9. Oracle系列二 基本的SQL SELECT语句

    1.查询表中全部数据 示例: SELECT * FROM employees; 说明: SELECT   标识 选择哪些列. FROM      标识从哪个表中选择. *           选择全部 ...

随机推荐

  1. WCF服务客户端首页调用慢的问题处理

    场景: WCF服务架设于IIS服务中,走TCP协议.客户端首次调用特别慢,第一次加载完后,都正常. 解决: 把服务中需要序列化的模型所在的工程 > 属性 > 生成 > 生成序列化程序 ...

  2. Visual studio 类视图和资源视图不显示的问题

    关于Visual studio 类视图和资源视图不显示的问题 解决方法: 1. 工具—选项—文本编辑器—C/C++—高级,浏览/导航下的禁用数据库选项置为False; 2. 输入命令:devenv / ...

  3. sql server 跨数据库插入数据

    创建服务器的连接,创建好后可以存在服务器上,可以在不同位置重复使用,和系统函数类似 exec sp_addlinkedserver 'RemoteServer', '', 'SQLOLEDB ', ' ...

  4. Eclipse插件推荐

    1.Eclipse颜色插件 https://github.com/eclipse-color-theme/eclipse-color-theme 2.google Code Analysis http ...

  5. UIScrollView 性能优化 - view转为Image

    进入做地图闹钟app,图层关系是这样的: subwayView 上先绘制线路上各个元素:线条 ,站点名称-Label,站点位置(画圆圈表示)-View.shapeLayer UIBezierPath ...

  6. 原生js获取元素style属性

    function getStyle(ele,attr){ if( ele.currentStyle ){ return ele.currentStyle[attr]; // ie } else { r ...

  7. c#Ice开发之环境配置(一)

    第一步,基于Windows下的安装,所以下载windows版的Ice,官网最新版本是Ice3.5.1: http://www.zeroc.com/download/ 安装完成可以在vs-工具的最下面看 ...

  8. ALSA 学习小记

    对于playback snd_pcm_begin snd_pcm_commit, 貌似 commit给的frame才会使得alsa去把数据填充 转自 http://magodo.github.io/ ...

  9. 异常:“System.Reflection.Metadata”已拥有为“System.Collections.Immutable”定义的依赖项

    参考动态执行T4模板:https://msdn.microsoft.com/zh-cn/library/bb126579.aspx 我项目是.NET Framework 4.5控制台应用程序写的. 执 ...

  10. 黑马程序员_ Objective-c 之Foundation笔记(二)

    NSArray NSArray的创建 NSArray *array = [NSArray arrayWithObject:@“jack”]   创建单个元素 NSArray *array3 = [NS ...