.Net_03_查询语句 (Sql 语句)

select * from 表 where 条件

1.查询语句中的条件

  -> 可以对数据进行筛选

  -> 只有数据的对应条件满足要求的时候,才会将数据取出

  -> 如果有多个条件,使用and、or等连接起来  

  ->在SQL Server中条件判断所用到的判断方法 =, > ,< ,>= ,<=, <> (!=) ,!> (<=), !< (>=) ,is not null

  between 18 and 26 (范围) 等等;
1   select * frm Student where stuAge >=10 and stuAge<30
2    --例如筛选年龄在18到26岁之间
3    select * from Student where stuAge between 18 and 26

 

2.时间函数

  getdate()获取当前时间。

  year()、month()、day() 这些函数可以得到时间的对应部分

1 --查询年龄
2 select year(getdate())-year(strBirthdate) as 年龄 from Student

3.聚合函数

  -> MAX(字段)  --最大值

  -> MIN(字段) --最小值

  -> AVG(字段)  --平均值

    -> 在计算时,对于null的数据不计入总是

  -> SUM(字段)  --总和

  -> COUNT(字段) 

    -> count(*)计算所有的列、count(字段)不计为null的列

 1 select 
 2     max(year(getdate()) - year(stuBirthdate)) as 最大年龄,
 3     min(year(getdate()) - year(stuBirthdate)) as 最小年龄
 4 from Student;
 5 ------------------------------------------------------
 6 select 
 7     sum(testBase) as 基础课程总分,
 8     avg(testBase) as 基础课程平均分,
 9 from 
10     Score
11 where
12     stuId = 1;
13 -----------------------------------------------------
14 select count(*) as 总人数
15     from Student
16 where 
17     year(stuStudydate) = 2012
18     and
19     month(stuStudydate) = 7;

4.模糊查询

  在查询过程中使用“字段=值”形式表示这个字符应该是多少的时候,

  才获取数据 如果在处理某些字符串的时候(匹配姓氏等),只需要匹配一部分,

  那么就得考虑模糊查询 在SQL Server中模糊查询常用的关键字符有三个 :

    % 任意个任意字符

    _ 任意一个字符;

    [] 在方括号中的任意一个字符,使用;

    [^]表示不在方括号中的一个字符

      在使用模糊匹配的时候使用的是like

select * from Student where address like '北京%';
select * from Student where Name like '张_';
select * from Student where Name  like '[李张王]三';
select * from Student where  Name like '[^张李王]三';

5.case函数

  -> 简单case函数(switch-case)
    case 字段
      when 值1 then 显示1
      when 值2 then 显示2
      ...
      when 值n then 显示n
      else 显示n+1
    end
  -> 搜索case函数(if else-if)
    case
      when 关于字段的表达式 then 显示1
      when 关于字段的表达式 then 显示2
      ...
      when 关于字段的表达式 then 显示n
      else 显示n+1
    end

 1 select
 2     stuId, 
 3     stuName, 
 4     case stuSex
 5         when 'f' then ''
 6         when 'm' then ''
 7         else '未知'
 8     end as [stuSex],
 9     stuBirthdate
10 from 
11     Student
12 ------------------------------------------------------
13 select
14     stuId, 
15     stuName, 
16     case
17         when stuSex = 'f' then ''
18         else ''
19     end as [stuSex],
20     stuBirthdate
21 from 
22     Student

 6.独立标量子查询(子查询返回一个值)

  -> 有一部分查询,内部查询与外部查询有一定的相关联信息,称为相关子查询

  -> 求“凤姐”同学的考试分数

  -> 将一个查询,查出来的一个值,作为另一个查询的一个条件的匹配项

  -> 必须保证子查询中的结果为一个“单值”

    例如分别有两个表一个分数表Score(主键表),一个学生表Student(外键表) 

1 Select 分数 from Score where 学号 inSelect 学号 from Student where Name ='凤姐'

7.表链接

  -> 交叉连接(*)
    求两个表的笛卡尔积
    select t1.成员, t2.成员 from
      表1 as t1
      cross join
      表2 as t2

  -> 内连接(在交叉连接基础之上多进行的筛选)
    -> 先实现交叉连接
    -> 在交叉连接的基础之上进行条件筛选
    select *
    from
      表1 as t1
      inner join
      表2 as t2
      on t1.字段 = t2.字段
    where
      t1.字段 = ...

  -> 外连接
    外连接分为左外连接与右外连接
    -> 左外连接就是在内连接的基础之上
    -> 将左中有的数据,而右表中没有数据的项再添加到结果中
    -> 没有数据的项,补null
    select *
    from
      左表 as s1
      left join
      右表 as s2
      on s1.字段 = s2.字段

  -> 连接中数据是合并表,但是修改了表的结构
  -> 联合中数据也是合并,但是没有修改表的结构,而是简单的将数据加起来

 1 create table ltbl1
 2 (
 3     id int,
 4     name nvarchar(5)
 5 );
 6 create table ltbl2
 7 (
 8     id int,
 9     name nvarchar(5)
10 );
11 
12 insert into ltbl1(id, name) values(3, '李四');
13 insert into ltbl1(id) values(2);
14 
15 insert into ltbl2(id, name) values(1, '程序员');
16 insert into ltbl2(id, name) values(2, '测试员');
17 
18 select * from ltbl1;
19 select * from ltbl2;
20 
21 --交叉连接
22 select * from ltbl1 cross join ltbl2
23 --内连接
24 select * from ltbl1 as t1 inner join ltbl2 as t2 on t1.id = t2.id;
25 --做外连接,将含有李四的那张表放在坐标的位置
26 select * from ltbl1 as t1 left join ltbl2 as t2 on t1.id = t2.id;

10、复制表结构/表结构和数据,创建新表

  select * into 新表名 from 旧表名 where 条件

   -> select * from 旧表名 where 1 > 2
      -> 检索所有数据中,满足where条件的数据,并将所有满足的数据列出来
      -> 没有检索出任何数据
   -> select *
      into 新表名
      from 旧表名 where 1 > 2
      -> 将检索出来的数据,加到新表中
      -> 有没有数据呢?没有,那么新表就只包含旧表的表结构
      -> 表结构包含所有的字段,以及identity的属性,没有包含约束
      -> 需要手动添加约束

1 --复制表结构,创建新表。
2     select * into newStudent from Student where 1 > 2
3 --复制表结构,并且把id小于10的数据复制,创建新表
4     select * into newStudent from Student where id<10

 

转载于:https://www.cnblogs.com/-ljj/archive/2013/01/29/2882012.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值