查询数据

简单的查询

 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. JavaScript中cookie的路径(path)和域(domain)

    cookie虽然是由一个网页所创建,但并不只是创建cookie的网页才能读 取该cookie.在默认情况下,与创建cookie的网页在同一目录或子目录下的所有网页都可以读取该cookie.但如果在这个 ...

  2. 使用超链接跳转页面(GridView)

    1. the html markup <div> <asp:GridView ID=" OnPageIndexChanging="GridView1_PageIn ...

  3. 用反射写自己的DataTable转为对应的Mod

    之前写过类似的方法,今天做项目的时候又遇到了,以前的代码没有保存,导致又得重新写 场景:当我们定义自己的很多模型(Mods)的时候,而数据库读取出来的却是DataSet,DataTable类型的时候, ...

  4. Usaco 2.3 Zero Sums(回溯DFS)--暴搜

    Zero SumConsider the sequence of digits from 1 through N (where N=9) in increasing order: 1 2 3 ... ...

  5. js实现input输入框只能输入数字的功能

    <input type="text" style="ime-mode:disabled;" onpaste="return false;&quo ...

  6. HttpGet()和HttpPost()

    转 http://www.cnblogs.com/hyddd/archive/2009/03/31/1426026.html Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,P ...

  7. android 获取本机号码需要root吗?

    首先要明白,有的手机是获取不到自身的手机号的, 查了些资料,有以下两种方式可以获取到:      1. 通过对方给你发短信,打电话获取本机号码:       2. 还有一个就是通过APN来查询,但是这 ...

  8. 如何修改Sublime 侧边栏Sidebar的颜色

    参考自:http://blog.csdn.net/a497393102/article/details/10563791 首先要找到 Default.sublime-theme 文件, 点击 subl ...

  9. java学习之二叉树的实现

    二叉树是一种数据结构,每个节点都有两个子节点. 二叉树的遍历有三种方式, 先序遍历是 根节点,左子树,右子树: 中序遍历是 左子树,根节点,右子树: 后序遍历是 左子树,右子树,根节点: java实现 ...

  10. poj 3624 Charm Bracelet(区间dp)

    题目链接:http://poj.org/problem?id=3624 思路分析: 经典的0-1背包问题: 分析如下: 代码如下: #include <iostream> using na ...