查询数据

简单的查询

 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. MongoDB初探系列之二:认识MongoDB提供的一些经常使用工具

    在初探一中,我们已经能够顺利的将MongoDB在我们自己的机器上跑起来了. 可是在其bin文件夹以下另一些我们不熟知的工具.接下来,将介绍一下各个小工具的用途以及初探一中MongoDB在data文件夹 ...

  2. asp.net根据模版生成Word小记

    最近遇到一个问题,客户提了一个新的需求,客户想要将显示在网页上的数据导出成Word进行套打,由于之前没有接触过这一块的内容,自己写的系统也没有使用这种功能,现在重头学习. 具体思路: 1.先制作Wor ...

  3. LeetCode 二叉树后序遍历(binary-tree-postorder-traversal)

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  4. Definitions

    Definitions and ODR Definitions are declarations that fully define the entity introduced by the decl ...

  5. C++之类的静态变量

    成员变量 通过对象名能够访问public成员变量 每个对象都可以有只属于自己的成员变量 成员变量不能在对象之间共享 类的静态成员 静态成员变量  存储在   全局数据区 #include<std ...

  6. 高级UNIX环境编程4 文件和目录

    #include<sys/stat.h> stat fstat lstat fchmod 对已打开的文件操作

  7. 蝕刻技術(Etching Technology)

    1. 前言 蚀刻是将材料使用化学反应或物理撞击作用而移除的技术. 蚀刻技术可以分为『湿蚀刻』(wet etching)及『干蚀刻』(dry etching)两类.在湿蚀刻中是使用化学溶液,经由化学反应 ...

  8. python 模块BeautifulSoup使用

    BeautifulSoup是一个专门用于解析html/xml的库.官网:http://www.crummy.com/software/BeautifulSoup/ 说明,BS有了4.x的版本了.官方说 ...

  9. C/C++用strncpy()与strstr()分割与匹配查找字符串

    最近做题遇到分割与匹配字符串的题目(hdu5311),看来别人的代码,才知道有strncpy()和strstr()函数,于是搜集了一点资料,记录一下基本用法. 一.strncpy() char * s ...

  10. poj 1256 Anagram(dfs)

    题目链接:http://poj.org/problem?id=1256 思路分析:该题为含有重复元素的全排列问题:由于题目中字符长度较小,采用暴力法解决. 代码如下: #include <ios ...