查询数据

简单的查询

 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. MVC设计模式JavaWeb实现

    JSP开发模式 jsp开发模式的发展 1.模式1:(适合小型项目的技术的开发)     a.第一版本号.纯jsp(封装数据.处理数据,显示数据)     b.第二版本号,Jsp+JavaBean.   ...

  2. c 这题做了半天,虽然做好了,但是思路还是不清晰,估计让我再做一次还是比较花时间的。

    输入一个大写字符,如F 比如: 输入:F 输出: FEDCBA EDCBAB DCBABC CBABCD BABCDE ABCDEF 输入 B 输出: BA AB #include<stdio. ...

  3. iOS开发常识

    一.NSString 创建字符串.  NSString *astring = @"This is a String!"; 创建空字符串,给予赋值.  NSString *astri ...

  4. Sharepoint 2013 启用搜做服务

    参考文件: http://www.cnblogs.com/jianyus/archive/2013/02/04/2891801.html 1. 创建好网站集,进入网站内容,点击搜素,会出现如下错误:( ...

  5. 【转】CTE(公用表表达式)

    本文转自:爽朗的微笑  http://www.cnblogs.com/shuangnet/archive/2013/03/22/2975929.html 公用表表达式 (CTE) 具有一个重要的优点, ...

  6. Javah生成JNI头文件

    首先确保java的环境变量配置好了. 1:打开cmd 进入doc命令窗口: 进入class所在目录,我的class是在F:\summerVacation\ndkhelloworld\bin\class ...

  7. sqm(sqlmapGUI) pcat修改版

    sqlmap是一款开源的注入工具,支持几乎所有的数据库,支持get/post/cookie注入,支持错误回显注入/盲注,还有其他多种注入方法. 支持代理,指纹识别技术判断数据库 .而sqm(sqlma ...

  8. git 删除右键菜单

    cmd进入"C:\Program Files (x86)\Git\git-cheetah"目录,运行regsvr32 /u git_shell_ext(64).dll

  9. Qt 技巧: 解决未解析的SSL问题

    因为https访问需要用到SSL认证,而QT默认是不支持SSL认证,所以在使用之前必须先做一些准备工作: 需要安装OpenSSL库: 1.首先打开http://slproweb.com/product ...

  10. perl学习(4) 子程序

    子程序,类比c语言中的函数,在形式上个人认为最大的区别:没有形参 1.1.定义子程序 1.2.调用 #! /usr/bin/perl sub marine { $n += 1 ; print &quo ...