zoukankan      html  css  js  c++  java
  • SQL SELECT 语句

     

    本章讲解 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" 表:

    IdLastNameFirstNameAddressCity
    1 Adams John Oxford Street London
    2 Bush George Fifth Avenue New York
    3 Carter Thomas Changan Street Beijing

    结果:

    LastNameFirstName
    Adams John
    Bush George
    Carter Thomas

    SQL SELECT * 实例

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

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

    SELECT * FROM Persons

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

    结果:

    IdLastNameFirstNameAddressCity
    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  按音序查询

  • 相关阅读:
    作业4
    PSP记录个人项目耗时
    代码复审
    是否需要有代码规范?
    作业二(1)
    作业二(2)
    作业一
    软件工程学期总结
    作业4
    作业3(PSP表格)
  • 原文地址:https://www.cnblogs.com/jingli6174/p/5522896.html
Copyright © 2011-2022 走看看