查询数据

简单的查询

 create table stu_info
(
sno int not null
,sname varchar(20) not null
,sex varchar(2) not null
,birth varchar(20) not null
,email varchar(20) not null
,telephone int not null
,depart varchar(20) not null
) select distinct depart from dbo.stu_info
-- select order by depart from dbo.stu_info
--select sname ,datediff(year,birth,getdate()) from dbo.stu_info
--命名
select sname as 姓名 from dbo.stu_info
-- 把查询结果保存为一个新表 select sname as 姓名 into sname2 from dbo.stu_info
-- 查询 sname2 即使用了 into后
select * from sname2
--链接表字段
select sname+depart as 姓名来源 from dbo.stu_info

指定条件的查询   关键字 where

用到两个概念  指针和字段

条件表达式

条件运算符(这里列举我自己还没掌握的):SQL特殊条件运算符:

in                 :在某个集合中          学分(2,3,4)

not in           : 。。。

between       : 在某个范围             学分 between 2 and 3

not between  : 。。。。。。

like              : 与某种模式匹配       姓名 like '%三%'                  //似乎是通配符(第一感觉)

not like        :  。。。。。。

is NULL        : 是NULL值              联系方式 2 is NULL               // 这里只能大写 NULL

is nut NULL   :   。。。。。。

  --     where
select * from dbo.stu_info where sno>3
select * from dbo.stu_info where sno=3
select sno,sname,sex,depart from dbo.stu_info where sname>'李四'
-- 查询日期数据 SQL 默认格式 月/日/年
select sno,birth as 生日,date from dbo.stu_info where date>'01/05/1980'
select sno,birth as 生日,date from dbo.stu_info where date<'01/05/1980' and date>'01/05/1790'
-- 按范围查询数据
select * from dbo.stu_info where sno between 2 and 4
select * from dbo.stu_info where email is not null --排序查询数据 order by 后接 按哪个字段排名
select sno,sname,birth from dbo.stu_info order by sname -- 设置排名方向 asc 升 desc 降
select sno,sname,birth from dbo.stu_info order by sname desc
-- 按多列排序 在desc 后加上需要排序的字段 即可 (升序)
--........
-- 按字段位置排序 有时表达式过长,这样减少错误率 , 下面的2 代表select后面字段的第二个值
select sname ,datediff(year,date,getdate()) as 年龄 from dbo.stu_info order by 2 desc -- 查询前 几(2)行数据 关键字 top
select top 2 sno,sname,birth from stu_info order by birth
-- 查询前 n)行数据 关键字 top percent 百分之n
select top 2 percent sno,sname,birth from stu_info order by birth -- where 与 order by 结合使用 where 一定在前
select sno,sname,telephone,depart from stu_info where telephone is not null order by sno desc

高级条件查询

 select * from stu_info where depart in ('中文系','外语系','计算机系')
order by depart asc -- like 与 % 通配符 模糊查询 %代表0个或多个字符 select * from stu_info where sname like '%三%'
--为了更好的体现like+% 的作用,插入几条语句
insert into stu_info ( sno,sname,sex,birth,email,telephone,depart,date)
values(6,'刘三姐','男','zz','zhiniao@gmail.com','','软件系','05/15/1983')
select * from stu_info
select * from stu_info where sname like '%三%'
select * from stu_info where sname like '三%'
--rtrim 将右边的空格除去
select * from stu_info where rtrim(sname) like '%三'
-- 指定个数的字符 ' _ '
select * from stu_info where sname like '刘%'
select * from stu_info where sname like '刘_' --按通配符_个数匹配

通配符 [] :

[nr]%                代表以"n"或"r"字母开头的所有字符串

[a-d]%img         代表以"a"、"b"、"c"、"d"字母开头,以"img"结尾的所有字符串

n[^b]%             代表以"n"字母开头,并且第二个字母不是"b"的所有字符串

其它的自己琢磨   举三反全

 -- 通配符 []
select * from stu_info where sname like '[张李]%'
select * from stu_info where sname like '[^张李]%'

定义转义字符 escape        “  like '%5#%' escape '#'   ”

 select * from stu_info where sname like '[^张李]#%' escape '#'

SQL Server 基础 03 查询数据基础的更多相关文章

  1. Sql Server 存储过程中查询数据无法使用 Union(All)

    原文:Sql Server 存储过程中查询数据无法使用 Union(All) 微软Sql Server数据库中,书写存储过程时,关于查询数据,无法使用Union(All)关联多个查询. 1.先看一段正 ...

  2. sql server操作2:查询数据库语句大全【转】

    注:以下操作均建立在上篇文章sql Server操作1的数据基础之上 一.实验目的 熟悉SQL语句的基本使用方法,学习如何编写SQL语句来实现查询 二.实验内容和要求 使用SQL查询分析器查询数据,练 ...

  3. 【学习记录】第一章 数据库设计-《SQL Server数据库设计和开发基础篇视频课程》

    一.课程笔记 1.1  软件开发周期 (1)需求分析阶段 分析客户的业务和数据处理需求. (2)概要设计阶段 设计数据库的E-R模型图,确认需求信息的正确和完整. /* E-R图:实体-关系图(Ent ...

  4. SQL Server中Table字典数据的查询SQL示例代码

    SQL Server中Table字典数据的查询SQL示例代码 前言 在数据库系统原理与设计(第3版)教科书中这样写道: 数据库包含4类数据: 1.用户数据 2.元数据 3.索引 4.应用元数据 其中, ...

  5. 恢复SQL Server被误删除的数据

    恢复SQL Server被误删除的数据 <恢复SQL Server被误删除的数据(再扩展)> 地址:http://www.cnblogs.com/lyhabc/p/4620764.html ...

  6. MS SQL Server数据库修复/MDF数据文件数据恢复/MDF质疑/mdf无法附加

    微软的SQL Server 数据库最常用的有两种类型的文件: 1.主要数据文件,文件后缀一般是.MDF: 2.事务日志文件,文件后缀一般是.LDF. 用户数据表.视图.存储过程等等数据,都是存放在MD ...

  7. 转:Sql Server中清空所有数据表中的记录

    如果要删除数据表中所有数据只要遍历一下数据库再删除就可以了,清除所有数据我们可以使用搜索出所有表名,构造为一条SQL语句进行清除了,这里我一一给各位同学介绍.   使用sql删除数据库中所有表是不难的 ...

  8. Razor视图引擎布局 Razor视图引擎的基本概念与法语 SQL Server Mobile 和 .NET 数据访问接口之间的数据类型映射 binary 和 varbinary datetime 和 smalldatetime float 和 real

    Razor视图引擎布局   不需要像过去aspx一样,使用.Master文件,而是统一使用.cshtml 或 .vbhtml文件.但文件名一般以 _开头,这样做文件不会当做View显示出来 使用@Re ...

  9. Sql Server中清空所有数据表中的记录

    Sql Server中清空所有数据表中的记录 清空所有数据表中的记录: 代码如下:exec sp_msforeachtable  @Command1 ='truncate table ?'删除所有数据 ...

随机推荐

  1. PE框架学习之道:PE框架——发送报文流程

    PE框架发送报文,适用于PE及VX技术 步骤: 1.在action中使用发送报文,要指定报文在router端的交易名称 2.如果使用supe.execute(context)来发送,不需要第一步 3. ...

  2. c#Winform程序的toolStripButton自己定义背景应用演示样例源代码

    C# Winform程序的toolStrip中toolStripButton的背景是蓝色的,怎样改变背景及边框的颜色和样式呢? 实现此功能须要重写toolStripButton的Paint方法 这里仅 ...

  3. 行列转换小结 Pivot ,Unpivot (转,改)

    行专列 Pivot 1)SQL 2000版本 静态 SELECT ID , SUM(CASE Code WHEN 'Item1' THEN Value END) AS Item1 , SUM(CASE ...

  4. URAL 1225 Flags

    题目:click here #include <bits/stdc++.h> using namespace std; typedef long long ll; ; int n; ll ...

  5. JAVA语言的素数判断,随机数,函数调用

    近来刚学JAVA,就从JAVA写起吧,JAVA判别素数,其实方法和C/C++没什么区别,主要就是想谈一下,其中包括的3个点. (1)JAVA语言产生随机数,random函数,定义参数max的作用是给出 ...

  6. myEclipse快捷键及其常用设置

    快捷键:    查找替换:ctrl + f    复制行: ctrl + alt + down    删除行: ctrl + d    插入行: shift + enter, ctrl + shift ...

  7. thinkphp第二天

    1.使用print_r();打印数组的时候最好使用<pre>标签,可以是数组表现的更加直观. pre 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本 ...

  8. hdu 3641 Treasure Hunting 强大的二分

    /** 大意:给定一组ai,bi . m = a1^b1 *a2^b2 * a3^ b3 * a4^b4*...*ai^bi 求最小的x!%m =0 思路: 将ai 质因子分解,若是x!%m=0 那么 ...

  9. 解析LayoutSubviews

    layoutSubviews作用 layoutSubviews是对subviews重新布局.比如,我们想更新子视图的位置的时候,可以通过调用layoutSubviews方法,既可以实现对子视图重新布局 ...

  10. Pagodas(等差数列)

    Pagodas Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...